Skip to content

Commit

Permalink
fix: Warn about subscriptions-transport-ws clients and provide migrat…
Browse files Browse the repository at this point in the history
…ion link

Closes enisdenjo#339
Related enisdenjo#325
  • Loading branch information
enisdenjo committed Mar 24, 2022
1 parent db0ccf5 commit e080739
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import {
*/
export const GRAPHQL_TRANSPORT_WS_PROTOCOL = 'graphql-transport-ws';

/**
* The deprecated subprotocol used by [subscriptions-transport-ws](https://github.com/apollographql/subscriptions-transport-ws).
*
* @private
*/
export const DEPRECATED_GRAPHQL_WS_PROTOCOL = 'graphql-ws';

/**
* `graphql-ws` expected and standard close codes of the [GraphQL over WebSocket Protocol](/PROTOCOL.md).
*
Expand Down
14 changes: 8 additions & 6 deletions src/use/fastify-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { FastifyRequest } from 'fastify';
import type * as fastifyWebsocket from 'fastify-websocket';
import { handleProtocols, makeServer, ServerOptions } from '../server';
import {
GRAPHQL_TRANSPORT_WS_PROTOCOL,
DEPRECATED_GRAPHQL_WS_PROTOCOL,
ConnectionInitMessage,
CloseCode,
} from '../common';
Expand Down Expand Up @@ -172,12 +172,14 @@ export function makeHandler<
socket.once('close', (code, reason) => {
if (pongWait) clearTimeout(pongWait);
if (pingInterval) clearInterval(pingInterval);
if (!isProd && code === CloseCode.SubprotocolNotAcceptable)
if (
!isProd &&
code === CloseCode.SubprotocolNotAcceptable &&
socket.protocol === DEPRECATED_GRAPHQL_WS_PROTOCOL
)
console.warn(
`WebSocket protocol error occured. It was most likely caused due to an ` +
`unsupported subprotocol "${socket.protocol}" requested by the client. ` +
`graphql-ws implements exclusively the "${GRAPHQL_TRANSPORT_WS_PROTOCOL}" subprotocol, ` +
'please make sure that the client implements it too.',
`Client provided the unsupported and deprecated subprotocol "${socket.protocol}" used by subscriptions-transport-ws.` +
'Please see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws.',
);
closed(code, String(reason));
});
Expand Down
14 changes: 8 additions & 6 deletions src/use/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as http from 'http';
import type * as ws from 'ws';
import { handleProtocols, makeServer, ServerOptions } from '../server';
import {
GRAPHQL_TRANSPORT_WS_PROTOCOL,
DEPRECATED_GRAPHQL_WS_PROTOCOL,
ConnectionInitMessage,
CloseCode,
Disposable,
Expand Down Expand Up @@ -157,12 +157,14 @@ export function useServer<
socket.once('close', (code, reason) => {
if (pongWait) clearTimeout(pongWait);
if (pingInterval) clearInterval(pingInterval);
if (!isProd && code === CloseCode.SubprotocolNotAcceptable)
if (
!isProd &&
code === CloseCode.SubprotocolNotAcceptable &&
socket.protocol === DEPRECATED_GRAPHQL_WS_PROTOCOL
)
console.warn(
`WebSocket protocol error occured. It was most likely caused due to an ` +
`unsupported subprotocol "${socket.protocol}" requested by the client. ` +
`graphql-ws implements exclusively the "${GRAPHQL_TRANSPORT_WS_PROTOCOL}" subprotocol, ` +
'please make sure that the client implements it too.',
`Client provided the unsupported and deprecated subprotocol "${socket.protocol}" used by subscriptions-transport-ws.` +
'Please see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws.',
);
closed(code, String(reason));
});
Expand Down

0 comments on commit e080739

Please sign in to comment.