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.
+ $ npm install apollo-link-ws subscriptions-transport-ws --save
Now we need to update our
ApolloClient instance to point to the subscription server.
src/ApolloClient.re and update the
link as follows:
+let connectionParams = Json.Encode.object_([("headers", headers)]);-let link = ApolloLinks.createHttpLink(- ~uri="https://hasura.io/learn/graphql",- ~headers=headers,- ()-);++let link = ApolloLinks.webSocketLink(+ ~uri="wss://hasura.io/learn/graphql",+ ~reconnect=true,+ ~connectionParams=connectionParams,+ ()+)
Note that we are replacing HttpLink with WebSocketLink and hence all GraphQL queries go through a single websocket connection. Look at how we are passing the headers. While initializing websocket link, we need to pass headers in the
headers key in the
connectionParams accepted by
- Build apps and APIs 10x faster
- Built-in authorization and caching
- 8x more performant than hand-rolled APIs