Core Subscriptions
Support for GraphQL Subscriptions in gqless.
Installation#
First, install @gqless/subscriptions into your project's dependencies:
- pnpm
- npm
- yarn
Usage#
If you activated subscriptions in the CLI Configuration you should have something like this in your generated client file:
It basically enables subscriptions only on client side, in case you are using SSR.
It's important to note that wsEndpoint can be a string, a function that returns a string or and an async function that returns a string.
setConnectionParams#
subscriptionsClient returns a helper in where you can set the connection parameters for the websocket connection, normally used for authorization purposes.
The second parameter is an optional boolean flag that automatically will reset the websocket connection if it's already connected.
Lazy behavior#
By default the subscriptions client uses a lazy behavior, which means that it will create the connection only when you actually make a subscription, and disconnect after a couple seconds on last unsubscribe.
More options#
| Name | Type | Default Value | Description |
|---|---|---|---|
| reconnect | boolean | true | Should the websocket connection try to reconnect |
| maxReconnectAttempts | boolean | Infinity | Amount of reconnection attempts |
| connectionCallback | () => void | undefined | Callback after successful connection |
| failedConnectionCallback | (payload: unknown) => Promise<void> | undefined | Callback after failed connection |
| failedReconnectCallback | () => void | undefined | Callback after last reconnection attempt failed |
| connectionInitPayload | (() => Promise<Record<string, unknown>> or Record<string, unknown> | {} | Connection init payload, or async function to get the init payload before connection, overridable with setConnectionParams |
| headers | Record<string,string> | undefined | Might not be actually supported by browsers, use connection params. Headers to be set on websocket connection |
| lazy | boolean | true | See Lazy behavior |