apollo-graphql-ws-link
Just the graphql-ws "Client with Apollo Recipe" as package.
Install
- run
npm install apollo-graphql-ws-link
- install peer dependencies
npm run install @apollo/client@^3.4.0 graphql@^15.5.0 graphql-ws@^5.3.0
Usage
- import WebSocketLink
import { WebSocketLink } from 'apollo-graphql-ws-link'
- create new WebSocketLink instance and pass graphql-ws client options
new WebSocketLink({ url: 'ws://localhost:3000/graphql' })
Example:
import { ApolloClient, split, createHttpLink } from '@apollo/client'
import { WebSocketLink } from 'apollo-graphql-ws-link'
const wsLink = new WebSocketLink({
url: `ws://localhost:3000/graphql`
})
const httpLink = createHttpLink({
uri: `http://localhost:3000/graphql`
})
export const client = new ApolloClient({
link: split(
({ query }) => {
const mainDefinition = getMainDefinition(query)
if (mainDefinition.kind === 'OperationDefinition') {
return mainDefinition.operation === 'subscription'
} else {
return false
}
},
wsLink,
httpLink
)
})