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

Fix batched queries context cloning #859

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion lib/routes.js
Expand Up @@ -358,7 +358,12 @@ module.exports = async function (app, opts) {

return Promise.all(request.body.map(async (r, operationId) => {
// Create individual reqs for multiple operations, otherwise reference the original req
const operationReq = operationsCount > 1 ? Object.create(request) : request
const operationReq = operationsCount > 1
? {
...request,
[kRequestContext]: Object.create(request[kRequestContext])
simoneb marked this conversation as resolved.
Show resolved Hide resolved
}
: request

Object.assign(operationReq[kRequestContext], { operationId })

Expand Down
71 changes: 71 additions & 0 deletions test/batched.js
Expand Up @@ -388,3 +388,74 @@ test('POST batched query has an individual context for each operation', async (t
sinon.assert.calledWith(contextSpy, 0, 2, sinon.match(/TestQuery/))
sinon.assert.calledWith(contextSpy, 1, 2, sinon.match(/DoubleQuery/))
})

test('POST batched query respects custom class-based context', async (t) => {
const app = Fastify()

const schema = `
type Query {
test: String
}
`

class CustomContext {
constructor () {
this.test = 'custom'
}

method () {
return this.test
}
}

const resolvers = {
test: async (args, ctx) => {
t.type(ctx, 'object')
t.type(ctx.reply, 'object')
t.type(ctx.app, 'object')
t.type(ctx.method, 'function')
t.equal(ctx.test, 'custom')
t.equal(ctx.method(), 'custom')
t.equal(ctx.constructor, CustomContext)
return ctx.method()
}
}

app.register(GQL, {
schema,
resolvers,
context: (request, reply) => {
t.type(request, 'object')
t.type(reply, 'object')
return new CustomContext()
},
allowBatchedQueries: true
})

const post = await app.inject({
method: 'POST',
url: '/graphql',
body: [
{
operationName: 'TestQuery',
query: 'query TestQuery { test }'
},
{
operationName: 'DoubleQuery',
query: 'query DoubleQuery { test }'
}
]
})

t.same(JSON.parse(post.body), [
{
data: {
test: 'custom'
}
}, {
data: {
test: 'custom'
}
}
])
})
78 changes: 78 additions & 0 deletions test/hooks-with-batching.js
@@ -0,0 +1,78 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const sinon = require('sinon')
const GQL = require('..')

test('batched query has an individual context for each operation through all the lifecycle hooks', async (t) => {
const app = Fastify()

const preParsingSpy = sinon.spy()
const preValidationSpy = sinon.spy()
const preExecutionSpy = sinon.spy()
const onResolutionSpy = sinon.spy()

const schema = `
type Query {
test: String
}
`

const resolvers = {
test: () => 'test'
}

await app.register(GQL, {
schema,
resolvers,
allowBatchedQueries: true
})

app.graphql.addHook('preParsing', (_, __, ctx) => {
preParsingSpy(ctx.operationId, ctx.operationsCount, ctx.__currentQuery)
})

app.graphql.addHook('preValidation', (_, __, ctx) => {
preValidationSpy(ctx.operationId, ctx.operationsCount, ctx.__currentQuery)
})

app.graphql.addHook('preExecution', (_, __, ctx) => {
preExecutionSpy(ctx.operationId, ctx.operationsCount, ctx.__currentQuery)
})

app.graphql.addHook('onResolution', (_, ctx) => {
onResolutionSpy(ctx.operationId, ctx.operationsCount, ctx.__currentQuery)
})

await app.inject({
method: 'POST',
url: '/graphql',
body: [
{
operationName: 'TestQuery',
query: 'query TestQuery { test }'
},
{
operationName: 'DoubleQuery',
query: 'query DoubleQuery { test }'
}
]
})

sinon.assert.calledTwice(preParsingSpy)
sinon.assert.calledWith(preParsingSpy, 0, 2, sinon.match(/TestQuery/))
sinon.assert.calledWith(preParsingSpy, 1, 2, sinon.match(/DoubleQuery/))

sinon.assert.calledTwice(preValidationSpy)
sinon.assert.calledWith(preValidationSpy, 0, 2, sinon.match(/TestQuery/))
sinon.assert.calledWith(preValidationSpy, 1, 2, sinon.match(/DoubleQuery/))

sinon.assert.calledTwice(preExecutionSpy)
sinon.assert.calledWith(preExecutionSpy, 0, 2, sinon.match(/TestQuery/))
sinon.assert.calledWith(preExecutionSpy, 1, 2, sinon.match(/DoubleQuery/))

sinon.assert.calledTwice(onResolutionSpy)
sinon.assert.calledWith(onResolutionSpy, 0, 2, sinon.match(/TestQuery/))
sinon.assert.calledWith(onResolutionSpy, 1, 2, sinon.match(/DoubleQuery/))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same response, but this time at hook level instead of at resolver level.

})