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: uniquely identify batched queries #849

Merged
merged 6 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions index.d.ts
Expand Up @@ -25,6 +25,9 @@ export interface PubSub {
export interface MercuriusContext {
app: FastifyInstance;
reply: FastifyReply;
operationsCount?: Number;
operationId?: Number;
__currentQuery: String;
/**
* __Caution__: Only available if `subscriptions` are enabled
*/
Expand Down
26 changes: 18 additions & 8 deletions lib/routes.js
Expand Up @@ -220,11 +220,16 @@ module.exports = async function (app, opts) {
return new MER_ERR_GQL_PERSISTED_QUERY_NOT_FOUND('Unknown query')
}

// Create individual contexts for multiple operations, otherwise reference the original context
const context = request[kRequestContext].operationsCount > 1
? Object.create(request[kRequestContext])
drakhart marked this conversation as resolved.
Show resolved Hide resolved
: request[kRequestContext]

// Handle the query, throwing an error if required
return reply.graphql(
query,
Object.assign(
request[kRequestContext],
context,
{ pubsub: subscriber, __currentQuery: query }
),
variables,
Expand Down Expand Up @@ -352,13 +357,18 @@ module.exports = async function (app, opts) {

if (allowBatchedQueries && Array.isArray(request.body)) {
// Batched query
return Promise.all(request.body.map(r =>
execute(r, request, reply)
.catch(e => {
const { response } = errorFormatter(e, request[kRequestContext])
return response
})
))
Object.assign(request[kRequestContext], { operationsCount: request.body.length })

return Promise.all(request.body.map(async (r, operationId) => {
Object.assign(request[kRequestContext], { operationId })
drakhart marked this conversation as resolved.
Show resolved Hide resolved

try {
return await execute(r, request, reply)
} catch (e) {
const { response } = errorFormatter(e, request[kRequestContext])
return response
}
}))
} else {
// Regular query
return execute(request.body, request, reply)
Expand Down
42 changes: 42 additions & 0 deletions test/batched.js
Expand Up @@ -2,6 +2,7 @@

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

test('POST regular query', async (t) => {
Expand Down Expand Up @@ -346,3 +347,44 @@ test('POST batched query with a resolver which succeeds and a resolver which thr

t.same(JSON.parse(res.body), [{ data: { add: 3 } }, { data: null, errors: [{ message: 'Internal Server Error' }] }])
})

test('POST batched query has an individual context for each operation', async (t) => {
const app = Fastify()

const contextSpy = sinon.spy()

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

const resolvers = {
test: (_, ctx) => contextSpy(ctx.operationId, ctx.operationsCount, ctx.__currentQuery)
}

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

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

t.ok(contextSpy.calledTwice)
t.ok(contextSpy.calledWith(0, 2, sinon.match(/TestQuery/)))
t.ok(contextSpy.calledWith(1, 2, sinon.match(/DoubleQuery/)))
simoneb marked this conversation as resolved.
Show resolved Hide resolved
})