Skip to content

Instantly share code, notes, and snippets.

@bogoslavskiy
Created March 5, 2020 17:39
Show Gist options
  • Save bogoslavskiy/c1e74d86d96d7bdc254dc13fc919939e to your computer and use it in GitHub Desktop.
Save bogoslavskiy/c1e74d86d96d7bdc254dc13fc919939e to your computer and use it in GitHub Desktop.
import { ApolloClient, split, HttpLink, InMemoryCache } from '@apollo/client';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/link-ws';
import Introspection from './introspection-result.json';
const wsLink = new WebSocketLink({
uri: `ws://localhost:5000/graphql`,
options: { reconnect: true }
});
const httpLink = new HttpLink({
uri: 'http://localhost:5000/graphql'
});
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
export const client = new ApolloClient({
cache: new InMemoryCache({ ...Introspection }),
link: splitLink
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment