Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app): Disable unused graphql-subscriptions on the client #2904

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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