mukesh

mukesh

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.

Marked As Solved

mukesh

mukesh

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.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Correct, those are your two options.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement