What is [Symbol(kHeaders)] and how do I parse it as headers

What is [Symbol(kHeaders)] and how do I parse it as headers


2

I am having trouble parsing the header retrieved from graphql’s context, what I would like to do is to basically forward the headers from the request into the response

My gateway:

GraphQLModule.forRootAsync<ApolloGatewayDriverConfig>({
    driver: ApolloGatewayDriver,
    inject: [ConfigService],
    useFactory: async(configService: ConfigService) => ({
        server: {
            introspection: true,
            playground: true,
            cache: 'bounded'
        },
        gateway: {
            buildService: (url) => new CustomDataSource(url),
            supergraphSdl: ...
        }
    })
})

My CustomDataSource which should forward headers:

export class CustomDataSource  extends RemoteGraphQLDataSource {
    constructor(config: any) {
        super(config);
        this.fetcher = fetcher.defaults({
            maxSockets: Infinity,
            strictSSL: false,
            retry: false,
        })
    }

    willSendRequest({context, request }) {
        console.log('context', context.req); // <---- here
        request.http?.headers.set('authorization', context.req.authorization);
    }
}

My issue now is that the context.req.headers do not exist when it should and logging it out gives me headers in a different name as kHeaders instead which I am unsure of how to parse this.

context.req:

{
  ...,
  ...,
  [Symbol(kHeaders)]: {
    authorization: 'abcdefg'
  }
}

I tried something like context.req[‘Symbol(kHeaders)’] which returns me cannot read properties, would like to know why is my headers returned this way and how do I parse this

4

  • The headers should be in context object. Have you tried to log context.authorization?

    – Mostafa Fakhraei

    Jan 20 at 7:55

  • yeee, it's weird the whole section for the normal header is gone, im only left with this symbol and rawheader, im using rawheader as a workaround now

    – LuxuryWaffles

    Jan 20 at 8:51

  • Encountering the same problem after upgrading to node v16/v18. In node v14 the header was available in the context but with the update it isn't. Did you solve this?

    – Fabian

    Mar 16 at 9:03

  • i believed i used the rawHeaders as a work around and wrote a util to parse it into object format, then later app was refactored such that the stuff i needed are all in the authorization token which I got from context.authorization

    – LuxuryWaffles

    Mar 16 at 9:22


1 Answer
1


0

That is probably not the end game solution for your issue, but for other people like me trying to access values inside [Symbol(kHeaders)… You can access the properties inside [Symbol(kHeaders)] using req.get().

In your case it would be req.get(‘authorization’)

New contributor

Hans Müller is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *