GraphQLWsLink

Execute subscriptions (or other operations) over WebSocket with the graphql-ws library


The GraphQLWsLink is a terminating link sends GraphQL operations over a WebSocket connection using the graphql-ws library. It's used most commonly with GraphQL subscriptions,

note
This link works with the graphql-ws library. If your server uses the deprecated subscriptions-transport-ws library, use the deprecated WebSocketLink link instead.

GraphQLWsLink requires the graphql-ws library. Install it in your project like so:

shell
1npm install graphql-ws
TypeScript
1 import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
2 import { createClient } from "graphql-ws";
3
4 const link = new GraphQLWsLink(
5   createClient({
6     url: "ws://localhost:3000/subscriptions",
7   })
8 );

Constructor signature

JavaScript
1constructor(
2  client: Client
3): GraphQLWsLink

Options

The GraphQLWsLink constructor takes a single argument: a Client instance from the graphql-ws library. To create this instance, call the library's createClient function. This function requires a url option, which is the URL to your WebSocket server. WebSocket URLs typically start with ws:// or wss://.

See the ClientOptions documentation for more details on the supported options provided to the createClient function.

Retrying failed connections

See the graphql-ws recipes for strategies on retrying failed connections from the client. We generally recommend this approach over retrying failed connections from the link chain or your components because it provides more detailed information on why the connection failed.

Alternatively, you can handle retries more generically within the link chain by using RetryLink. This link resends the operation to the terminating link upon failure.

Usage

See Subscriptions for more information on using subscription operations in Apollo Client.

Feedback

Edit on GitHub

Ask Community