mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify

Home Page:https://mercurius.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accessing headers from subscripions through Gateways

samberic opened this issue · comments

Hi team,
First, thanks for all the great work you've done on Mercurius. It's a fantastic project.

My issue is an extension of #424 which was closed a year ago. Wha I'm trying to achieve is this:

  • All users have a header (a JWT) which we use for auth and identification purposes.
  • We use this on subscriptions- both on initial connect (for logging) and then on messages that are sent through

the issue
As far as I can tell/understand, when operating through the gateway the gateway connects to the service once and multiplexes all calls through it. As a result I can never get user headers.

Minimal reproduction: https://gist.github.com/samberic/808e2fb94130c7122ab58ac25d6206a5

What I would expect to happen:
When subscribing via graphiql, including the header { "userId":123 }, I would see on the service subscription.onConnect method the log line "Sub connect for user: 123"

What actually happens:
On start up I see

Sub connect for user: undefined
Sub connect for user: undefined

and it's never called again.

If there is an alternative way to get access to the headers, perhaps with the rewriteConnectionInitPayload/onConnect, I'm all ears, but right now I can't find anyway to make this work.

Ah! This is indeed true. Would you like to send a PR to add an option to control this behavior?

I'm looking into this at the moment, the code base is proving tricky :). Is there a guide or a discord or slack?

We chat in the Fastify discord https://discord.com/invite/fastify

if you are using mercurius-gateway there is an option called collectors that allows you to gather headers from the response of federated services