Docs
Launch GraphOS Studio

Subscriptions Link

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


We recommend reading

before learning about individual links.

The GraphQLWsLink is a

that's used most commonly with
subscriptions
(which usually communicate over WebSocket), although you can send queries and over WebSocket as well.

GraphQLWsLink requires the

library. Install it in your project like so:

npm install graphql-ws

Note: This link works with the newer graphql-ws library. If your server uses the older subscriptions-transport-ws, you should use the

instead.

Constructor

import { GraphQLWsLink } from "@apollo/client/link/subscriptions";
import { createClient } from "graphql-ws";
const link = new GraphQLWsLink(
createClient({
url: "ws://localhost:3000/subscriptions",
}),
);

Options

The GraphQLWsLink constructor takes a single , which is a Client returned from the graphql-ws createClient function.

The createClient function can take many options, described in the

. 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

.

Previous
Schema
Next
WebSocket (older protocol)
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company