Skip to content

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

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 14 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-books-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@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!

10 changes: 7 additions & 3 deletions gateway-js/src/datasources/RemoteGraphQLDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class RemoteGraphQLDataSource<
async process(
options: GraphQLDataSourceProcessOptions<TContext>,
): Promise<GatewayGraphQLResponse> {
const { request, context: originalContext } = options;
const { request, context: originalContext, nodeGraphPath } = options;
// Deal with a bit of a hairy situation in typings: when doing health checks
// and schema checks we always pass in `{}` as the context even though it's
// not really guaranteed to be a `TContext`, and then we pass it to various
Expand Down Expand Up @@ -143,6 +143,7 @@ export class RemoteGraphQLDataSource<
request: requestWithoutQuery,
context,
overallCachePolicy,
nodeGraphPath
});
}
}
Expand All @@ -160,6 +161,7 @@ export class RemoteGraphQLDataSource<
request: requestWithQuery,
context,
overallCachePolicy,
nodeGraphPath
});
}

Expand Down Expand Up @@ -226,15 +228,17 @@ export class RemoteGraphQLDataSource<
request,
context,
overallCachePolicy,
nodeGraphPath
}: {
response: GatewayGraphQLResponse;
request: GatewayGraphQLRequest;
context: TContext;
overallCachePolicy: GatewayCachePolicy | null;
nodeGraphPath: GraphQLDataSourceProcessOptions<TContext>['nodeGraphPath']
}): Promise<GatewayGraphQLResponse> {
const processedResponse =
typeof this.didReceiveResponse === 'function'
? await this.didReceiveResponse({ response, request, context })
? await this.didReceiveResponse({ response, request, context, nodeGraphPath })
: response;

if (overallCachePolicy) {
Expand Down Expand Up @@ -266,7 +270,7 @@ export class RemoteGraphQLDataSource<
public didReceiveResponse?(
requestContext: Required<
Pick<GatewayGraphQLRequestContext<TContext>, 'request' | 'response' | 'context'>
>,
> & Pick<GraphQLDataSourceProcessOptions<TContext>, 'nodeGraphPath'>
): GatewayGraphQLResponse | Promise<GatewayGraphQLResponse>;

public didEncounterError(
Expand Down
6 changes: 6 additions & 0 deletions gateway-js/src/datasources/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ResponsePath } from '@apollo/query-planner';
import { GatewayGraphQLResponse, GatewayGraphQLRequestContext } from '@apollo/server-gateway-interface';

export interface GraphQLDataSource<
Expand All @@ -21,6 +22,11 @@ export type GraphQLDataSourceProcessOptions<
* The request to send to the subgraph.
*/
request: GatewayGraphQLRequestContext<TContext>['request'];

/**
* 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.

} & (
| {
kind: GraphQLDataSourceRequestKind.INCOMING_OPERATION;
Expand Down
1 change: 1 addition & 0 deletions gateway-js/src/executeQueryPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ async function executeFetch(
incomingRequestContext: context.requestContext,
context: context.requestContext.context,
document: fetch.operationDocumentNode,
nodeGraphPath: currentCursor.path
});

if (response.errors) {
Expand Down