Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
connectionParams: {
token: localStorage.getItem('token') || undefined,
refreshToken: localStorage.getItem('refreshToken') || undefined,
clientId: localStorage.getItem('clientId') || undefined
},
connectionCallback: () => {
if (connectionAttempts > 0) {
console.log('Reconnect successful! Start resetting apollo store');
if (client) client.resetStore();
}
connectionAttempts += 1;
}
}
});
const link = ApolloLink.split(
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
wsLink,
httpLink
);
const errorLink = ErrorLink.onError(error => {
console.error(error);
if (!config.onError) return;
if (error.networkError) {
const { message } = error.networkError;
if (/Failed to fetch/.test(message)) {
config.onError(message, 'failed_to_fetch');
}
}));
return next ? next(operation) : null;
});
const httpLink = ApolloLink.from([authLink, batchLink]);
const subscriptionClient = new SubscriptionClient(`${isSecureProtocol() ? 'wss' : 'ws'}://${host}/graphql`, {
reconnect: true,
connectionParams: () => {
const token = localStorage.getItem('token');
return { Authorization: token };
},
});
const wsLink = new WebSocketLink(subscriptionClient);
const link = ApolloLink.split(
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
wsLink,
httpLink,
);
(window as any).subscription = subscriptionClient;
return {
healthEndpoint,
apollo: new ApolloClient({ link, cache: new InMemoryCache() }),
subscription: subscriptionClient,
};
}