Absinthe subscription trigger doesn't work for namespaced mutation

Hello lovely people,

I did a tremendous research but couldn’t figure out why my mutation is not recognized by the subscription > trigger/2 macro.

I have the following in my types

defmodule MyAppWeb.Graphql.Discussions.Types do
  ...
  # helper to namespace queries and mutations:
  defp resolver_self(parent, _res, _context), do: {:ok, parent}
  
  # Queries and mutations for Discussions and DisccusionComments are namespaced under the field
  # `discussions`:
  object :discussions_namespace_mutations do
    field(:discussions, :discussions_mutations) do
      resolve(&resolver_self/3)
    end
  end

  #
  # Mutations
  #
  object :discussions_mutations do
    field :create_comment, :discussion_comment do
      arg(:discussion_thread_id, non_null(:uuid4))
      arg(:comment, non_null(:string))

      resolve(&Resolver.create_comment/2)
    end
  end

  #
  # Subscriptions
  #
  object :discussions_subscription do
    field :discussion_thread, list_of(:discussion_comment) do
      arg :id, non_null(:uuid4)

      # set subscription topic(s)
      config fn args, _info ->
        {:ok, topic: args.id}
      end

      **# This is where the issue is -- the mutation is not recognized as it is namespaced above**
      trigger :create_comment, topic: fn
        discussion_comment -> discussion_comment.discussion_thread_id
      end


      resolve fn discussion_comment, _, _ ->
        {:ok, discussion_comment}
      end
    end
  end

end

I didn’t find a way to trigger my namespaced mutation from the [Absinthe.Schema.Notation.trigger/2](https://hexdocs.pm/absinthe/Absinthe.Schema.Notation.html#trigger/2) macro.

However, if I don’t use the trigger/2 macro & just run [Absinthe.Subscription.publish/3](https://hexdocs.pm/absinthe/Absinthe.Subscription.html#publish/3) in the resolver Resolver.create_comment/2 - everything works as expected. But I want to figure out how to make the trigger/2 macro work in this scenario.

Any help is highly appreciated. Thank you all.

Hi @mukesh, you aren’t showing your actual mutation object so there are some details I’m not really following. Are you doing something like:

mutation {
  discussions { createComment() }
}

If so, this isn’t really spec compliant. Mutations are all supposed to be fields directly within the root mutation object mutation do.

Hey @benwilson512 ,

Yes, my mutation looks something like that.

Here is what I am using in my test

@mutation """
  mutation CreateDiscussionCommentMutation(
    $discussionThreadId: UUID4!,
    $comment: String!
  ) {
    discussions {
      createComment(
        discussionThreadId: $discussionThreadId,
        comment: $comment,
      ) {
          id
          comment
          authorId
          discussionThreadId
          status
        }
      }
    }
  """

So, @benwilson512 , I think I have only 2 options

  1. Make my mutation to be a field directly within the root mutation - meaning it cannot be namespaced under discussions anymore OR
  2. Keep my mutation namespaced, not use trigger/2 macro & use Absinthe.Subscription.publish/3 directly in the resolver.

Isn’t it?
Or is there any other way to reference my namespaced mutation within trigger macro?

P.S.- Thanks for the swift reply.

Correct, those are your two options.

1 Like