absinthe_phoenix icon indicating copy to clipboard operation
absinthe_phoenix copied to clipboard

Controller-based mutations don't "trigger" subscriptions

Open peaceful-james opened this issue 4 years ago • 1 comments

I can expand on this as much as you want so I'll just put down the basic info for now.

If I have a mutation in a phoenix controller (in a @graphql module attribute), like this:

  @graphql """
  mutation UpdateMenuItem($id: ID!, $description: String!) {
  update_menu_item(id: $id, input: {description: $description}) {
    errors { key message }
    menu_item {
      name
      description
    }
    }
  }
  """
  def update(conn, %{data: %{update_menu_item: %{errors: nil, menu_item: menu_item}}}) do
    # ... controller update logic here, conn |> redirect or whatever
  end

And if this mutation is meant to trigger a subscription, for example if my schema.ex is like this:

subscription do
    field :updated_menu_item, :menu_item do
      config(fn _args, _info ->
        {:ok, topic: "*"}
      end)

      trigger(:update_menu_item,
        topic: fn
          %{menu_item: menu_item} -> ["*"]
          _ -> []
        end
      )

      resolve(fn %{menu_item: menu_item}, _, _ ->
        {:ok, menu_item}
      end)
    end

# more subscriptions,etc.
end

then the subscription is not triggered. This might have something to do with how the Absinthe.Phoenix SDL is processed differently to normal Graphql SDL.

peaceful-james avatar Jul 12 '20 01:07 peaceful-james

I have forked this repo and made a very naive/messy attempt to prove the problem with a test. https://github.com/peaceful-james/absinthe_phoenix/blob/6a4ff3c7aa132acdc85a4933eda805a5ebefce2b/test/absinthe/phoenix/controller_test.exs#L163 Maybe somebody who knows what they're doing can give me some pointers? I would love to understand why these controller-sourced mutations don't trigger subscriptions.

peaceful-james avatar Aug 28 '20 17:08 peaceful-james