authenticationcookiesgraphqlnestjsnestjs-jwt

Can't set cookie in response NestJs


I Have a nestjs graphQL auth microservice and trying to set a http only cookie containing the token, and for some reason setting the cookie doesn't work, i get this error:

{
  "errors": [
    {
      "message": "response.cookie is not a function",
      "path": [
        "login"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "GraphQLError: response.cookie is not a function",
          "    at downstreamServiceError (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:872:12)",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:577:9",
          "    at Array.map (<anonymous>)",
          "    at sendOperation (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:576:38)",
          "    at processTicksAndRejections (node:internal/process/task_queues:95:5)",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:450:41",
          "    at executeNode (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:372:9)",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/executeQueryPlan.ts:195:27",
          "    at /home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/gateway/src/index.ts:826:28",
          "    at execute (/home/ghassen/Desktop/pfe/MyKlad Project/api-gateway/node_modules/@apollo/server/src/requestPipeline.ts:536:22)"
        ],
        "serviceName": "users"
      }
    }
  ],
  "data": null
}

Here is main.ts

async function bootstrap() {
  const app: NestExpressApplication =
    await NestFactory.create<NestExpressApplication>(
      UsersModule,
      new ExpressAdapter(),
    );

  app.use(cookieParser());

  return app.listen(9009);
}

bootstrap();

and this is my auth resolver

@Resolver('Auth')
export class AuthResolver {
  constructor(private authService: AuthService) {}

  @Query('login')
  async login(
    @Res() response: Response,
    @Args('user') user: LoginUserInput,
  ): Promise<LoginResult> {
    // log(res.cookie('test', 33));

    try {
      const result = await this.authService.validateUserByPassword(user);

      if (result) {
        response.cookie('access_token', result.token);
        return result;
      }
      throw new AuthenticationError(
        'Could not log-in with the provided credentials',
      );
    } catch (err) {
      throw err;
    }
  }

}

i can't seem to figure out why as for my point of view everything should be working.

I tried fastify platform, with fastify cookie and I am getting a similar error. I am expecting to set the cookie


Solution

  • @Res() is not a valid decorator for GraphQL resolvers. You should instead use @Context() to get the context object and pull the res object from there.

    @Context() { res } or @Context() ctx => ctx.res should do it. You may need to modify your GraphQlModule.forRoot() to include context: ({ req, res }) => ({ req, res })