From 56f65d0611bdee5e69fa2c456f20c9dca2fb03a7 Mon Sep 17 00:00:00 2001 From: Alex Tennant Date: Wed, 13 Mar 2024 17:45:15 +0000 Subject: [PATCH] feat: make request available in context by default --- index.d.ts | 1 + lib/routes.js | 4 ++-- test/types/index.ts | 11 ++++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2610c694..b1c61213 100644 --- a/index.d.ts +++ b/index.d.ts @@ -30,6 +30,7 @@ declare namespace mercurius { export interface MercuriusContext { app: FastifyInstance; + request: FastifyRequest; reply: FastifyReply; operationsCount?: number; operationId?: number; diff --git a/lib/routes.js b/lib/routes.js index 9d5d7985..77b6d744 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -305,9 +305,9 @@ module.exports = async function (app, opts) { // Generate the context for this request if (contextFn) { request[kRequestContext] = await contextFn(request, reply) - Object.assign(request[kRequestContext], { reply, app }) + Object.assign(request[kRequestContext], { request, reply, app }) } else { - request[kRequestContext] = { reply, app } + request[kRequestContext] = { request, reply, app } } validationHandler(request.validationError) diff --git a/test/types/index.ts b/test/types/index.ts index 3c86cde9..b2d5351a 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -63,7 +63,7 @@ const resolvers: IResolvers = { // declare module 'mercurius' { declare module '../../' { interface MercuriusContext { // eslint-disable-line - request: FastifyRequest + extra: boolean } } @@ -89,9 +89,9 @@ app.register(mercurius, { }, queryDepth: 8, cache: true, - context: (request) => { + context: () => { return { - request + extra: true } }, schemaTransforms: (schema) => schema @@ -117,9 +117,9 @@ app.register(mercurius, { }, queryDepth: 8, cache: true, - context: (request) => { + context: () => { return { - request + extra: true } }, schemaTransforms: (schema) => schema @@ -197,6 +197,7 @@ app.register(async function (app) { app.graphql.defineResolvers({ Query: { dogs (_root, _params, ctx) { + ctx.extra ctx.request ctx.reply ctx.pubsub