#NestJs Fastify Mercurius Gateway - Passing Header / Cookie to Service

2 messages · Page 1 of 1 (latest)

ionic vale
#

Hi. I have two Nestjs applications running. One is configured as a mercurius gateway and the other one as mercurius federation service. The gateway passes my queries to the service and the service responds correctly.

Now I want to add an Auth Guard to my service. My idea was to get some JWT access token from my service and store it in a cookie / header and pass that information through the gateway to the client. When the client sends the next request, the gateway passes the Information to the service and the service can use the auth guard with that Information.

My problem here is, that I cannot pass that Information through the gateway. I can set cookies directly in the gateway and the client receives it. But with the next request passed through the gateway to the service, the service doesn't get any Information about the clients cookie.

So my question is: How can i send cookies and header information from a client through the gateway to the service?

#

For some more Information:

I can send queries / mutations to the gateway and manipulate request and reply like so:

    GraphQLModule.forRootAsync<MercuriusGatewayDriverConfig>({
      driver: MercuriusGatewayDriver,
      imports: [ConfigModule, JwtModule.register({}), HttpModule, LoggerModule],
      inject: [ConfigService, JwtService, HttpService, PinoLogger],
      useFactory: (
        configService: ConfigService,
        jwtService: JwtService,
        httpService: HttpService,
        logger: PinoLogger,
      ): any => ({        
        context: async (request: FastifyRequest, reply: FastifyReply) => {
          
          console.log(request.cookies);
          reply.setCookie('foo', 'bar');

          return {request, reply}
        }
      }),
      // ...
    })

I also tried to use fastify hooks to manipulate request and response but still the service won't get that Information:

export class AppModule implements NestModule {
  constructor(private readonly appHost: HttpAdapterHost) {}
  configure(consumer: any) {
    const fastify = this.appHost.httpAdapter.getInstance() as FastifyInstance;
    fastify.addHook('onRequest', (req, res, done) => { 
      
      // try to manipulate headers before passing to the service
      req.headers = { 'x-user': 'FOO' };

      done();
    });
    
    fastify.addHook('onSend', (req, res, payload, done) => {

      // setting cookie before sending response, so that the next request with cookies will be passed through to the service
      res.setCookie('foo', 'bar');
      
      done();
    });
  }
}

So I am pretty sure, that I am doing something wrong here. What I found was this github issue: https://github.com/mercurius-js/mercurius/issues/65
Which leads to the documentation from mercurius/gateway. https://github.com/mercurius-js/mercurius-gateway#collectors
Unfortunately I didn't get the context.collection example to work.... Maybe this is the way to go but I couldn't figure it out

GitHub

Any Example on how to leverage fastify-jwt plugin with fastify-gql for authentication and Authorization example?

GitHub

Mercurius federation support plugin. Contribute to mercurius-js/mercurius-gateway development by creating an account on GitHub.