Skip to content

Commit

Permalink
test: allow disabling subscriptions through a session storage hidden …
Browse files Browse the repository at this point in the history
…value

- set TEST_DISABLE_SUBSCRIPTIONS to true in sessionStorage to completely
disable subscriptions
  • Loading branch information
ebomcke-awell committed Oct 25, 2023
1 parent 48af052 commit 028b2f1
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/services/graphql/apollo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 028b2f1

Please sign in to comment.