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 13 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
---

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.
14 changes: 12 additions & 2 deletions gateway-js/src/datasources/RemoteGraphQLDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isObject } from '../utilities/predicates';
import { GraphQLDataSource, GraphQLDataSourceProcessOptions, GraphQLDataSourceRequestKind } from './types';
import { createHash } from '@apollo/utils.createhash';
import { ResponsePath } from '@apollo/query-planner';
import { parseCacheControlHeader } from './parseCacheControlHeader';
import fetcher from 'make-fetch-happen';
import { Headers as NodeFetchHeaders, Request as NodeFetchRequest } from 'node-fetch';
Expand Down Expand Up @@ -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.

? options.pathInIncomingRequest
: null;

// 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 +149,7 @@ export class RemoteGraphQLDataSource<
request: requestWithoutQuery,
context,
overallCachePolicy,
pathInIncomingRequest
});
}
}
Expand All @@ -160,6 +167,7 @@ export class RemoteGraphQLDataSource<
request: requestWithQuery,
context,
overallCachePolicy,
pathInIncomingRequest
});
}

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

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

public didEncounterError(
Expand Down
7 changes: 7 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 Down Expand Up @@ -47,6 +48,12 @@ export type GraphQLDataSourceProcessOptions<
* this will increase the memory used by the gateway query plan cache.
*/
document?: GatewayGraphQLRequestContext<TContext>['document'];

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

*/
pathInIncomingRequest?: ResponsePath | null;
}
| {
kind:
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,
pathInIncomingRequest: currentCursor.path
});

if (response.errors) {
Expand Down