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 });
},
});
Comments
0 commentsPlease sign in to leave a comment.