Type-graphql: TypegraphQL context in nest

Created on 19 Mar 2019  路  5Comments  路  Source: MichalLytek/type-graphql

Haven't found any documentation on how to get a context and lets say auth headers for validation in NestJS with TypegraphQL module. Does anybody knows how to intercept or inject context in TypegraphQL module, would really appreciate. Any sample of guards for JWT

Out of scope Question

All 5 comments

I'm not responsible for the integration of TypeGraphQL with Nest. You should ask about that in the Nest GraphQL repo:
https://github.com/nestjs/graphql/issues/163

In Nest, TypeGraphQL is used mostly for reflection and schema generation, and only some subset of the features works. So you can't apply the JWT and auth recipes from examples in Nest:
https://github.com/19majkel94/type-graphql/blob/afe2b94b93fc822e9a66f98db30fcc8f0ce3fca4/examples/authorization/index.ts#L16-L31

solution for those who might need it
https://github.com/nestjs/nest/issues/1767

@19majkel94 actually I have implemented JWT auth as well, works fine

Hey @hxdef2517
Could you possibly show me your solution?

Here is the method from my resolver:

  @Query(() => String)
  @UseGuards(AuthGuard)
  async me(@Ctx() ctx: MyContext) {
    return 'Hello';
  }

So, my problem is that ctx argument appears as undefined but in the following example, when I inspect ctx everything is ok, it's get populated with the request object.

  @Query(() => String)
  @UseGuards(AuthGuard)
  async me(...args: any) {
    return 'Hello';
  }

@nakhodkiin
use Context, not Ctx

import { Context } from '@nestjs/graphql'

@Query(() => String)
async me(@Context() ctx: MyContext) {
  return 'Hello';
}
Was this page helpful?
0 / 5 - 0 ratings