scaphold-docs
scaphold-docs copied to clipboard
update subscription example code to use Apollo Link
Since apollo-client 2.0 NetworkInterface has been deprecated in favor of Apollo Link. It was quite confusing for me to make sense of the deprecated usage example. Look into updating the documentation source code. Here is the original documentation's example on how to do it:
import ApolloClient from 'apollo-client';
import { split } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
// Create an http link:
const httpLink = new HttpLink({
uri: 'http://localhost:3000/graphql'
});
// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: `ws://localhost:5000/`,
options: {
reconnect: true
}
});
// using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink,
);
const clientGraphql = new ApolloClient({
link,
initialState: {},
});