From 6acc19903c5bfac13fe98382bd7b8f2d1f36a0f9 Mon Sep 17 00:00:00 2001 From: Drakhart <14996002+drakhart@users.noreply.github.com> Date: Wed, 31 Aug 2022 16:02:39 +0200 Subject: [PATCH] test: more on batched queries context --- test/batched.js | 71 +++++++++++++++++++++++++++++++++ test/hooks-with-batching.js | 78 +++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 test/hooks-with-batching.js diff --git a/test/batched.js b/test/batched.js index cdf98c1c..1147fda5 100644 --- a/test/batched.js +++ b/test/batched.js @@ -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' + } + } + ]) +}) diff --git a/test/hooks-with-batching.js b/test/hooks-with-batching.js new file mode 100644 index 00000000..a4ebeb22 --- /dev/null +++ b/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/)) +})