graphqlapolloapollo-federationgqlgen

Federated graphql context with gqlgen


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


Solution

  • 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))
            },
          })
        },
      },
    }