-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathexecuteQueryPlan.ts
623 lines (564 loc) · 18.5 KB
/
executeQueryPlan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import {
GraphQLExecutionResult,
GraphQLRequestContext,
} from 'apollo-server-types';
import { Headers } from 'apollo-server-env';
import {
execute,
GraphQLError,
Kind,
TypeNameMetaFieldDef,
GraphQLFieldResolver,
GraphQLFormattedError,
isAbstractType,
GraphQLSchema,
} from 'graphql';
import { Trace, google } from 'apollo-reporting-protobuf';
import { defaultRootOperationNameLookup } from '@apollo/federation';
import { GraphQLDataSource } from './datasources/types';
import { OperationContext } from './operationContext';
import {
FetchNode,
PlanNode,
QueryPlan,
ResponsePath,
QueryPlanSelectionNode,
QueryPlanFieldNode,
getResponseName,
toAPISchema
} from '@apollo/query-planner';
import { deepMerge } from './utilities/deepMerge';
import { isNotNullOrUndefined } from './utilities/array';
import { SpanStatusCode } from "@opentelemetry/api";
import { OpenTelemetrySpanNames, tracer } from "./utilities/opentelemetry";
export type ServiceMap = {
[serviceName: string]: GraphQLDataSource;
};
type ResultMap = Record<string, any>;
interface ExecutionContext<TContext> {
queryPlan: QueryPlan;
operationContext: OperationContext;
serviceMap: ServiceMap;
requestContext: GraphQLRequestContext<TContext>;
errors: GraphQLError[];
}
export async function executeQueryPlan<TContext>(
queryPlan: QueryPlan,
serviceMap: ServiceMap,
requestContext: GraphQLRequestContext<TContext>,
operationContext: OperationContext,
): Promise<GraphQLExecutionResult> {
return tracer.startActiveSpan(OpenTelemetrySpanNames.EXECUTE, async span => {
try {
const errors: GraphQLError[] = [];
const context: ExecutionContext<TContext> = {
queryPlan,
operationContext,
serviceMap,
requestContext,
errors,
};
let data: ResultMap | undefined | null = Object.create(null);
const captureTraces = !!(
requestContext.metrics && requestContext.metrics.captureTraces
);
if (queryPlan.node) {
const traceNode = await executeNode(
context,
queryPlan.node,
data!,
[],
captureTraces,
);
if (captureTraces) {
requestContext.metrics!.queryPlanTrace = traceNode;
}
}
let result = await tracer.startActiveSpan(OpenTelemetrySpanNames.POST_PROCESSING, async (span) => {
// FIXME: Re-executing the query is a pretty heavy handed way of making sure
// only explicitly requested fields are included and field ordering follows
// the original query.
// It is also used to allow execution of introspection queries though.
try {
const executionResult = await execute({
schema: toAPISchema(operationContext.schema),
document: {
kind: Kind.DOCUMENT,
definitions: [
operationContext.operation,
...Object.values(operationContext.fragments),
],
},
rootValue: data,
variableValues: requestContext.request.variables,
// See also `wrapSchemaWithAliasResolver` in `gateway-js/src/index.ts`.
fieldResolver: defaultFieldResolverWithAliasSupport,
});
data = executionResult.data;
if (executionResult.errors?.length) {
errors.push(...executionResult.errors)
}
} catch (error) {
span.setStatus({ code:SpanStatusCode.ERROR });
return { errors: [error] };
}
finally {
span.end()
}
if(errors.length > 0) {
span.setStatus({ code:SpanStatusCode.ERROR });
}
return errors.length === 0 ? { data } : { errors, data };
});
if(result.errors) {
span.setStatus({ code:SpanStatusCode.ERROR });
}
return result;
}
catch (err) {
span.setStatus({ code:SpanStatusCode.ERROR });
throw err;
}
finally {
span.end();
}
});
}
// Note: this function always returns a protobuf QueryPlanNode tree, even if
// we're going to ignore it, because it makes the code much simpler and more
// typesafe. However, it doesn't actually ask for traces from the backend
// service unless we are capturing traces for Studio.
async function executeNode<TContext>(
context: ExecutionContext<TContext>,
node: PlanNode,
results: ResultMap | ResultMap[],
path: ResponsePath,
captureTraces: boolean,
): Promise<Trace.QueryPlanNode> {
if (!results) {
// XXX I don't understand `results` threading well enough to understand when this happens
// and if this corresponds to a real query plan node that should be reported or not.
//
// This may be if running something like `query { fooOrNullFromServiceA {
// somethingFromServiceB } }` and the first field is null, then we don't bother to run the
// inner field at all.
return new Trace.QueryPlanNode();
}
switch (node.kind) {
case 'Sequence': {
const traceNode = new Trace.QueryPlanNode.SequenceNode();
for (const childNode of node.nodes) {
const childTraceNode = await executeNode(
context,
childNode,
results,
path,
captureTraces,
);
traceNode.nodes.push(childTraceNode!);
}
return new Trace.QueryPlanNode({ sequence: traceNode });
}
case 'Parallel': {
const childTraceNodes = await Promise.all(
node.nodes.map(async childNode =>
executeNode(context, childNode, results, path, captureTraces),
),
);
return new Trace.QueryPlanNode({
parallel: new Trace.QueryPlanNode.ParallelNode({
nodes: childTraceNodes,
}),
});
}
case 'Flatten': {
return new Trace.QueryPlanNode({
flatten: new Trace.QueryPlanNode.FlattenNode({
responsePath: node.path.map(
id =>
new Trace.QueryPlanNode.ResponsePathElement(
typeof id === 'string' ? { fieldName: id } : { index: id },
),
),
node: await executeNode(
context,
node.node,
flattenResultsAtPath(results, node.path),
[...path, ...node.path],
captureTraces,
),
}),
});
}
case 'Fetch': {
const traceNode = new Trace.QueryPlanNode.FetchNode({
serviceName: node.serviceName,
// executeFetch will fill in the other fields if desired.
});
try {
await executeFetch(
context,
node,
results,
path,
captureTraces ? traceNode : null,
);
} catch (error) {
context.errors.push(error);
}
return new Trace.QueryPlanNode({ fetch: traceNode });
}
}
}
async function executeFetch<TContext>(
context: ExecutionContext<TContext>,
fetch: FetchNode,
results: ResultMap | (ResultMap | null | undefined)[],
_path: ResponsePath,
traceNode: Trace.QueryPlanNode.FetchNode | null,
): Promise<void> {
const logger = context.requestContext.logger || console;
const service = context.serviceMap[fetch.serviceName];
return tracer.startActiveSpan(OpenTelemetrySpanNames.FETCH, {attributes:{service:fetch.serviceName}}, async span => {
try {
if (!service) {
throw new Error(`Couldn't find service with name "${fetch.serviceName}"`);
}
let entities: ResultMap[];
if (Array.isArray(results)) {
// Remove null or undefined entities from the list
entities = results.filter(isNotNullOrUndefined);
} else {
entities = [results];
}
if (entities.length < 1) return;
let variables = Object.create(null);
if (fetch.variableUsages) {
for (const variableName of fetch.variableUsages) {
const providedVariables = context.requestContext.request.variables;
if (
providedVariables &&
typeof providedVariables[variableName] !== 'undefined'
) {
variables[variableName] = providedVariables[variableName];
}
}
}
if (!fetch.requires) {
const dataReceivedFromService = await sendOperation(
context,
fetch.operation,
variables,
);
for (const entity of entities) {
deepMerge(entity, dataReceivedFromService);
}
} else {
const requires = fetch.requires;
const representations: ResultMap[] = [];
const representationToEntity: number[] = [];
entities.forEach((entity, index) => {
const representation = executeSelectionSet(
context.operationContext,
entity,
requires,
);
if (representation && representation[TypeNameMetaFieldDef.name]) {
representations.push(representation);
representationToEntity.push(index);
}
});
// If there are no representations, that means the type conditions in
// the requires don't match any entities.
if (representations.length < 1) return;
if ('representations' in variables) {
throw new Error(`Variables cannot contain key "representations"`);
}
const dataReceivedFromService = await sendOperation(
context,
fetch.operation,
{...variables, representations},
);
if (!dataReceivedFromService) {
return;
}
if (
!(
dataReceivedFromService._entities &&
Array.isArray(dataReceivedFromService._entities)
)
) {
throw new Error(`Expected "data._entities" in response to be an array`);
}
const receivedEntities = dataReceivedFromService._entities;
if (receivedEntities.length !== representations.length) {
throw new Error(
`Expected "data._entities" to contain ${representations.length} elements`,
);
}
for (let i = 0; i < entities.length; i++) {
deepMerge(entities[representationToEntity[i]], receivedEntities[i]);
}
}
}
catch (err) {
span.setStatus({ code:SpanStatusCode.ERROR });
throw err;
}
finally
{
span.end();
}
});
async function sendOperation(
context: ExecutionContext<TContext>,
source: string,
variables: Record<string, any>,
): Promise<ResultMap | void | null> {
// We declare this as 'any' because it is missing url and method, which
// GraphQLRequest.http is supposed to have if it exists.
let http: any;
// If we're capturing a trace for Studio, then save the operation text to
// the node we're building and tell the federated service to include a trace
// in its response.
if (traceNode) {
http = {
headers: new Headers({ 'apollo-federation-include-trace': 'ftv1' }),
};
if (
context.requestContext.metrics &&
context.requestContext.metrics.startHrTime
) {
traceNode.sentTimeOffset = durationHrTimeToNanos(
process.hrtime(context.requestContext.metrics.startHrTime),
);
}
traceNode.sentTime = dateToProtoTimestamp(new Date());
}
const response = await service.process({
request: {
query: source,
variables,
http,
},
context: context.requestContext.context,
});
if (response.errors) {
const errors = response.errors.map(error =>
downstreamServiceError(
error,
fetch.serviceName,
source,
variables,
),
);
context.errors.push(...errors);
}
// If we're capturing a trace for Studio, save the received trace into the
// query plan.
if (traceNode) {
traceNode.receivedTime = dateToProtoTimestamp(new Date());
if (response.extensions && response.extensions.ftv1) {
const traceBase64 = response.extensions.ftv1;
let traceBuffer: Buffer | undefined;
let traceParsingFailed = false;
try {
// XXX support non-Node implementations by using Uint8Array? protobufjs
// supports that, but there's not a no-deps base64 implementation.
traceBuffer = Buffer.from(traceBase64, 'base64');
} catch (err) {
logger.error(
`error decoding base64 for federated trace from ${fetch.serviceName}: ${err}`,
);
traceParsingFailed = true;
}
if (traceBuffer) {
try {
const trace = Trace.decode(traceBuffer);
traceNode.trace = trace;
} catch (err) {
logger.error(
`error decoding protobuf for federated trace from ${fetch.serviceName}: ${err}`,
);
traceParsingFailed = true;
}
}
if (traceNode.trace) {
// Federation requires the root operations in the composed schema
// to have the default names (Query, Mutation, Subscription) even
// if the implementing services choose different names, so we override
// whatever the implementing service reported here.
const rootTypeName =
defaultRootOperationNameLookup[
context.operationContext.operation.operation
];
traceNode.trace.root?.child?.forEach((child) => {
child.parentType = rootTypeName;
});
}
traceNode.traceParsingFailed = traceParsingFailed;
}
}
return response.data;
}
}
/**
*
* @param source Result of GraphQL execution.
* @param selectionSet
*/
function executeSelectionSet(
operationContext: OperationContext,
source: Record<string, any> | null,
selections: QueryPlanSelectionNode[],
): Record<string, any> | null {
// If the underlying service has returned null for the parent (source)
// then there is no need to iterate through the parent's selection set
if (source === null) {
return null;
}
const result: Record<string, any> = Object.create(null);
for (const selection of selections) {
switch (selection.kind) {
case Kind.FIELD:
const responseName = getResponseName(selection as QueryPlanFieldNode);
const selections = (selection as QueryPlanFieldNode).selections;
if (typeof source[responseName] === 'undefined') {
throw new Error(`Field "${responseName}" was not found in response.`);
}
if (Array.isArray(source[responseName])) {
result[responseName] = source[responseName].map((value: any) =>
selections
? executeSelectionSet(operationContext, value, selections)
: value,
);
} else if (selections) {
result[responseName] = executeSelectionSet(
operationContext,
source[responseName],
selections,
);
} else {
result[responseName] = source[responseName];
}
break;
case Kind.INLINE_FRAGMENT:
if (!selection.typeCondition) continue;
const typename = source && source['__typename'];
if (!typename) continue;
if (doesTypeConditionMatch(operationContext.schema, selection.typeCondition, typename)) {
deepMerge(
result,
executeSelectionSet(operationContext, source, selection.selections),
);
}
break;
}
}
return result;
}
function doesTypeConditionMatch(
schema: GraphQLSchema,
typeCondition: string,
typename: string,
): boolean {
if (typeCondition === typename) {
return true;
}
const type = schema.getType(typename);
if (!type) {
return false;
}
const conditionalType = schema.getType(typeCondition);
if (!conditionalType) {
return false;
}
if (isAbstractType(conditionalType)) {
return schema.isSubType(conditionalType, type);
}
return false;
}
function flattenResultsAtPath(value: any, path: ResponsePath): any {
if (path.length === 0) return value;
if (value === undefined || value === null) return value;
const [current, ...rest] = path;
if (current === '@') {
return value.flatMap((element: any) => flattenResultsAtPath(element, rest));
} else {
return flattenResultsAtPath(value[current], rest);
}
}
function downstreamServiceError(
originalError: GraphQLFormattedError,
serviceName: string,
query: string,
variables?: Record<string, any>,
) {
let {
message,
extensions,
path,
} = originalError
if (!message) {
message = `Error while fetching subquery from service "${serviceName}"`;
}
extensions = {
code: 'DOWNSTREAM_SERVICE_ERROR',
// XXX The presence of a serviceName in extensions is used to
// determine if this error should be captured for metrics reporting.
serviceName,
query,
variables,
...extensions,
};
return new GraphQLError(
message,
undefined,
undefined,
undefined,
path,
originalError as Error,
extensions,
);
}
export const defaultFieldResolverWithAliasSupport: GraphQLFieldResolver<
any,
any
> = function(source, args, contextValue, info) {
// ensure source is a value for which property access is acceptable.
if (typeof source === 'object' || typeof source === 'function') {
// if this is an alias, check it first because a downstream service
// would have returned the data *already cast* to an alias responseName
const property = source[info.path.key];
if (typeof property === 'function') {
return source[info.fieldName](args, contextValue, info);
}
return property;
}
};
// Converts an hrtime array (as returned from process.hrtime) to nanoseconds.
//
// ONLY CALL THIS ON VALUES REPRESENTING DELTAS, NOT ON THE RAW RETURN VALUE
// FROM process.hrtime() WITH NO ARGUMENTS.
//
// The entire point of the hrtime data structure is that the JavaScript Number
// type can't represent all int64 values without loss of precision:
// Number.MAX_SAFE_INTEGER nanoseconds is about 104 days. Calling this function
// on a duration that represents a value less than 104 days is fine. Calling
// this function on an absolute time (which is generally roughly time since
// system boot) is not a good idea.
//
// XXX We should probably use google.protobuf.Duration on the wire instead of
// ever trying to store durations in a single number.
function durationHrTimeToNanos(hrtime: [number, number]) {
return hrtime[0] * 1e9 + hrtime[1];
}
// Converts a JS Date into a Timestamp.
function dateToProtoTimestamp(date: Date): google.protobuf.Timestamp {
const totalMillis = +date;
const millis = totalMillis % 1000;
return new google.protobuf.Timestamp({
seconds: (totalMillis - millis) / 1000,
nanos: millis * 1e6,
});
}