mercurius-js / mercurius

Implement GraphQL servers and gateways with Fastify
https://mercurius.dev/
MIT License
2.34k stars 234 forks source link

Accessing headers from subscripions through Gateways #924

Closed samberic closed 1 year ago

samberic commented 1 year ago

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 https://github.com/mercurius-js/mercurius/pull/424 which was closed a year ago. Wha I'm trying to achieve is this:

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.

mcollina commented 1 year ago

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

samberic commented 1 year ago

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

mcollina commented 1 year ago

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

marco-ippolito commented 1 year ago

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