Apollo Docs
/

API Reference: @apollo/gateway

Apollo Gateway API reference


This API reference documents the exports from the @apollo/gateway.

ApolloGateway

The core of the federated gateway implementation. For an example, see the "implementing" section.

constructor(options): ApolloGateway

Parameters

  • options: Object

    • serviceList: ServiceDefinition[] (required)

      An array of service definitions. A service definition contains the name and url for a federated service. The name is an arbitrary unique string and is primarily used for query planner output, error messages, logging, and the like.

      const gateway = new ApolloGateway({
        ...
        serviceList: [    { name: 'products', url: 'https://products-service.dev/graphql' },    { name: 'reviews', url: 'https://reviews-service.dev/graphql' }  ]});
    • buildService: (service: ServiceDefinition) => GraphQLDataSource

      A function to be called for each service in options.serviceList. The service is provided as an argument, and a GraphQLDataSource is expected to be returned. Allows for customizing transports.

      const gateway = new ApolloGateway({
        ...
        buildService({ name, url }) {    return new RemoteGraphQLDataSource({      url,      willSendRequest({ request, context }) {        request.http.headers.set('x-user-id', context.userId);      },    });  },});
    • introspectionHeaders: { [key: string]: string }

      An object containing headers sent with introspection queries

      const gateway = new ApolloGateway({
        ...
        introspectionHeaders: {    Authorization: 'my-header'  }});
    • debug: Boolean

      With debug enabled, the server will log startup messages as well as query plans during incoming requests.

Returns

An ApolloGateway instance, which is intended to be passed as the gateway configuration option of ApolloServer like so:

const server = new ApolloServer({
  gateway: new ApolloGateway({ serviceList: [...] }),
});

RemoteGraphQLDataSource

The RemoteGraphQLDataSource export is used to connect to underlying federated services from the gateway. It allows for customizations of its request using the willSendRequest function. By default, the ApolloGateway creates a new RemoteGraphQLDataSource for each service using the url from the service definition as its target location.

constructor(options): RemoteGraphQLDataSource

Parameters

  • options: Object

    • url: string (required)

      The location of the service to fetch using an HTTP request.

    • willSendRequest: (request: GraphQLRequest) => Promise<void>

      A function to be called on every fetch to the underlying GraphQL service. This method takes information about the incoming request, as well as the context from Apollo Server, to allow for customizing the request.

      new RemoteGraphQLDataSource({
        url,
        willSendRequest({ request, context }) {    request.http.headers.set('x-user-id', context.userId);  },});