Skip to content

Commit

Permalink
Ensure metrics is present before plugin initialization.
Browse files Browse the repository at this point in the history
This eliminates the need to guard for the presence of `metrics` on the
`requestContext` within plugins who are calling `requestDidStart`.

Ref: #3998 (comment)
  • Loading branch information
abernix committed Apr 28, 2020
1 parent fc05e8d commit 48eab7e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,7 @@ export class ApolloServerBase {
request,
context: options.context || Object.create(null),
cache: options.cache!,
metrics: {},
response: {
http: {
headers: new Headers(),
Expand Down
15 changes: 10 additions & 5 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ export async function processGraphQLRequest<TContext>(
// all of our own machinery will certainly set it now.
const logger = requestContext.logger || console;

// If request context's `metrics` already exists, preserve it, but _ensure_ it
// exists there and shorthand it for use throughout this function. As of this
// comment, the sole known case where `metrics` already exists is when the
// `captureTraces` property is present and set to the result of the boolean
// `reporting` option on the legacy (V1) server options, here:
// https://git.io/Jfmsb. I suspect this disappears when this is the direct
// entry into request processing, rather than through, e.g. `runHttpQuery`.
const metrics = requestContext.metrics =
requestContext.metrics || Object.create(null);

let cacheControlExtension: CacheControlExtension | undefined;
const extensionStack = initializeExtensionStack();
(requestContext.context as any)._extensionStack = extensionStack;
Expand All @@ -137,11 +147,6 @@ export async function processGraphQLRequest<TContext>(

await initializeDataSources();

const metrics = requestContext.metrics || Object.create(null);
if (!requestContext.metrics) {
requestContext.metrics = metrics;
}

const request = requestContext.request;

let { query, extensions } = request;
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface GraphQLRequestContext<TContext = Record<string, any>> {
*/
readonly errors?: ReadonlyArray<GraphQLError>;

readonly metrics?: GraphQLRequestMetrics;
readonly metrics: GraphQLRequestMetrics;

debug?: boolean;
}
Expand Down

0 comments on commit 48eab7e

Please sign in to comment.