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 11 to 2

moxley

moxley OP

Great, thanks for the help!

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, ...)

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?

moxley

moxley OP

I’ll give that a shot. Thanks!

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

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

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

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.

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?

zachdaniel

zachdaniel

Creator of Ash

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

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews