Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing information between triggering mutation and subscriptions #1344

Open
martosaur opened this issue Oct 23, 2024 · 0 comments
Open

Passing information between triggering mutation and subscriptions #1344

martosaur opened this issue Oct 23, 2024 · 0 comments

Comments

@martosaur
Copy link
Contributor

Suppose we want to create a subscription that would simply publish names of all mutations that were called:

# test/absinthe/execution/subscription_test.exs
  defmodule Schema do
    use Absinthe.Schema

    object :user do
      field :id, :id
      field :name, :string
    end

    subscription do    
      field :all_mutations, :string do
        config fn _, _ -> {:ok, topic: "*"} end
        trigger [:update_user], topic: fn _ -> "*" end
        # None of the input arguments can carry `updateUser` string
        resolve fn _mutation_result, _args, _resolution -> {:ok, "updateUser"} end
      end
    end

    mutation do
      field :update_user, :user do
        arg :id, non_null(:id)

        resolve fn _, %{id: id}, _ ->
          {:ok, %{id: id, name: "foo"}}
        end
      end
    end
  end

  @query """
  subscription {
    allMutations 
  }
  """
  test "can pass context from mutation to subscription" do
    id = "1"
    
    assert {:ok, %{"subscribed" => topic}} = run_subscription(@query, Schema, context: %{pubsub: PubSub})
    
    mutation = """
    mutation ($userId: ID!) {
      updateUser(id: $userId) { id name }
    }
    """
    
    assert {:ok, %{data: _}} =
             run_subscription(mutation, Schema,
               variables: %{"userId" => id},
               context: %{pubsub: PubSub}
             )

    assert_receive({:broadcast, msg})

    assert %{
             event: "subscription:data",
             result: %{data: %{"allMutations" => "updateUser"}},
             topic: topic
           } == msg
  end

If I'm reading the code right, it's not possible with just trigger macro right now. The only piece of information that is passed to a subscription's resolver from a mutation is the mutation's result.

Would it make sense to provide an avenue for passing some arbitrary context between two resolutions? I'm happy to take a stab at implementing this, but not quite sure what would be the best place to put this extra context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant