absinthe icon indicating copy to clipboard operation
absinthe copied to clipboard

Emit item on subscription

Open guitcastro opened this issue 3 years ago • 2 comments

It's possible always call the resolve api after client has subscribed?

per example:


subscription do
    field :educational_element, :educational_element do
      arg :root_id, non_null(:string)

      config fn args, _ ->
        {:ok, topic: args.root_id}
      end

      resolve fn %{root_id: id}, _ ->
        {:ok, Repo.get(id)}
      end

      trigger :update_or_create_educational_element, topic: fn educational_element ->
        educational_element.id
      end
    end
  end

If use subscribe to the topic, no data will show until some changes happens. I know that graphql does not determinate a default behavior for subscription, however it's a very common scenario to emit an item on subscription.

It's possible to configure absinthe to do it?

guitcastro avatar Sep 20 '21 00:09 guitcastro

A workaround to this is

config fn args, _ ->
  topic = "some_topic"
  
  spawn(fn -> emit_item(some_topic) end)
  
  {:ok, topic: topic}
end

But this creates a possibility for a race-condition It would be nice to have in the config macro the possibility to just have a callback after the topic is registered like:

config fn args, _ ->
  topic = "some_topic"
  
  {:ok, topic: topic, after_register: fn registered_topic -> :anything end}
end

eliasdarruda avatar Jun 02 '22 18:06 eliasdarruda

But this creates a possibility for a race-condition

Yeah this will almost assuredly race because the end client hasn't even subscribed to the actual pubsub topic over which they'll get data at this point.

It's that second subscription that is the reason this is tricky. We can't fire off anything at the time that the subscription is submitted, all we can do is register the doc and then return the pubsub topic for the doc. The client then needs to subscribe to that topic. It's at that point that we could fire something off, but client side subscriptions to Phoenix PubSub don't provide Absinthe a mechanism for hooking in. So really what we need is an entirely out of band way to just trigger a subscription.

benwilson512 avatar Jun 03 '22 17:06 benwilson512