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

Emit item on subscription #1103

Open
guitcastro opened this issue Sep 20, 2021 · 2 comments
Open

Emit item on subscription #1103

guitcastro opened this issue Sep 20, 2021 · 2 comments

Comments

@guitcastro
Copy link

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?

@eliasdarruda
Copy link

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

@benwilson512
Copy link
Contributor

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.

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

3 participants