I think the formatResponse
argument to your ApolloServer constructor should help you. This allows you to process responses from the server before they are sent to client applications. The function takes a GraphQLResponse
and GraphQLRequestContext
as input and returns a GraphQLResponse
. The interface for GraphQLResponse
is as follows:
export interface GraphQLResponse {
data?: Record<string, any> | null;
errors?: ReadonlyArray<GraphQLFormattedError>;
extensions?: Record<string, any>;
http?: Pick<Response, 'headers'> & Partial<Pick<Mutable<Response>, 'status'>>;
}
This means that you can configure your server with the additional argument to parse responses before they are sent to the client:
const myServer = new ApolloServer(
...,
formatResponse: (response, requestContext) => {
// then create a new response here with your additional headers and return it, for instance
const newResponse = { ...response, http: { response.http, { headers: ...response.http.headers, newHeader: newValue };
return newResponse;
}
Note that the above is only a skeleton, you will need to check the response object more thoroughly than that as its properties are largely optional, but this should give you the gist.
For more details see the documentation here