Repository: mercurius-js/mercurius
Pull request #594 (2021-10-06 08:03:43)

feat: switch default subscription protocol to graphql-transport-ws
according with #593 
here a PR to switch the default subscription protocol to graphql-transport-ws


Patch:
diff --git a/lib/subscription-client.js b/lib/subscription-client.js
index 4bf0638..294046c 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')
@@ -45,7 +45,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])


Test patch:
diff --git a/test/subscription-client.js b/test/subscription-client.js
index ae3fe88..dd2a967 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' }))
       }
     })
@@ -406,7 +406,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' }))
       }
     })
@@ -501,7 +501,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 efb8b42..d887d47 100644
--- a/test/subscription-connection.js
+++ b/test/subscription-connection.js
@@ -8,6 +8,7 @@ const SubscriptionConnection = require('../lib/subscription-connection')
 const { PubSub } = require('../lib/subscriber')
 const { GRAPHQL_WS, GRAPHQL_TRANSPORT_WS } = require('../lib/subscription-protocol')
 
+
 test('socket is closed on unhandled promise rejection in handleMessage', t => {
   t.plan(1)
   let handleConnectionCloseCalled = false
@@ -47,7 +48,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 +75,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 +87,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 +102,7 @@ test('subscription connection closes context on GQL_STOP message correctly (prot
     on () {},
     close () {},
     send (message) {},
-    protocol: GRAPHQL_WS
+    protocol: GRAPHQL_TRANSPORT_WS
   }, {})
 
   sc.subscriptionContexts = new Map()
@@ -149,7 +150,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()
@@ -203,7 +204,7 @@ test('handles error in send and closes connection', async t => {
         t.pass()
       },
       on () {},
-      protocol: GRAPHQL_WS
+      protocol: GRAPHQL_TRANSPORT_WS
     },
     {}
   )
@@ -216,7 +217,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({
@@ -256,7 +257,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
@@ -267,7 +268,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: { }
   }))
 })
@@ -285,7 +286,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({
@@ -306,14 +307,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: { }
   }))
 })
@@ -329,12 +330,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: { }
   }))
 })
@@ -352,7 +353,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 () {
@@ -378,7 +379,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')
@@ -393,7 +394,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' }
@@ -406,7 +407,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')
@@ -421,7 +422,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' }
@@ -444,13 +445,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' }
@@ -468,7 +469,7 @@ test('subscription connection handleConnectionInitExtension returns the onConnec
     on () { },
     close () { },
     send (message) { },
-    protocol: GRAPHQL_WS
+    protocol: GRAPHQL_TRANSPORT_WS
   }, {
     onConnect: function () {
       return onConnectResult
@@ -494,7 +495,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 })