Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch default subscription protocol to graphql-transport-ws #594

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/subscription-client.js
Expand Up @@ -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')
Expand Down Expand Up @@ -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])
Expand Down
6 changes: 4 additions & 2 deletions 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
Expand Down
14 changes: 7 additions & 7 deletions test/subscription-client.js
Expand Up @@ -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' } } }))
}
})
})
Expand Down Expand Up @@ -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' }))
}
})
Expand Down Expand Up @@ -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' }))
}
})
Expand Down Expand Up @@ -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' }))
}
})
Expand Down Expand Up @@ -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' }))
}
})
Expand Down Expand Up @@ -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
Expand Down
48 changes: 24 additions & 24 deletions test/subscription-connection.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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))

Expand All @@ -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')
Expand All @@ -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({
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -155,7 +155,7 @@ test('handles error in send and closes connection', async t => {
t.pass()
},
on () {},
protocol: GRAPHQL_WS
protocol: GRAPHQL_TRANSPORT_WS
},
{}
)
Expand All @@ -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({
Expand All @@ -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
Expand All @@ -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: { }
}))
})
Expand All @@ -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({
Expand All @@ -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: { }
}))
})
Expand All @@ -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: { }
}))
})
Expand All @@ -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 () {
Expand All @@ -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')
Expand All @@ -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' }
Expand All @@ -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')
Expand All @@ -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' }
Expand All @@ -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' }
Expand All @@ -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
Expand All @@ -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 })
Expand Down