#Using throttler with graphql causes: TypeError: Cannot read properties of undefined (reading 'header

1 messages · Page 1 of 1 (latest)

main rock
#

I'm following the official nestjs documentation and using the throttler globally, but when I try to make a graphql request, it keeps throwing this error. It works for plain http requests though.

@Injectable()
export class GqlThrottlerGuard extends ThrottlerGuard {
  getRequestResponse(context: ExecutionContext) {
    if (context.getType() === 'http') {
      const ctx = context.switchToHttp();
      return { req: ctx.getRequest(), res: ctx.getResponse() };
    }
    const gqlCtx = GqlExecutionContext.create(context);
    const ctx = gqlCtx.getContext();

    return { req: ctx.req, res: ctx.res };
  }
@Module({
  imports: [
    ThrottlerModule.forRoot([
      {
        ttl: 60000,
        limit: 10,
      },
    ]),
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: GqlThrottlerGuard,
    },
    // ....
  ],
})

export class AppModule {
  constructor() {
  }
}

main rock
#

Found the solution. Had to add the res object in the context when initializing the graphql client

const apolloDriverConfig: ApolloDriverConfig = {
  context: ({ req, res }) => ({ req, res }),
  // ....
};