I have Phoenix channels working such that regular server status information is published at a regular interval.
I’m trying to reimplement this to push these updates through GraphQL subscriptions. Is there a good or correct way to emit these changes? As in, do I need to create a mutation doc to cause a trigger – or is there a function I can call directly?
Any advice or links would be appreciated,
Thanks,
Scott S.
easco
April 18, 2018, 9:30pm
2
There is a function you can call which triggers a subscription without having a mutation. It is ‘Absinthe.Subscription.publish’.
Read about it here:
# Understanding Subscriptions
GraphQL subscriptions are a way to have events in the server push data out to clients in real time. The client submits a subscription document that asks for particular data, and then when events happen that document is run against that event and the resulting data is pushed out.
Like queries and mutations, subscriptions are not intrinsically tied to any particular transport, and they're built within Absinthe itself to be able to operate on many different platforms.
At the moment however the most common and fully featured platform that you can run them on with Elixir is via Phoenix channels, so this guide will walk you through the basics of getting them hooked up to a phoenix application.
### Absinthe.Phoenix Setup
Packages you'll need:
```elixir
{:absinthe, "~> 1.4.0"},
{:absinthe_phoenix, "~> 1.4.0"},
```
You need to have a working Phoenix pubsub configured. Here is what the default looks like if you create a new Phoenix project:
```elixir
This file has been truncated. show original
At the Very bottom of the page.
I found this documentation to be unclear so in the example call:
Absinthe.Subscription.publish(MyAppWeb.Endpoint, comment, comment_added: “absinthe-graphql/absinthe”)
The “comment” is the new value for the thing that has changed. This would be the same thing you would return from a resolver.
“comment_added” is the field of a root subscription corresponding to the value above.
The string “absinthe-graphq/absinthe” is the topic you registered in the “config” function of the field in the subscription.
3 Likes