#friendlier way of doing logic before forwarding request to subgraph, inside the gateway?interceptor?

5 messages · Page 1 of 1 (latest)

loud grove
#

I'm currently using the willSendRequest option to do some logic, but I'd like to do authorization on the gateway, would it be possible to use an interceptor to achieve this?

#

@sharp bone sorry to ping you, was wondering if off the top of your head you know of a way to get access to services inside this bit during module registration?

 GraphQLModule.forRootAsync<ApolloGatewayDriverConfig>({
      driver: ApolloGatewayDriver,
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService<AppConfig>) => {
        const server: ApolloDriverConfig = {
          debug: true,
          playground: true,
          autoSchemaFile: './apps/gateway/schema.gql',
          sortSchema: true,
          introspection: true,
          context: ({ req, res }) => ({ req, res }),
        };
        const origins = configService.get('CORS_ORIGINS');
        server.cors = { origin: origins, credentials: true };
        server.path = '/';

        return {
          gateway: {
            buildService: ({ url }) => {
              return new RemoteGraphQLDataSource({
                url,
                willSendRequest({ request, context }) {
                  if (context?.req?.headers?.authorization) {
                    // forwarding authorization header
                    request.http.headers.set(
                      'authorization',
                      context?.req?.headers?.authorization
                    );

                    // forwarding trace id header
                    request.http.headers.set(
                      'clientTraceId',
                      context?.req?.headers?.clientTracerId
                    );
                  }

                  // do authorization here
                },

I will have the context inside that willSendRequest

I'll want to call a redis service to checkup some stuff about the token, so a bit difficult to achieve in a static way -- I do also have the:

context: ({ req, res }) => ({ req, res }), where I could potentiall try to do this just not sure how to extract service from context

sharp bone
#

You can inject things into the factory via inject like you do for the ConfigService, right?

loud grove
#

I literally just noticed that

#

the second I saw you started typing