In addition to fetching data using queries and modifying data using mutations, the GraphQL spec also has a third operation type, called
GraphQL Subscriptions is a way to push data from the server to the clients that choose to listen to real time messages from the server.
Subscriptions are similar to queries in that they specify the selection set (list of fields) that the client is requesting, but instead of immediately returning a single answer, a result is sent every time a specified event happens on the server.
A common use-case for subscriptions is notifying the client side of changes based on events, for example the creation of a new object, updated fields and so on.
GraphQL subscriptions have to be defined in the schema:
A client can subscribe by sending a subscription query:
The response sent to the client looks as follows:
In the above example, the subscription is expected to send a new result every time a comment is added on GitHunt for a specific repository. Note that the code above only defines the GraphQL subscription in the schema. Read setting up subscriptions on the client and setting up GraphQL subscriptions for the server to learn how to add subscriptions to your app.
Subscriptions are a good alternative to polling when the initial state is large, but the incremental change sets are small. The starting state can be fetched with a query and subsequently updated through a subscription.
Setting up subscriptions on the client
To start using GraphQL subscriptions on the client with a Websocket transport, install
subscriptions-transport-ws from npm:
subscriptions-transport-wsis an transport implementation for subscriptions that works with any server or client, not only Apollo.
Read here on how to setup GraphQL subscriptions on your server.
Then, create a GraphQL subscriptions transport client (
Then, extend your existing Apollo-Client network interface using the
With GraphQL subscriptions your client will be alerted on push from the server and you should choose the pattern that fits your application the most:
- Use it as a notification and run any logic you want when it fires, for example alerting the user or refetching data
- Use the data sent along with the notification and merge it directly into the store (existing queries are automatically notified)
subscribeToMore, you can easily do the latter.
subscribeToMore is a function available on every query result in
react-apollo. It works just like
fetchMore, except that the update function gets called every time the subscription returns, instead of only once.
Here is a regular query:
Now, let’s add the subscription.
Add a function called
subscribeToNewComments that will subscribe using
subscribeToMore and update the query’s store with the new data using
and start the actual subscription by calling the
subscribeToNewComments function with the subscription variables:
Authentication over WebSocket
In many cases it is necessary to authenticate clients before allowing them to receive subscription results. To do this, the
SubscriptionClient constructor accepts a
connectionParams field, which passes a custom object that the server can use to validate the connection before setting up any subscriptions.
You can use
connectionParamsfor anything else you might need, not only authentication, and check its payload on the server side with SubscriptionsServer.