API / Apollo Gateway Interview questions
How do you implement custom middleware or plugins in Apollo Gateway?
Apollo Gateway doesn't have a plugin system of its own separate from Apollo Server — the main extension point for touching outgoing subgraph requests and incoming subgraph responses is overriding buildService to supply a custom RemoteGraphQLDataSource.
const { ApolloGateway, RemoteGraphQLDataSource } = require('@apollo/gateway'); class AuthenticatedDataSource extends RemoteGraphQLDataSource { willSendRequest({ request, context }) { request.http.headers.set('authorization', context.token || ''); } didReceiveResponse({ response, request, context }) { return response; } } const gateway = new ApolloGateway({ buildService({ url }) { return new AuthenticatedDataSource({ url }); }, });
willSendRequest is the hook most commonly used — forwarding auth headers, adding tracing headers, or attaching request-scoped context to each outgoing subgraph call. didReceiveResponse is available for inspecting or transforming what comes back before it's stitched into the final result.
More Related questions...