Skip to content

Commit

Permalink
Merge pull request #2904 from OpenNeuroOrg/websocket-link-disable
Browse files Browse the repository at this point in the history
fix(app): Disable unused graphql-subscriptions on the client
  • Loading branch information
nellh authored Sep 25, 2023
2 parents 492aac1 + feb2bc7 commit 4edbb03
Showing 1 changed file with 2 additions and 40 deletions.
42 changes: 2 additions & 40 deletions packages/openneuro-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import {
ApolloClient,
InMemoryCache,
ApolloLink,
split,
Observable,
createHttpLink,
} from '@apollo/client'
import { setContext } from '@apollo/client/link/context'
import { WebSocketLink } from '@apollo/client/link/ws'
import { getMainDefinition } from '@apollo/client/utilities'
import semver from 'semver'

const authLink = getAuthorization =>
Expand All @@ -27,18 +24,6 @@ const authLink = getAuthorization =>
}
})

const wsLink = uri => {
const root = uri.replace('http', 'ws').replace('/crn', '')
const subscriptions = '-subscriptions'
const link = root + subscriptions
return new WebSocketLink({
uri: link,
options: {
reconnect: true,
},
})
}

const hbar = '\n-----------------------------------------------------\n'
const parse = version => [semver.major(version), semver.minor(version)]
const checkVersions = (serverVersion, clientVersion) => {
Expand Down Expand Up @@ -94,28 +79,6 @@ const compareVersionsLink = clientVersion =>
),
)

const createLink = (uri, getAuthorization, fetch, enableWebsocket) => {
return split(
({ query }) => {
/**
* Typescript complains because this can return
* FragmentDefinitionNode or OperationDefinitionNode
* so we cannot use a simple destructuring
* `const { kind, operation }`
**/
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
enableWebsocket
? wsLink(uri)
: middlewareAuthLink(uri, getAuthorization, fetch),
middlewareAuthLink(uri, getAuthorization, fetch),
)
}

/**
* Setup a client for working with the OpenNeuro API
*/
Expand All @@ -128,14 +91,13 @@ export const createClient = (
links = [],
ssrMode = false,
cache = undefined,
enableWebsocket = false,
} = {},
) => {
// createLink must be last since it contains a terminating link
// middlewareAuthLink must be last since it contains a terminating link
const composedLink = ApolloLink.from([
compareVersionsLink(clientVersion),
...links,
createLink(uri, getAuthorization, fetch, enableWebsocket),
middlewareAuthLink(uri, getAuthorization, fetch),
])

const apolloClientOptions = {
Expand Down

0 comments on commit 4edbb03

Please sign in to comment.