Subscription
When we had initially set up Apollo, we used Apollo Boost to install the required dependencies. But subscriptions is an advanced use case which Apollo Boost does not support. So we have to install more dependencies to set up subscriptions.
React Native Apollo Subscriptions Setup
yarn add apollo-link-ws subscriptions-transport-ws
Now we need to update our ApolloClient
instance to point to the subscription server.
Open src/apollo.js
and update the following imports:
- import { HttpLink } from 'apollo-link-http';+ import { WebSocketLink } from 'apollo-link-ws';
Update the makeApolloClient
function to integrate WebSocketLink.
// create an apollo link instance, a network interface for apollo client- const link = new HttpLink({- uri: `https://hasura.io/learn/graphql`,- headers: {- Authorization: `Bearer ${token}`- }- });+ const link = new WebSocketLink({+ uri: `wss://hasura.io/learn/graphql`,+ options: {+ reconnect: true,+ connectionParams: {+ headers: {+ Authorization: `Bearer ${token}`+ }+ }+ }+ })
Note that we are replacing HttpLink with WebSocketLink and hence all GraphQL queries go through a single websocket connection.
Did you find this page helpful?
Start with GraphQL on Hasura for Free
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs