graphql federation -- gateway isn't forwarding RESPONSE headers

580 views Asked by At

I have a node + nest graphql api with a gateway and subgraphs.

I have already configured the REQUEST headers to be forward to my subgraphs. The problem I now have is opposite, it seems that the gateway strips away any custom response headers set by the subgraphs.

I can confirm this happens during local development, so it's not caused by my in-production infrastructure.

But for the sake of it, talking to the subgraphs directly in production, the custom headers are present in the response headers.

I can only conclude this is a problem at a gateway configuration level.

EDIT:

Found solution meanwhile:

  gateway: {
            buildService: ({ name, url }) => {
              return new RemoteGraphQLDataSource({
                url,
                didReceiveResponse({ response, context }) {
                  const header = response?.http?.headers?.get('traceid');

                  if (header) {
                    (context?.req?.res as Response)?.setHeader(
                      'traceId',
                      header
                    );
                  }

                  return response;
                },
              });
            },
1

There are 1 answers

0
tagplus5 On

It you are using NestJS GraphQL with Fastify and Apollo Federation Gateway, you can pass headers from subgraphs to client this way:

...
gateway: {
  buildService: ({ name, url }) => {
    return new RemoteGraphQLDataSource({
      ...
      didReceiveResponse({ response, request, context }) {
        const headers = response?.http?.headers;
        headers?.forEach((value, key) => {
          context?.reply?.header?.(key, value);
        });

        return response;
      },
    });
  },
  ...
},