Subscriptions Link
Execute subscriptions (or other operations) over WebSocket with the graphql-ws library
We recommend reading Apollo Link overview before learning about individual links.
The GraphQLWsLink
is a terminating link that's used most commonly with GraphQL subscriptions (which usually communicate over WebSocket), although you can send queries and mutations over WebSocket as well.
GraphQLWsLink
requires the graphql-ws
library. Install it in your project like so:
1npm install graphql-ws
Note: This link works with the newer
graphql-ws
library. If your server uses the oldersubscriptions-transport-ws
, you should use theWebSocketLink
link from@apollo/client/link/ws
instead.
Constructor
1import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
2import { createClient } from "graphql-ws";
3
4const link = new GraphQLWsLink(
5 createClient({
6 url: "ws://localhost:3000/subscriptions",
7 }),
8);
Options
The GraphQLWsLink
constructor takes a single argument, which is a Client
returned from the graphql-ws
createClient
function.
The createClient
function can take many options, described in the graphql-ws
docs for ClientOptions
. The one required option is url
, which is the URL (typically starting with ws://
or wss://
, which are the equivalents of http://
and https://
respectively) to your WebSocket server. (Note that this differs from the older link's URL option, which is named uri
instead of url
.)
Usage
See Subscriptions.