Moving from apollo-boost to GraphQL subscriptions with apollo-client

import { ApolloProvider } from "react-apollo";
import ApolloClient from "apollo-boost";
const client = new ApolloClient({
uri: "https://w5xlvm3vzz.lp.gql.zone/graphql"
});
ReactDOM.render(
(<ApolloProvider client={client}>
<App />
</ApolloProvider>),
document.getElementById('root'));import { ApolloProvider } from "react-apollo";
// Remove the apollo-boost import and change to this:
import ApolloClient from "apollo-client";
// Setup the network "links"
import { WebSocketLink } from 'apollo-link-ws';
import { HttpLink } from 'apollo-link-http';
import { split } from 'apollo-link';
import { getMainDefinition } from 'apollo-utilities';
import { InMemoryCache } from 'apollo-cache-inmemory';
const wsurl = wss://hasura-demo.herokuapp.com/v1alpha1/graphql;
const httpurl = https://hasura-demo.herokuapp.com/v1alpha1/graphql;
const wsLink = new WebSocketLink({
uri: wsurl,
options: {
reconnect: true
}
});
const httpLink = new HttpLink({
uri: httpurl,
});
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
)
const client = new ApolloClient({
link,
cache: new InMemoryCache()
});
// Use the client just as before
ReactDOM.render(
(<ApolloProvider client={client}>
<App />
</ApolloProvider>),
document.getElementById('root'));- Create a
wsLink - Create an
httpLink - Create a new apollo-link which delegates all GraphQL queries to
httpLinkunless they aresubscriptionqueries - Create a
ApolloClientwhich uses the link object we created above - Enable
InMemoryCachebecause that’s what we had withapollo-boost
npm install --save apollo-client apollo-link-ws apollo-link-http apollo-link apollo-utilities apollo-cache-inmemoryRelated reading


