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

@apollo/gateway - Expose the current request query path to the "willSendRequest" and "didReceiveResponse" hooks #2384

Merged
merged 14 commits into from
Feb 21, 2023
Merged

@apollo/gateway - Expose the current request query path to the "willSendRequest" and "didReceiveResponse" hooks #2384

merged 14 commits into from
Feb 21, 2023

Conversation

talazenkot
Copy link
Contributor

@talazenkot talazenkot commented Feb 8, 2023

Description:
Exposed the path to the node currently resolved by the gateway to the willSendRequest and didReceiveResponse hooks.

Solves #2353

@netlify
Copy link

netlify bot commented Feb 8, 2023

👷 Deploy request for apollo-federation-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 2da416c

@changeset-bot
Copy link

changeset-bot bot commented Feb 8, 2023

🦋 Changeset detected

Latest commit: 2da416c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@apollo/gateway Patch
@apollo/federation-internals Patch
@apollo/composition Patch
@apollo/query-planner Patch
@apollo/query-graphs Patch
@apollo/subgraph Patch
apollo-federation-integration-testsuite Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 8, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@talazenkot talazenkot changed the title Current request graph path exposure @apollo/gateway - Expose the current request query path to the "willSendRequest" and "didReceiveResponse" hooks Feb 8, 2023
@korinne
Copy link
Contributor

korinne commented Feb 9, 2023

Thanks again for opening a PR. We have filed this PR as under review, and will provide feedback shortly.

@talazenkot
Copy link
Contributor Author

talazenkot commented Feb 13, 2023

Thanks again for opening a PR. We have filed this PR as under review, and will provide feedback shortly.

@korinne hey! any news regarding this? maybe there's something I can do from my side to push it forward?

/**
* The path to the current processed node within the graph query
*/
nodeGraphPath?: ResponsePath;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd move this inside the INCOMING_OPERATION part (that path makes no sense for a HEALTH_CHECK or LOADING_SCHEMA kind of request) and maybe rename it to something like pathInIncomingRequest (we call incomingRequestContext the total operation received by the gateway, and the "path" is really the path within that gateway operation at which this particular subgraph "sub-query" fits in, and that's probably how I would describe that field btw; I also wouldn't use node in the name as that's query plan specific naming and most users aren't meant to be familiar with that).

Could be nice to also document that the path may be undefined if it is unavailable, but will be empty (rather than undefined) for "top-level" fetches.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarifications! I've adopted your guidelines.

"@apollo/gateway": patch
---

Current request graph path exposure
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be awesome to expand this entry a bit. I'd suggest something along the lines of:

Exposes, for each subgraph request, the path in the overall gateway operation at which that subgraph request gets inserted. This path is now available as the pathInIncomingRequest field in the arguments of RemoteGraphQLDataSource.willSendRequest and RemoteGraphQLDataSource.didReceiveResponse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Amazing. thanks!

@talazenkot talazenkot requested a review from pcmanus February 21, 2023 09:41
Copy link
Contributor

@pcmanus pcmanus left a comment

Choose a reason for hiding this comment

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

A few last remarks. Sorry, I may not have been super clear in my previous ones.

@@ -67,6 +68,11 @@ export class RemoteGraphQLDataSource<
options: GraphQLDataSourceProcessOptions<TContext>,
): Promise<GatewayGraphQLResponse> {
const { request, context: originalContext } = options;
const pathInIncomingRequest =
options.kind === GraphQLDataSourceRequestKind.INCOMING_OPERATION
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need that test (we can use options.pathInIncomingRequest directly): it's going to be undefined for non-INCOMING_OPERATION, but that feels almost more appropriate, and that avoids having to deal with null at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the null assignment to undefined, however, I cannot use options.pathInIncomingRequest directly because the pathInIncomingRequest property is not guaranteed to exist in options because of the usage of typescript's union operator:
Screenshot 2023-02-21 at 14 53 19

therefore, I must verify options.kind before using it, unless you can think of another approach?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, correct, this is my bad.


/**
* The path in the overall gateway operation at which that subgraph request gets inserted.
* Please note that this could be set to `undefined` when the path is not available, or set to `null` for top-level fetch operations.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe the "or set to null for top-level fetch operations" part is quite right: it should say "or be an empty array for top-level fetch operations".

What I mean/understand by "top-level" fetch operations is just subgraph fetches that are not entity fetches but rather query some Query/Mutation fields, and those will still be GraphQLDataSourceRequestKind.INCOMING_OPERATION but where the path in executeQueryPlan will be empty.

Your current patch sets null for queries that are not GraphQLDataSourceRequestKind.INCOMING_OPERATION, but those are queries that don't belong to the execution of a gateway query: they are instead "maintenance" queries the gateway does for either refreshing it's understanding of the schema or "health checks" of the subgraph. As said in my other comment, having the path to undefined to mean "not available" is fine for those imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. Misunderstood. Got you now.

@talazenkot
Copy link
Contributor Author

A few last remarks. Sorry, I may not have been super clear in my previous ones.

No worries. Thanks a lot of your responsiveness and explanations. I've made a few adjustments according to your comments, and also explained why as far as I can tell I cannot use options.pathInIncomingRequest directly.

Copy link
Contributor

@pcmanus pcmanus left a comment

Choose a reason for hiding this comment

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

Lgtm. Thanks a lot for the contribution.

@@ -67,6 +68,11 @@ export class RemoteGraphQLDataSource<
options: GraphQLDataSourceProcessOptions<TContext>,
): Promise<GatewayGraphQLResponse> {
const { request, context: originalContext } = options;
const pathInIncomingRequest =
options.kind === GraphQLDataSourceRequestKind.INCOMING_OPERATION
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, correct, this is my bad.

@pcmanus pcmanus merged commit 1a7f484 into apollographql:main Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants