typegraphql-nestjs icon indicating copy to clipboard operation
typegraphql-nestjs copied to clipboard

@Inject(CONTEXT) is undefined

Open ahilke opened this issue 10 months ago • 2 comments

It seems that @Inject(CONTEXT) is not working with typegraphql-nestjs the way it does for @nestjs/graphql.

I am trying to do something like this:

import { Inject, Injectable } from "@nestjs/common";
import { CONTEXT } from "@nestjs/graphql";
import { Recipe } from "./types";

@Injectable()
export default class RecipeService {
  private readonly recipes: Recipe[] = [];

  constructor(@Inject(CONTEXT) context) {
    console.log('Request Headers:', context?.req?.headers);
  } 
}

With an app module that looks like this:

import { ApolloDriver } from "@nestjs/apollo";
import { Module } from "@nestjs/common";
import { TypeGraphQLModule } from "typegraphql-nestjs";

import RecipeModule from "./recipe/module";

@Module({
  imports: [
    TypeGraphQLModule.forRoot({
      driver: ApolloDriver,
      emitSchemaFile: true,
      context: ({ req, res }) => {
        return { req, res };
      }
    }),
    RecipeModule,
  ],
})
export default class AppModule {}

However, context is just undefined. A similar example using @nestjs/graphql however does work, mostly following https://docs.nestjs.com/fundamentals/injection-scopes#request-provider.

I have not found anything in the docs that says this should not work or how to achieve this differently, so if this is expected or just works differently please let me know. I think this may be a similar issue as discussed in https://github.com/MichalLytek/typegraphql-nestjs/issues/22, but it was never reproduced or fixed.

Initially I was not sure myself where the issue comes from, so I have created a reproduction with both typegraphql-nestjs and @nestjs/graphql here: https://github.com/ahilke/typegraphql-nestjs-inject-context.

Thank you for your support!

ahilke avatar Apr 08 '24 18:04 ahilke