moxley

moxley

I defined a generic mutation action that takes an :id argument and an :input argument with an embedded resource backing :input. When I test the AshGraphql mutation, it doesn’t know about the arguments. It appears that AshGraphql has generated an input type that I can’t control.

[
  %{
    "locations" => [%{"column" => 36, "line" => 2}],
    "message" =>
      "Unknown argument \"id\" on field \"silenceConversationNotifications\" of type \"RootMutationType\"."
  },
  %{
    "locations" => [%{"column" => 45, "line" => 2}],
    "message" =>
      "Argument \"input\" has invalid value $input.\nIn field \"input\": Expected type \"Conversation2InputInput!\", found null.\nIn field \"id\": Expected type \"String!\", found null.\nIn field \"silenceUntil\": Unknown field.\nIn field \"token\": Unknown field."
  }
]
    mutation SilenceConversationNotifications($id: ID!, $input: SilenceConversationNotificationsInput!) {
      silenceConversationNotifications(id: $id, input: $input) {
        result {
          id
        }
        errors {
          code
          fields
          message
          shortMessage
          vars
        }
      }
    }

GraphQL variables:

%{
  "id" => conversation.id,
  "input" => %{"silenceUntil" => silence_until, "token" => token}
  }

Here’s the action:

    action :silence_notifications, GF.Messaging.Types.SilenceNotificationsResult do
      argument :id, :string, allow_nil?: false
      argument :input, SilenceNotificationsInput, allow_nil?: false

Here’s the AshGraphql mutation:

    mutations do
      action :silence_conversation_notifications, :silence_notifications

The input embedded resource:

defmodule GF.Messaging.Resources.SilenceNotificationsInput do
  use Ash.Resource,
    data_layer: :embedded,
    extensions: [AshGraphql.Resource]

  graphql do
    type :silence_notifications_input
  end

  attributes do
    attribute :silence_until, :utc_datetime do
      allow_nil? false

      constraints precision: :second,
                  cast_dates_as: :start_of_day,
                  timezone: :utc

      public? true
    end

    attribute :token, :string do
      allow_nil? true
      constraints trim?: true, allow_empty?: false
      public? true
    end
  end
end

Any ideas?

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

:thinking: I think your id just needs to go inside the input. What does the generated GraphQL schema for that look like?

zachdaniel

zachdaniel

Creator of Ash

like input: %{id: .., input: ...}

moxley

moxley OP

But I want the id to be outside of the input, so that it looks and behaves like an update action. Is that possible?

How do I view the generated GraphQL schema?

moxley

moxley OP

Stepping back a bit, this is what I’m trying to accomplish:

I need a graphQL mutation that can take either a session user, or a special, single-purpose signed token to authenticate the user. The mutation should take an ID, and an input of map values. One of those map values is the token. I tried to do this with a regular update action, but the read action requires a session user, so that doesn’t work with the token authentication.

zachdaniel

zachdaniel

Creator of Ash

You could view it in playground or you could use mix absinthe.schema.sdl Your.Schema to see the full schema. Right now it does not look like we have a way for you to put those action inputs outside of the action, but it is not something that would be hard to add. We would likely call it something like input_location :input_object | :top_level.

For your specific case though you should be able to add identity false in the update mutation to not need the id input generated by default, and read_action :get_by_token_or_id that has arguments that you could use to locate the user.

moxley

moxley OP

Okay, then with identity false, and with the read_action :get_by_token_or_id, would the mutation still accept an ID argument?

zachdaniel

zachdaniel

Creator of Ash

It should have whatever the arguments are on the read action, so in the read action you could do something like:

argument :id, :string

prepare fn query, _ -> 
  id = 
    if is_token(query.arguments.token) do
      verify_token_and_get_id(query.arguments.token)
    else
      query.arguments.token 
    end

  Ash.Query.filter(query, id == ^id)
end
moxley

moxley OP

I’ll give that a shot. Thanks!

moxley

moxley OP

That gets us much closer. Here are the actions now:

    read :get_by_token_or_id do
      argument :id, :string, allow_nil?: false
      argument :token, :string
      argument :input, :map

      prepare fn query, _ ->
        dbg(query)
        query
      end
    end

    update :silence_notifications do
      require_atomic? false
      argument :token, :string
      argument :silence_until, :utc_datetime

      change fn changeset, context ->
        token = changeset.arguments.token
        dbg(token)

And here’s the debug output:

[lib/gf/messaging/conversation2.ex:54: GF.Messaging.Conversation2.preparation_0_generated_90FF210C1DDB60A3EB6FCCEDE0E8B59A/2]
query #=> #Ash.Query<
  resource: GF.Messaging.Conversation2,
  tenant: 662830,
  arguments: %{id: "IxLCkOBOkr4PnAZ"},
  filter: #Ash.Filter<id == "IxLCkOBOkr4PnAZ">,
  limit: 1
>

[lib/gf/messaging/conversation2.ex:68: GF.Messaging.Conversation2.change_0_generated_F9B8E45DFA6967330A15B9606FF0286F/2]
token #=> "SFMyNTY.c0VqUmthRVloWXRIbG1KOjIwMjUtMDUtMDRa.mTw3wS0IbepQ2C2u663g8SGylMsMBmBKrD-ZwBduV_8"

The variables passed to the GraphQL mutation are:

          variables: %{
            "id" => conversation.id,
            "input" => %{"silenceUntil" => silence_until, "token" => token}
          }

It looks like the read action doesn’t see any arguments other than :id. However, instead of relying on the read action to provide authorization gatekeeping, the update can do that, I think. Do you see any issues with that?

zachdaniel

zachdaniel

Creator of Ash

You can do it with filters on the update action, yes.

  update :silence_notifications do
      require_atomic? false
      argument :id, :string
      argument :token, :string
      argument :silence_until, :utc_datetime

      change fn changeset, context ->
        token = changeset.arguments.token
        dbg(token)
        Ash.Changeset.filter(changeset, ...)

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews