graphql-ws
Advanced tools
Changelog
4.0.0 (2021-01-13)
onDisconnect
callback (#94) (2a61268)server.opened
(closed
) now requires the close event code and reason for reporting to the onDisconnect
callback.Context.subscriptions
record value can be either an AsyncIterator
or a Promise
.Changelog
3.0.0 (2020-12-09)
retryTimeout
option has been replaced with the new retryWait
.retryWait
allows you to control the retry timeout strategy by resolving the returned promise when ready. The default implements the randomised exponential backoff like so:
// this is the default
const retryWait = async function randomisedExponentialBackoff(retries: number) {
let retryDelay = 1000; // start with 1s delay
for (let i = 0; i < retries; i++) {
retryDelay *= 2; // square `retries` times
}
await new Promise((resolve) =>
setTimeout(
// resolve pending promise with added random timeout from 300ms to 3s
resolve,
retryDelay + Math.floor(Math.random() * (3000 - 300) + 300),
),
);
};
Changelog
2.0.0 (2020-11-20)
Summary of breaking changes:
keepAlive
. The user should provide its own keep-alive implementation. (I highly recommend WebSocket Ping and Pongs)request
in the server context.makeServer
(no more createServer
)extra
field in the Context
for storing custom values useful for callbacksOnly the server has to be migrated. Since this release allows you to use your favourite WebSocket library (or your own implementation), using ws is just one way of using graphql-ws
. This is how to use the implementation shipped with the lib:
/**
* ❌ instead of the lib creating a WebSocket server internally with the provided arguments
*/
import https from 'https';
import { createServer } from 'graphql-ws';
const server = https.createServer(...);
createServer(
{
onConnect(ctx) {
// were previously directly on the context
ctx.request as IncomingRequest
ctx.socket as WebSocket
},
...rest,
},
{
server,
path: '/graphql',
},
);
/**
* ✅ you have to supply the server yourself
*/
import https from 'https';
import ws from 'ws'; // yarn add ws
import { useServer } from 'graphql-ws/lib/use/ws'; // notice the import path
const server = https.createServer(...);
const wsServer = new ws.Server({
server,
path: '/graphql',
});
useServer(
{
onConnect(ctx) {
// are now in the `extra` field
ctx.extra.request as IncomingRequest
ctx.extra.socket as WebSocket
},
...rest,
},
wsServer,
// optional keepAlive with ping pongs (defaults to 12 seconds)
);