1

How do you get the graphql context in a gqlgen subgraph that has been passed down from the federated graphql gateway?

hboylan
  • 357
  • 3
  • 12
  • Looks like it can't be done. Instead, we pass "context" to subgraphs using headers. Anyone have experience with this? – hboylan Jun 30 '22 at 14:30

1 Answers1

1

You would need to add a buildService function in your gateway that json stringifies the context in the gateway and then injects that into a header that will be sent to the subgraph and then in the subgraph you will need to parse that header and put that into the context yourself so your resolves can access it. Something like this:

{    
  gateway: {
    buildService({ url }) {
      return new RemoteGraphQLDataSource({
        url,
        willSendRequest({ request, context }) {
          request.http.headers.set('X-GQL-Context', JSON.stringify(context))
        },
      })
    },
  },
}
Kdawgwilk
  • 369
  • 3
  • 11