Skip to content

Commit

Permalink
Update to Fastify v4 (#808)
Browse files Browse the repository at this point in the history
* Update to Fastify v4

Signed-off-by: Matteo Collina <[email protected]>

* Update CI

Signed-off-by: Matteo Collina <[email protected]>

* 100% codecov

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Jun 9, 2022
1 parent edc602d commit ef154c5
Show file tree
Hide file tree
Showing 43 changed files with 159 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
node-version: [14.x, 16.x, 18.x]
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v3
Expand Down
42 changes: 29 additions & 13 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ const getSchema = {
}
}

const postSchema = (allowBatchedQueries) => ({
body: {
type: allowBatchedQueries ? ['object', 'array'] : 'object',
const postSchema = (allowBatchedQueries) => {
let body = {
type: 'object',
properties: {
...requestProperties,
variables: {
Expand All @@ -100,17 +100,33 @@ const postSchema = (allowBatchedQueries) => ({
type: 'object'
}
}
},
// JSON schema isn't allowing ['object', 'array'] on response.
response: allowBatchedQueries
? {}
: {
'2xx': {
type: 'object',
properties: responseProperties
}

if (allowBatchedQueries) {
body = {
anyOf: [{
...body
}, {
type: 'array',
items: {
...body
}
}
})
}]
}
}
return {
body,
// JSON schema isn't allowing ['object', 'array'] on response.
response: allowBatchedQueries
? {}
: {
'2xx': {
type: 'object',
properties: responseProperties
}
}
}
}

function validationHandler (validationError) {
if (validationError) {
Expand Down
6 changes: 2 additions & 4 deletions lib/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ function createConnectionHandler ({ subscriber, fastify, onConnect, onDisconnect
}
}

module.exports = function (fastify, opts, next) {
module.exports = async function (fastify, opts) {
const { getOptions, subscriber, verifyClient, onConnect, onDisconnect, lruGatewayResolvers, entityResolversFactory, subscriptionContextFn, keepAlive, fullWsTransport } = opts

// If `fastify.websocketServer` exists, it means `@fastify/websocket` already registered.
// Without this check, @fastify/websocket will be registered multiple times and raises FST_ERR_DEC_ALREADY_PRESENT.
if (fastify.websocketServer === undefined) {
fastify.register(fastifyWebsocket, {
await fastify.register(fastifyWebsocket, {
options: {
maxPayload: 1048576,
verifyClient
Expand All @@ -87,6 +87,4 @@ module.exports = function (fastify, opts, next) {
fullWsTransport
})
})

next()
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
"@sinonjs/fake-timers": "^9.0.0",
"@types/node": "^17.0.0",
"@types/ws": "^8.2.0",
"@types/isomorphic-form-data": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"autocannon": "^7.3.0",
"concurrently": "^7.0.0",
"docsify-cli": "^4.4.3",
"fastify": "^3.28.0",
"fastify": "^4.0.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"sinon": "^14.0.0",
Expand All @@ -54,9 +55,8 @@
},
"dependencies": {
"@fastify/error": "^3.0.0",
"@fastify/static": "^5.0.0",
"@fastify/websocket": "^5.0.0",
"@types/isomorphic-form-data": "^2.0.0",
"@fastify/static": "^6.0.0",
"@fastify/websocket": "^6.0.0",
"events.on": "^1.0.1",
"fastify-plugin": "^3.0.1",
"graphql": "^16.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/custom-root-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ test('redefined subscription type', t => {
}
})

app.listen(0, err => {
app.listen({ port: 0 }, err => {
t.error(err)

const ws = new WebSocket('ws://localhost:' + (app.server.address()).port + '/graphql', 'graphql-ws')
Expand Down
2 changes: 1 addition & 1 deletion test/federation.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ test('subscription server sends update to subscriptions', t => {
federationMetadata: true
})

app.listen(0, err => {
app.listen({ port: 0 }, err => {
t.error(err)

const ws = new WebSocket('ws://localhost:' + (app.server.address()).port + '/graphql', 'graphql-ws')
Expand Down
2 changes: 1 addition & 1 deletion test/gateway/aliases-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function createTestService (t, schema, resolvers = {}) {
federationMetadata: true,
allowBatchedQueries: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/aliases.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function createTestService (t, schema, resolvers = {}) {
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
4 changes: 2 additions & 2 deletions test/gateway/application-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('onGatewayReplaceSchema - polling interval with a new schema should trigger
federationMetadata: true
})

await userService.listen(0)
await userService.listen({ port: 0 })

const userServicePort = userService.server.address().port

Expand Down Expand Up @@ -149,7 +149,7 @@ test('onGatewayReplaceSchema - should log an error should any errors occur in th
federationMetadata: true
})

await userService.listen(0)
await userService.listen({ port: 0 })

const userServicePort = userService.server.address().port

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/batching-on-both-gateway-and-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function createTestService (t, schema, resolvers = {}) {
federationMetadata: true,
allowBatchedQueries: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
6 changes: 3 additions & 3 deletions test/gateway/connectionInit-extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ test('connectionInit extension e2e testing', t => {
Promise.all([
userService.ready(),
notificationService.ready(),
userService.listen(0),
notificationService.listen(0)
userService.listen({ port: 0 }),
notificationService.listen({ port: 0 })
]).then(() => {
gateway.register(GQL, {
subscription: true,
Expand Down Expand Up @@ -193,7 +193,7 @@ test('connectionInit extension e2e testing', t => {
}
})

gateway.listen(0, err => {
gateway.listen({ port: 0 }, err => {
t.error(err)
async function addUser () {
await gateway.inject({
Expand Down
2 changes: 1 addition & 1 deletion test/gateway/custom-directives-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function createTestService (t, schema, resolvers = {}) {
federationMetadata: true,
allowBatchedQueries: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/custom-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function createTestService (t, schema, resolvers = {}) {
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/errors-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function createTestService (t, schema, resolvers = {}, allowBatchedQueries
federationMetadata: true,
allowBatchedQueries
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
4 changes: 2 additions & 2 deletions test/gateway/extends-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function createService (t, schema, resolvers = {}) {
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })

return [service, service.server.address().port]
}
Expand Down Expand Up @@ -407,7 +407,7 @@ test('gateway distributes query correctly to services when querying with inline
}
})

await gateway.listen(0)
await gateway.listen({ port: 0 })

const query = `
query {
Expand Down
4 changes: 2 additions & 2 deletions test/gateway/fix-726.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ async function buildProxy (port1, port2) {
test('federated node should be able to return external Type directly', async (t) => {
const port1 = 3027
const serviceOne = await buildService()
await serviceOne.listen(port1)
await serviceOne.listen({ port: port1 })
t.teardown(() => { serviceOne.close() })

const port2 = 3028
const serviceTwo = await buildServiceExternal()
await serviceTwo.listen(port2)
await serviceTwo.listen({ port: port2 })
t.teardown(() => { serviceTwo.close() })

const serviceProxy = await buildProxy(port1, port2)
Expand Down
2 changes: 1 addition & 1 deletion test/gateway/hooks-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function createTestService (t, schema, resolvers = {}) {
federationMetadata: true,
allowBatchedQueries: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function createTestService (t, schema, resolvers = {}) {
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/include-directive-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function createTestService (t, schema, resolvers = {}) {
federationMetadata: true,
allowBatchedQueries: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/include-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function createTestService (t, schema, resolvers = {}) {
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/load-balancing-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function createTestService (t, schema, resolvers = {}, fn = async () => {}
federationMetadata: true,
allowBatchedQueries: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/load-balancing.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function createTestService (t, schema, resolvers = {}, fn = async () => {}
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })
return [service, service.server.address().port]
}

Expand Down
2 changes: 1 addition & 1 deletion test/gateway/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function createService (t, schema, resolvers = {}) {
resolvers,
federationMetadata: true
})
await service.listen(0)
await service.listen({ port: 0 })

return [service, service.server.address().port]
}
Expand Down
Loading

0 comments on commit ef154c5

Please sign in to comment.