Skip to content

Instantly share code, notes, and snippets.

@homerquan
Created March 27, 2019 00:46
Show Gist options
  • Save homerquan/4237d861c80d1c8afd7147bdfa87ef0d to your computer and use it in GitHub Desktop.
Save homerquan/4237d861c80d1c8afd7147bdfa87ef0d to your computer and use it in GitHub Desktop.
apollo client with graphql-subscription 1.x to 2.x
const uri = 'https://api.graph.cool/simple/v1/cj7yuh1df1tnt0147ihz25bgi'
const wsClient = {
uri: 'wss://subscriptions.graph.cool/v1/cj7yuh1df1tnt0147ihz25bgi',
options: {
reconnect: true,
connectionParams: {
authToken: localStorage.getItem(GC_AUTH_TOKEN),
}
}
}
const link = ApolloLink.split(
operation => { // check if it is a subscription
const operationAST = getOperationAST(operation.query, operation.operationName);
return !!operationAST && operationAST.operation === 'subscription';
},
new WebSocketLink(wsClient),
new HttpLink({ uri }),
);
const client = new ApolloClient({
networkInterface: link
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment