Graphql: [bug] GqlExecutionContext has undefined `req` when used with `apollo-server-fastify`

Created on 25 Oct 2020  路  8Comments  路  Source: nestjs/graphql

I'm submitting a...

  • [x] Bug report

Current behavior


context: ExecutionContext and GqlExecutionContext gives undefined req

Expected behavior


req should give FastifyRequest object, similar to what Express Request provides.

Minimal reproduction of the problem with instructions

  1. Create a Guard
  2. In canActivate(context: ExecutionContext), you can inspect the context, but req is undefined
  3. Do: GqlExecutionContext.create(context).getContext() which still gives undefined req

Additionally: there are FastifyDeprecation notices, see below:

(node:39933) [FSTDEP003] FastifyDeprecation: You are using the legacy Content Type Parser function signature. Use the one suggested in the documentation instead.
(node:39933) [FSTDEP001] FastifyDeprecation: You are accessing the Node.js core request object via "request.req", Use "request.raw" instead.
context= ExecutionContextHost {
  args: [
    undefined,
    {
      where: [Object: null prototype],
      limit: 20,
      start: 0,
      sort: 'createdAt:desc'
    },
    { req: undefined, _extensionStack: [GraphQLExtensionStack] },
    {
      fieldName: 'users',
      fieldNodes: [Array],
      returnType: [User!]!,
      parentType: Query,
      path: [Object],
      schema: [GraphQLSchema],
      fragments: [Object: null prototype] {},
      rootValue: undefined,
      operation: [Object],
      variableValues: [Object],
      cacheControl: [Object]
    }
  ],
  constructorRef: [class UsersResolver],
  handler: [AsyncFunction: users],
  contextType: 'graphql'
}
gqlCtx= {
  req: undefined,
  _extensionStack: GraphQLExtensionStack { extensions: [] }
}

What is the motivation / use case for changing the behavior?


Fix bug.

Environment


Nest version:

For Tooling issues:

  • Node version: v12.19.0
  • Platform: Ubuntu 20.04.1

Others:

  • Always reproducible as long as using fastify

All 8 comments

Would you like to create a PR for this issue?

@kamilmysliwiec I hope so, but I need some pointer on what function/file I should fix?

@ceefour

Not sure you got same my problem, I'm also using the apollo-server-fastify, And look like the data now store at request instead of req

1

My GqlAuthGuard

import { Injectable, ExecutionContext } from '@nestjs/common'
import { AuthGuard } from '@nestjs/passport'
import { GqlExecutionContext } from '@nestjs/graphql'

@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
  getRequest(context: ExecutionContext) {
    const ctx = GqlExecutionContext.create(context)
    return ctx.getContext().req
  }
}

@phattranky I'm not sure too. As you can see from my log, req is undefined while request doesn't exist at all.

This should be fixed in 7.8.1

I am having this same issue with the latest version 7.10.6 of @nestjs/graphql. req and request are both undefined. I am using express with nestjs.

@clayrisser

Hello. You really shouldn't post in Github like this. Because.....

  1. this is an old issue. Necroposting is unwanted, anywhere.
  2. This issue is about fastify, not express.
  3. To help you, you should bring your problem up in Nest's Discord server. https://discord.gg/TrES3tBN
  4. If after discussion in Discord shows you have a bug/ issue, then you can post a _new issue_ here.

Thanks for your understanding!

Scott

Was this page helpful?
0 / 5 - 0 ratings