How to customize request to pass on to subgraph on managed federation

Hi, I m testing our Apollo graphql to use the managed federation, but I cant seem to find a way to pass in buildService(definition: ServiceEndpointDefinition) into the gateway’s option as the doc mentioned that we just have to initialize the gateway const gateway = new ApolloGateway() without any options and it will detect that we are using managed federation.

Old Code:

class AuthenticatedDataSource extends RemoteGraphQLDataSource {
  willSendRequest({ request, context }: any) {
    const { authentication, authorization, environment, instance, operationName } = context;
    request.http!.headers.set('authentication', authentication);
    request.http!.headers.set('authorization', authorization);
    request.http!.headers.set('environment', environment);
    request.http!.headers.set('instance', instance);
    request.http!.headers.set('operationName', operationName);
    request.http!.headers.set('traceId', uid());
    request.http!.headers.set(
      'x-correlation-id',
      context['x-correlation-id'] === undefined ? 'polling' : context['x-correlation-id'],
    );
  }
}
const gateway = new ApolloGateway({
   buildService(definition: ServiceEndpointDefinition) {
     const { url } = definition;
     return new AuthenticatedDataSource({ url });
   },
   experimental_updateSupergraphSdl: async () => {
     const supergraphSdl = readFileSync('./supergraph.graphql').toString();
     return {
       id: new Date().toISOString(),
      supergraphSdl,
    };
  },
});

anyone? From the Apollo team maybe? Is it this line of code that is preventing the headers to be passed to the subgraphs?
image

I cant seem to find a way to pass in buildService(definition: ServiceEndpointDefinition) into the gateway’s option as the doc mentioned that we just have to initialize the gateway const gateway = new ApolloGateway() without any options and it will detect that we are using managed federation.

Passing buildService is allowed with managed federation. The gateway doesn’t use managed federation if you pass:

  • serviceList
  • supergraphSdl
  • experimental_updateSupergraphSdl

But other options have no affect on managed federation. I think that answers your question, but please let me know if I misunderstood!

1 Like

Hi, thank you so much for this. I should’ve tried it first.