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 all 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
: undefined;

// 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
}): 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 }
): 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 an empty array for top-level fetch operations.
*/
pathInIncomingRequest?: ResponsePath;
}
| {
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