From 028b2f11eeaaa72f98ca18a9aa682ef213e2e08a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Etienne=20B=C3=B6mcke?= Date: Wed, 25 Oct 2023 18:13:16 +0200 Subject: [PATCH] test: allow disabling subscriptions through a session storage hidden value - set TEST_DISABLE_SUBSCRIPTIONS to true in sessionStorage to completely disable subscriptions --- src/services/graphql/apollo-client.ts | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/services/graphql/apollo-client.ts b/src/services/graphql/apollo-client.ts index 409f688d..d9e3e973 100644 --- a/src/services/graphql/apollo-client.ts +++ b/src/services/graphql/apollo-client.ts @@ -12,6 +12,7 @@ import { ErrorLink, onError } from '@apollo/client/link/error' import { getMainDefinition } from '@apollo/client/utilities' import { createClient as createWebsocketClient } from 'graphql-ws' import { GraphQLWsLink } from '@apollo/client/link/subscriptions' +import { isNil } from 'lodash' export const createClient = ({ httpUri, @@ -45,17 +46,26 @@ export const createClient = ({ return forward(operation) }) - const wsLink = - typeof window !== 'undefined' - ? new GraphQLWsLink( - createWebsocketClient({ - url: wsUri, - connectionParams: { - authToken: window.sessionStorage.getItem('accessToken'), - }, - }) - ) - : null + const createWsLink = () => { + if (isNil(window?.sessionStorage.getItem('accessToken'))) { + return null + } + if ( + window?.sessionStorage.getItem('TEST_DISABLE_SUBSCRIPTIONS') === 'true' + ) { + console.info('Disabling GraphQL subscriptions for testing purposes') + return null + } + return new GraphQLWsLink( + createWebsocketClient({ + url: wsUri, + connectionParams: { + authToken: window.sessionStorage.getItem('accessToken'), + }, + }) + ) + } + const wsLink = createWsLink() const isSubscription = ({ query }: { query: DocumentNode }) => { const definition = getMainDefinition(query)