The GraphQL operation to resolve.
The matching SharedClient, or undefined if no client applies.
import { ApolloClient, ApolloLink, InMemoryCache } from '@apollo/client'
import { HttpLink } from '@apollo/client/link/http'
import { GraphQLWsLink } from '@apollo/client/link/subscriptions'
import { OperationTypeNode } from 'graphql'
import { createSharedClient } from 'graphql-shared-ws'
import { setupRestartSubscription } from 'apollo-shared-ws'
// GraphQL API endpoint served over wss.
const apiSharedClient = createSharedClient({ url: 'wss://api.example.com/graphql' })
// GraphQL performance reporting endpoint served over wss.
const perfSharedClient = createSharedClient({ url: 'wss://perf.example.com/graphql' })
// Return the SharedClient associated with the GraphQLWsLink that will handle
// the operation.
const clientResolver: ClientResolver = (operation) => {
if (operation.operationType !== OperationTypeNode.SUBSCRIPTION) return undefined
if (operation.operationName?.startsWith('api')) {
return apiSharedClient
}
return perfSharedClient
}
const apiLink = new GraphQLWsLink(apiSharedClient)
const perfLink = new GraphQLWsLink(perfSharedClient)
const wsLink = ApolloLink.split(
({ operationName }) => Boolean(operationName?.startsWith('api')),
apiLink,
perfLink
)
const httpLink = new HttpLink({ uri: 'https://example.com/graphql' })
const link = ApolloLink.split(
({ operationType }) => Boolean(operationType === OperationTypeNode.SUBSCRIPTION),
wsLink,
httpLink
)
const client = new ApolloClient({
link,
cache: new InMemoryCache(),
})
setupRestartSubscription(client, {
sharedClientResolver: clientResolver,
})
Resolves the SharedClient to use for a GraphQL operation.
This is primarily intended for use with ApolloLink.split. The resolver should return the SharedClient associated with the GraphQLWsLink selected for the given operation.