Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow gateway query planner to pass request context to GraphQLDataSource #419

Closed
mgilbey opened this issue Sep 4, 2020 · 1 comment
Closed

Comments

@mgilbey
Copy link

mgilbey commented Sep 4, 2020

When the query plan is executed, the query planner calls each relevant GraphQLDataSource, and it passes to it an object containing request; the new request for the data source, and context; this is the context property from the GraphQLRequestContext.

We can see this in the current send operation function:

    const response = await service.process({
      request: {
        query: source,
        variables,
        http,
      },
      context: context.requestContext.context,
    });

As there are many contexts involved here, lets call the GraphQLRequestContext the request context, and the context property from that object the user context. The type of this object would be GraphQLRequestContext<UserContext>.

The user context is created from the incoming http request in the contextFn argument to the ApolloServer constructor. This context is 'scoped' to the http request; all graphql operations in a single http request will share this object.

The request context is created for each graphql operation, and will be passed to apollo server plugins via the various lifecycle functions. This provides a good mechanism for adding properties to the request context that are useful for request or application diagnostics. Lets use the queryHash property from the request context as an example of data that we want to consume in a GraphQLDataSource. Our request context looks like:

{
  queryHash: "1234abcd...",
  context: { .. }, // user context
  ... other fields
}

We can see that the sendOperation above will only pass the user context and not the request context to the GraphQLDataSource, therefore preventing the GraphQLDataSource from accessing any data for the operation; in our example use case the queryHash. We cannot store this information in the user context as it is shared by all operations.

The feature request is to allow customisation of the context object that the gateway passes to the GraphQLDataSource so that information specific to operations can be retrieved, or to expose the original request context.

@abernix abernix transferred this issue from apollographql/apollo-server Jan 15, 2021
@glasser
Copy link
Member

glasser commented Jul 8, 2021

I think it would make sense to add a third topLevelRequestContext: GraphQLRequestContext option to process. (Arguably we could deprecate the context option if we wanted since it would be redundant.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants