node.jstypescriptgraphqlnestjstypegraphql

How to access Response object in NestJS GraphQL resolver


How can I access pass @Res() into my graphql resolvers?

this doesn't work:

@Mutation(() => String)
  login(@Args('loginInput') loginInput: LoginInput, @Res() res: Response) {
    return this.authService.login(loginInput, res);
  }

Solution

  • @Res() is for HTTP Requests. To access to res object you'd need to first make sure it is added to the context for graphql by using context: ({ req, res }) => ({ req, res }) in the GraphqlModule's options, and then you can use @Context() ctx to get the context and ctx.res to get the response object