Skip to content

Commit

Permalink
Expose the current request query path to the "willSendRequest" and "d…
Browse files Browse the repository at this point in the history
…idReceiveResponse" hooks (#2384)

This commit exposes the `path` to a given subgraph fetch in the hooks of `RemoteGraphQLDataSource`.
  • Loading branch information
talazenkot authored Feb 21, 2023
1 parent 8acc523 commit 1a7f484
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
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
? 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

0 comments on commit 1a7f484

Please sign in to comment.