How Do I Forward Headers from Gateway to Subgraph?

Have more questions? Submit a request

When defining your gateway object, you can forward request data from a gateway to a subgraph server by passing in a build service function. The gateway calls the build service function once for each subgraph.

The other necessary component is a RemoteGraphQLDataSource object. You can use the willSendRequest method to take the original request (context) and use that data to populate the request to your subgraphs(request).

For the complete API reference, please see the BuildService docs.

See below for an example of forwarding a single user's id header:

// gateway server index.ts
class AuthenticatedDataSource extends RemoteGraphQLDataSource {
  willSendRequest({ request, context }) {
    request.http.headers.set("x-user-id", context.userId);
  }
}

const gateway = new ApolloGateway({
  // ...other options...
  buildService({ name, url }) {
    return new AuthenticatedDataSource({ url });
  },
});

Articles in this section

Was this article helpful?
0 out of 0 found this helpful
Share

Comments

0 comments

Please sign in to leave a comment.