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: make request available in context by default #1088

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ declare namespace mercurius {

export interface MercuriusContext {
app: FastifyInstance;
request: FastifyRequest;
reply: FastifyReply;
operationsCount?: number;
operationId?: number;
Expand Down
4 changes: 2 additions & 2 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const resolvers: IResolvers = {
// declare module 'mercurius' {
declare module '../../' {
interface MercuriusContext { // eslint-disable-line
request: FastifyRequest
extra: boolean
}
}

Expand All @@ -89,9 +89,9 @@ app.register(mercurius, {
},
queryDepth: 8,
cache: true,
context: (request) => {
context: () => {
return {
request
extra: true
}
},
schemaTransforms: (schema) => schema
Expand All @@ -117,9 +117,9 @@ app.register(mercurius, {
},
queryDepth: 8,
cache: true,
context: (request) => {
context: () => {
return {
request
extra: true
}
},
schemaTransforms: (schema) => schema
Expand Down Expand Up @@ -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
Expand Down
Loading