From dc23be36e8f55e23486ec29435034400043b72c7 Mon Sep 17 00:00:00 2001 From: brainrepo Date: Wed, 6 Oct 2021 09:59:17 +0200 Subject: [PATCH] feat: switch default subscription protocol to graphql-transport-ws --- lib/subscription-client.js | 4 +-- lib/subscription-protocol.js | 6 +++-- test/subscription-client.js | 14 +++++----- test/subscription-connection.js | 48 ++++++++++++++++----------------- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/lib/subscription-client.js b/lib/subscription-client.js index f588df18..c78e87c8 100644 --- a/lib/subscription-client.js +++ b/lib/subscription-client.js @@ -4,7 +4,7 @@ const sJSON = require('secure-json-parse') const WebSocket = require('ws') const { - GRAPHQL_WS, + GRAPHQL_TRANSPORT_WS, getProtocolByName } = require('./subscription-protocol') const { MER_ERR_GQL_SUBSCRIPTION_MESSAGE_INVALID, MER_ERR_GQL_SUBSCRIPTION_CONNECTION_NOT_READY, MER_ERR_INVALID_OPTS } = require('./errors') @@ -43,7 +43,7 @@ class SubscriptionClient { if (Array.isArray(protocols) && protocols.length > 0) { this.protocols = protocols } else { - this.protocols = [GRAPHQL_WS] + this.protocols = [GRAPHQL_TRANSPORT_WS] } this.protocolMessageTypes = getProtocolByName(this.protocols[0]) diff --git a/lib/subscription-protocol.js b/lib/subscription-protocol.js index a480555a..3f45f14d 100644 --- a/lib/subscription-protocol.js +++ b/lib/subscription-protocol.js @@ -1,11 +1,13 @@ 'use strict' const GRAPHQL_WS = 'graphql-ws' -module.exports.GRAPHQL_WS = GRAPHQL_WS +const GRAPHQL_TRANSPORT_WS = 'graphql-transport-ws' + +module.exports.GRAPHQL_TRANSPORT_WS = GRAPHQL_TRANSPORT_WS module.exports.getProtocolByName = function (name) { switch (true) { - case (name.indexOf('graphql-transport-ws') !== -1): + case (name.indexOf(GRAPHQL_TRANSPORT_WS) !== -1): return { GQL_CONNECTION_INIT: 'connection_init', // Client -> Server GQL_CONNECTION_ACK: 'connection_ack', // Server -> Client diff --git a/test/subscription-client.js b/test/subscription-client.js index 107d2edc..0ad480fa 100644 --- a/test/subscription-client.js +++ b/test/subscription-client.js @@ -59,8 +59,8 @@ test('subscription client calls the publish method with the correct payload', (t const data = JSON.parse(isBinary ? message : message.toString()) if (data.type === 'connection_init') { ws.send(JSON.stringify({ id: undefined, type: 'connection_ack' })) - } else if (data.type === 'start') { - ws.send(JSON.stringify({ id: '1', type: 'data', payload: { data: { foo: 'bar' } } })) + } else if (data.type === 'subscribe') { + ws.send(JSON.stringify({ id: '1', type: 'next', payload: { data: { foo: 'bar' } } })) } }) }) @@ -94,7 +94,7 @@ test('subscription client calls the publish method with null after GQL_COMPLETE const data = JSON.parse(isBinary ? message : message.toString()) if (data.type === 'connection_init') { ws.send(JSON.stringify({ id: undefined, type: 'connection_ack' })) - } else if (data.type === 'start') { + } else if (data.type === 'subscribe') { ws.send(JSON.stringify({ id: '1', type: 'complete' })) } }) @@ -155,7 +155,7 @@ test('subscription client tries to reconnect when server closes', (t) => { const data = JSON.parse(isBinary ? message : message.toString()) if (data.type === 'connection_init') { ws.send(JSON.stringify({ id: undefined, type: 'connection_ack' })) - } else if (data.type === 'start') { + } else if (data.type === 'subscribe') { ws.send(JSON.stringify({ id: '1', type: 'complete' })) } }) @@ -201,7 +201,7 @@ test('subscription client multiple subscriptions is handled by one operation', t const data = JSON.parse(isBinary ? message : message.toString()) if (data.type === 'connection_init') { ws.send(JSON.stringify({ id: undefined, type: 'connection_ack' })) - } else if (data.type === 'start') { + } else if (data.type === 'subscribe') { ws.send(JSON.stringify({ id: '1', type: 'complete' })) } }) @@ -378,7 +378,7 @@ test('subscription client not throwing error on GQL_CONNECTION_KEEP_ALIVE type p server.on('connection', function connection (ws) { ws.on('message', function incoming (message, isBinary) { const data = JSON.parse(isBinary ? message : message.toString()) - if (data.type === 'start') { + if (data.type === 'subscribe') { ws.send(JSON.stringify({ id: '1', type: 'complete' })) } }) @@ -473,7 +473,7 @@ test('subscription client does not send message if operation is already started' let sent = false class MockSubscriptionClient extends SubscriptionClient { sendMessage (operationId, type, payload) { - if (operationId && type === 'start') { + if (operationId && type === 'subscribe') { if (!sent) { t.pass() sent = true diff --git a/test/subscription-connection.js b/test/subscription-connection.js index 99b8bbef..09e35076 100644 --- a/test/subscription-connection.js +++ b/test/subscription-connection.js @@ -6,7 +6,7 @@ const fastify = require('fastify') const mq = require('mqemitter') const SubscriptionConnection = require('../lib/subscription-connection') const { PubSub } = require('../lib/subscriber') -const { GRAPHQL_WS } = require('../lib/subscription-protocol') +const { GRAPHQL_TRANSPORT_WS } = require('../lib/subscription-protocol') test('socket is closed on unhandled promise rejection in handleMessage', t => { t.plan(1) @@ -47,7 +47,7 @@ test('socket is closed on unhandled promise rejection in handleMessage', t => { app.listen(0, () => { const url = 'ws://localhost:' + (app.server.address()).port + '/graphql' - const ws = new WebSocket(url, 'graphql-ws') + const ws = new WebSocket(url, 'graphql-transport-ws') const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8', objectMode: true }) t.teardown(client.destroy.bind(client)) @@ -74,7 +74,7 @@ test('subscription connection sends error message when message is not json strin payload: 'Message must be a JSON string' }), message) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) await sc.handleMessage('invalid json string') @@ -86,7 +86,7 @@ test('subscription connection handles GQL_CONNECTION_TERMINATE message correctly on () {}, close () { t.pass() }, send (message) {}, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) await sc.handleMessage(JSON.stringify({ @@ -101,7 +101,7 @@ test('subscription connection closes context on GQL_STOP message correctly', asy on () {}, close () {}, send (message) {}, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) sc.subscriptionContexts = new Map() @@ -125,7 +125,7 @@ test('subscription connection completes resolver iterator on GQL_STOP message co on () {}, close () {}, send (message) {}, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) sc.subscriptionIters = new Map() @@ -155,7 +155,7 @@ test('handles error in send and closes connection', async t => { t.pass() }, on () {}, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {} ) @@ -168,7 +168,7 @@ test('subscription connection handles GQL_STOP message correctly, with no data', on () {}, close () {}, send (message) {}, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) await sc.handleMessage(JSON.stringify({ @@ -192,7 +192,7 @@ test('subscription connection send error message when GQL_START handler errs', a payload: 'handleGQLStart error' }), message) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) sc.isReady = true @@ -203,7 +203,7 @@ test('subscription connection send error message when GQL_START handler errs', a await sc.handleMessage(JSON.stringify({ id: 1, - type: 'start', + type: 'subscribe', payload: { } })) }) @@ -221,7 +221,7 @@ test('subscription connection send error message when client message type is inv payload: 'Invalid payload type' }), message) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) await sc.handleMessage(JSON.stringify({ @@ -242,14 +242,14 @@ test('subscription connection handles GQL_START message correctly, when payload. { type: 'error', id: 1, payload: 'Must provide document.' } ), message) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) sc.isReady = true await sc.handleMessage(JSON.stringify({ id: 1, - type: 'start', + type: 'subscribe', payload: { } })) }) @@ -265,12 +265,12 @@ test('subscription connection handles when GQL_START is called before GQL_INIT', { type: 'connection_error', payload: { message: 'Connection has not been established yet.' } } ), message) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) await sc.handleMessage(JSON.stringify({ id: 1, - type: 'start', + type: 'subscribe', payload: { } })) }) @@ -288,7 +288,7 @@ test('subscription connection extends context with onConnect return value', asyn send (message) { t.equal(JSON.stringify({ type: 'connection_ack' }), message) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, { context, onConnect: function () { @@ -314,7 +314,7 @@ test('subscription connection send GQL_ERROR message if connectionInit extension payload: 'Forbidden' }) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, { onConnect: function () { throw new Error('Not allowed') @@ -329,7 +329,7 @@ test('subscription connection send GQL_ERROR message if connectionInit extension sc.isReady = true await sc.handleMessage(JSON.stringify({ id: 1, - type: 'start', + type: 'subscribe', payload: {}, extensions: [ { type: 'connectionInit' } @@ -342,7 +342,7 @@ test('subscription connection does not create subscription if connectionInit ext on () { }, close () { }, send (message) {}, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, { onConnect: function () { throw new Error('Not allowed') @@ -357,7 +357,7 @@ test('subscription connection does not create subscription if connectionInit ext sc.isReady = true await sc.handleMessage(JSON.stringify({ id: 1, - type: 'start', + type: 'subscribe', payload: {}, extensions: [ { type: 'connectionInit' } @@ -380,13 +380,13 @@ test('subscription connection send GQL_ERROR on unknown extension', async (t) => payload: 'Unknown extension unknown' }) }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, { }) sc.isReady = true await sc.handleMessage(JSON.stringify({ id: 1, - type: 'start', + type: 'subscribe', payload: {}, extensions: [ { type: 'unknown' } @@ -404,7 +404,7 @@ test('subscription connection handleConnectionInitExtension returns the onConnec on () { }, close () { }, send (message) { }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, { onConnect: function () { return onConnectResult @@ -430,7 +430,7 @@ test('subscription connection extends the context with the connection_init paylo on () { }, close () { }, send (message) { }, - protocol: GRAPHQL_WS + protocol: GRAPHQL_TRANSPORT_WS }, {}) await sc.handleConnectionInit({ type: 'connection_init', payload: connectionInitPayload })