From aa9b8294312c92340e16ef81257f9bc4fb87c8d1 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 17 Jun 2021 22:38:26 +0100 Subject: [PATCH] Update apollo config --- src/index.tsx | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index 822fa77..1f74535 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -5,7 +5,10 @@ import { InMemoryCache, createHttpLink, ApolloProvider, + split, } from '@apollo/client'; +import { WebSocketLink } from '@apollo/client/link/ws'; +import { getMainDefinition } from '@apollo/client/utilities'; import { Elements } from '@stripe/react-stripe-js'; import { loadStripe } from '@stripe/stripe-js'; import { setContext } from '@apollo/client/link/context'; @@ -26,6 +29,25 @@ const httpLinkSupport = createHttpLink({ uri: process.env.REACT_APP_GRAPHQL_SUPPORT_API, }); +const wsLink = new WebSocketLink({ + uri: `${process.env.REACT_APP_GRAPHQL_SUPPORT_SUBSCRIPTIONS_API}`, + options: { + reconnect: true, + }, +}); + +const splitLink = split( + ({ query }) => { + const definition = getMainDefinition(query); + return ( + definition.kind === 'OperationDefinition' && + definition.operation === 'subscription' + ); + }, + wsLink, + httpLinkSupport +); + const authLink = setContext((_, { headers }) => { const token = localStorage.getItem('token'); @@ -43,7 +65,7 @@ export const clientMain = new ApolloClient({ }); export const clientSupport = new ApolloClient({ - link: authLink.concat(httpLinkSupport), + link: authLink.concat(splitLink), cache: new InMemoryCache(), });