Skip to content

Add context object to GraphQLDataSource.didReceiveResponse arguments #3360

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

Merged
merged 24 commits into from
Oct 4, 2019

Conversation

trevor-scheer
Copy link
Contributor

This adds a third parameter context to the didReceiveResponse method on a GraphQLDataSource. This gives the hook access to the context object, allowing it to read and modify as necessary.

As demonstrated by the included test, this enables a GraphQLDataSource to inspect the response and add information to context accordingly.

This also provides parity with another hook, willSendRequest, which already has access to the context object.

@@ -87,9 +91,10 @@ export class RemoteGraphQLDataSource implements GraphQLDataSource {
>,
): ValueOrPromise<void>;

public async didReceiveResponse<TResult = any>(
public async didReceiveResponse<TContext, TResult = any>(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fly-by comment: Adding this as the first positional type argument would be a breaking change for any existing implementors (who aren't us, since we just rely on the default of any) — can it be added after TResult = any with its own default value?

(Realistically though, we should probably have this TContext as a type variable on the entire class rather than having it localize to this method.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. I took the easy way in 0ecd617.

I'll take another stab at TContext on the class, but RemoteGraphQLDataSource.process() wasn't really having it.

@trevor-scheer trevor-scheer marked this pull request as ready for review October 1, 2019 22:04
Copy link
Member

@abernix abernix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking wonderful! I think it certainly will work in its current state and meet the necessary goal but there's just some polishing and bike-shedding of patterns (patterns which your PR did not introduce) to sort out before we finalize it and move it into the release.

@jhampton
Copy link
Contributor

jhampton commented Oct 3, 2019

Flowchart for discussion @trevor-scheer and @abernix

Copy link
Member

@abernix abernix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updates and new examples here are wonderful! This whole PR is wonderful!

I left one comment where I think there's a typo but LGTM!

trevor-scheer and others added 20 commits October 4, 2019 13:39
This adds a third parameter to the `didReceiveResponse` method on
a GraphQLDataSource. This gives the hook access to the context
object, allowing it to read and modify the context as necessary.

As demonstrated by the included test, this enables a GraphQLDataSource
to inspect the response and add information to the context accordingly.

This also provides parity with another hook, willSendRequest, which already
has access to the context object.
Co-Authored-By: Jesse Rosenberger <[email protected]>
The sub-classing pattern is one we'd prefer to propagate,
so we should use it in our tests as well. This also enables
the use of `super`!
@trevor-scheer trevor-scheer force-pushed the trevor/data-source-context branch from 7712eb8 to c610ba6 Compare October 4, 2019 20:42
@trevor-scheer trevor-scheer merged commit 8881d45 into master Oct 4, 2019
@rtymchyk
Copy link

Excuse my inability to understand this, but I'm having trouble following what 'context' is here. Flow chart implies that it's the context of the final response, and some of the documentation has hints of that as well, but I wanted to confirm this.

What I'd really like to do is pass through some response headers from service X to the final response headers of the gateway.

@abernix abernix deleted the trevor/data-source-context branch February 25, 2020 21:02
abernix pushed a commit to apollographql/federation that referenced this pull request Sep 4, 2020
…nts (apollographql/apollo-server#3360)

This adds a third parameter to the `didReceiveResponse` method on
a GraphQLDataSource. This gives the hook access to the context
object, allowing it to read and modify the context as necessary.

As demonstrated by the included test, this enables a GraphQLDataSource
to inspect the response and add information to the context accordingly.

This also provides parity with another hook, willSendRequest, which already
has access to the context object.
Apollo-Orig-Commit-AS: apollographql/apollo-server@8881d45
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants