-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathbuildQueryPlan.ts
991 lines (874 loc) · 29.3 KB
/
buildQueryPlan.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
import { isNotNullOrUndefined } from './utilities/predicates';
import {
DocumentNode,
FragmentDefinitionNode,
getNamedType,
getOperationRootType,
GraphQLCompositeType,
GraphQLError,
GraphQLObjectType,
GraphQLSchema,
GraphQLType,
isAbstractType,
isCompositeType,
isIntrospectionType,
isListType,
isNamedType,
isObjectType,
Kind,
OperationDefinitionNode,
SelectionSetNode,
TypeNameMetaFieldDef,
VariableDefinitionNode,
OperationTypeNode,
print,
stripIgnoredCharacters,
} from 'graphql';
import {
Field,
FieldSet,
groupByResponseName,
matchesField,
selectionSetFromFieldSet,
debugPrintField,
debugPrintFields,
groupByScope,
} from './FieldSet';
import {
FetchNode,
ParallelNode,
PlanNode,
SequenceNode,
QueryPlan,
ResponsePath,
trimSelectionNodes,
} from './QueryPlan';
import { getResponseName } from './utilities/graphql';
import { MultiMap } from './utilities/MultiMap';
import {
getFederationMetadataForType,
getFederationMetadataForField,
} from './composedSchema';
import { DebugLogger } from './utilities/debug';
import { QueryPlanningContext } from './QueryPlanningContext';
import { Scope } from './Scope';
import deepEqual from 'deep-equal';
function stringIsTrue(str?: string) : boolean {
if (!str) {
return false;
}
switch (str.toLocaleLowerCase()) {
case "true":
case "yes":
case "1":
return true;
default:
return false;
}
}
const debug = new DebugLogger(stringIsTrue(process.env.APOLLO_QP_DEBUG));
const typenameField = {
kind: Kind.FIELD,
name: {
kind: Kind.NAME,
value: TypeNameMetaFieldDef.name,
},
};
export type OperationContext = {
schema: GraphQLSchema;
operation: OperationDefinitionNode;
fragments: FragmentMap;
};
export type FragmentMap = { [fragmentName: string]: FragmentDefinitionNode };
export interface BuildQueryPlanOptions {
autoFragmentization: boolean;
}
export function buildQueryPlan(
operationContext: OperationContext,
options: BuildQueryPlanOptions = { autoFragmentization: false },
): QueryPlan {
const context = buildQueryPlanningContext(operationContext, options);
if (context.operation.operation === 'subscription') {
throw new GraphQLError(
'Query planning does not support subscriptions for now.',
[context.operation],
);
}
const rootType = getOperationRootType(context.schema, context.operation);
const isMutation = context.operation.operation === 'mutation';
debug.log(() => `Building plan for ${isMutation ? "mutation" : "query"} "${rootType}" (fragments: [${Object.keys(context.fragments)}], autoFragmentization: ${context.autoFragmentization})`);
debug.group(`Collecting root fields:`);
const fields = context.collectFields(
Scope.create(context, rootType),
context.operation.selectionSet,
);
debug.groupEnd(`Collected root fields:`);
debug.groupedValues(fields, debugPrintField);
debug.group('Splitting root fields:');
// Mutations are a bit more specific in how FetchGroups can be built, as some
// calls to the same service may need to be executed serially.
const groups = isMutation
? splitRootFieldsSerially(context, fields)
: splitRootFields(context, fields);
debug.groupEnd('Computed groups:');
debug.groupedValues(groups, debugPrintGroup);
const nodes = groups.map(group =>
executionNodeForGroup(context, group, rootType),
);
return {
kind: 'QueryPlan',
node: nodes.length
// if an operation is a mutation, we run the root fields in sequence,
// otherwise we run them in parallel
? flatWrap(isMutation ? 'Sequence' : 'Parallel', nodes)
: undefined,
};
}
function executionNodeForGroup(
context: QueryPlanningContext,
{
serviceName,
fields,
requiredFields,
internalFragments,
mergeAt,
dependentGroups,
}: FetchGroup,
parentType?: GraphQLCompositeType,
): PlanNode {
const selectionSet = selectionSetFromFieldSet(fields, parentType);
const requires =
requiredFields.length > 0
? selectionSetFromFieldSet(requiredFields)
: undefined;
const variableUsages = context.getVariableUsages(
selectionSet,
internalFragments,
);
const operation = requires
? operationForEntitiesFetch({
selectionSet,
variableUsages,
internalFragments,
})
: operationForRootFetch({
selectionSet,
variableUsages,
internalFragments,
operation: context.operation.operation,
});
const fetchNode: FetchNode = {
kind: 'Fetch',
serviceName,
requires: requires ? trimSelectionNodes(requires?.selections) : undefined,
variableUsages: Object.keys(variableUsages),
operation: stripIgnoredCharacters(print(operation)),
};
const node: PlanNode =
mergeAt && mergeAt.length > 0
? {
kind: 'Flatten',
path: mergeAt,
node: fetchNode,
}
: fetchNode;
if (dependentGroups.length > 0) {
const dependentNodes = dependentGroups.map(dependentGroup =>
executionNodeForGroup(context, dependentGroup),
);
return flatWrap('Sequence', [node, flatWrap('Parallel', dependentNodes)]);
} else {
return node;
}
}
interface VariableUsages {
[name: string]: VariableDefinitionNode
}
function mapFetchNodeToVariableDefinitions(
variableUsages: VariableUsages,
): VariableDefinitionNode[] {
return variableUsages ? Object.values(variableUsages) : [];
}
function operationForRootFetch({
selectionSet,
variableUsages,
internalFragments,
operation = 'query',
}: {
selectionSet: SelectionSetNode;
variableUsages: VariableUsages;
internalFragments: Set<FragmentDefinitionNode>;
operation?: OperationTypeNode;
}): DocumentNode {
return {
kind: Kind.DOCUMENT,
definitions: [
{
kind: Kind.OPERATION_DEFINITION,
operation,
selectionSet,
variableDefinitions: mapFetchNodeToVariableDefinitions(variableUsages),
},
...internalFragments,
],
};
}
function operationForEntitiesFetch({
selectionSet,
variableUsages,
internalFragments,
}: {
selectionSet: SelectionSetNode;
variableUsages: VariableUsages;
internalFragments: Set<FragmentDefinitionNode>;
}): DocumentNode {
const representationsVariable = {
kind: Kind.VARIABLE,
name: { kind: Kind.NAME, value: 'representations' },
};
return {
kind: Kind.DOCUMENT,
definitions: [
{
kind: Kind.OPERATION_DEFINITION,
operation: 'query',
variableDefinitions: ([
{
kind: Kind.VARIABLE_DEFINITION,
variable: representationsVariable,
type: {
kind: Kind.NON_NULL_TYPE,
type: {
kind: Kind.LIST_TYPE,
type: {
kind: Kind.NON_NULL_TYPE,
type: {
kind: Kind.NAMED_TYPE,
name: { kind: Kind.NAME, value: '_Any' },
},
},
},
},
},
] as VariableDefinitionNode[]).concat(
mapFetchNodeToVariableDefinitions(variableUsages),
),
selectionSet: {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: '_entities' },
arguments: [
{
kind: Kind.ARGUMENT,
name: {
kind: Kind.NAME,
value: representationsVariable.name.value,
},
value: representationsVariable,
},
],
selectionSet,
},
],
},
},
...internalFragments,
],
};
}
// Wraps the given nodes in a ParallelNode or SequenceNode, unless there's only
// one node, in which case it is returned directly. Any nodes of the same kind
// in the given list have their sub-nodes flattened into the list: ie,
// flatWrap('Sequence', [a, flatWrap('Sequence', b, c), d]) returns a SequenceNode
// with four children.
function flatWrap(
kind: ParallelNode['kind'] | SequenceNode['kind'],
nodes: PlanNode[],
): PlanNode {
if (nodes.length === 0) {
throw Error('programming error: should always be called with nodes');
}
if (nodes.length === 1) {
return nodes[0];
}
return {
kind,
nodes: nodes.flatMap(n => (n.kind === kind ? n.nodes : [n])),
} as PlanNode;
}
function splitRootFields(
context: QueryPlanningContext,
fields: FieldSet,
): FetchGroup[] {
const groupsByService: {
[serviceName: string]: FetchGroup;
} = Object.create(null);
function groupForService(serviceName: string) {
let group = groupsByService[serviceName];
if (!group) {
group = new FetchGroup(serviceName);
groupsByService[serviceName] = group;
}
return group;
}
splitFields(context, [], fields, field => {
const { scope, fieldNode, fieldDef } = field;
const { parentType } = scope;
// The root type is necessarily an object type.
const owningService = context.getOwningService(parentType as GraphQLObjectType, fieldDef);
if (!owningService) {
throw new GraphQLError(
`Couldn't find owning service for field "${parentType.name}.${fieldDef.name}"`,
fieldNode,
);
}
return groupForService(owningService);
});
return Object.values(groupsByService);
}
// For mutations, we need to respect the order of the fields, in order to
// determine which fields can be batched together in the same request. If
// they're "split" by fields belonging to other services, then we need to manage
// the proper sequencing at the gateway level. In this example, we need 3
// FetchGroups (requests) in sequence:
//
// mutation abc {
// createReview() # reviews service (1)
// updateReview() # reviews service (1)
// login() # account service (2)
// deleteReview() # reviews service (3)
// }
function splitRootFieldsSerially(
context: QueryPlanningContext,
fields: FieldSet,
): FetchGroup[] {
const fetchGroups: FetchGroup[] = [];
function groupForField(serviceName: string) {
let group: FetchGroup;
// If the most recent FetchGroup in the array belongs to the same service,
// the field in question can be batched within that group.
const previousGroup = fetchGroups[fetchGroups.length - 1];
if (previousGroup && previousGroup.serviceName === serviceName) {
return previousGroup;
}
// If there's no previous group, or the previous group is from a different
// service, then we need to add a new FetchGroup.
group = new FetchGroup(serviceName);
fetchGroups.push(group);
return group;
}
splitFields(context, [], fields, field => {
const { scope, fieldNode, fieldDef } = field;
const { parentType } = scope;
// The root type is necessarily an object type.
const owningService = context.getOwningService(parentType as GraphQLObjectType, fieldDef);
if (!owningService) {
throw new GraphQLError(
`Couldn't find owning service for field "${parentType.name}.${fieldDef.name}"`,
fieldNode,
);
}
return groupForField(owningService);
});
return fetchGroups;
}
function splitSubfields(
context: QueryPlanningContext,
path: ResponsePath,
fields: FieldSet,
parentGroup: FetchGroup,
) {
splitFields(context, path, fields, field => {
const { scope, fieldNode, fieldDef } = field;
const { parentType } = scope;
let baseService, owningService;
// Committed by @trevor-scheer but authored by @martijnwalraven
// Treat abstract types as value types to replicate type explosion fix
// XXX: this replicates the behavior of the Rust query planner implementation,
// in order to get the tests passing before making further changes. But the
// type explosion fix this depends on is fundamentally flawed and needs to
// be replaced.
if (!isObjectType(parentType) || getFederationMetadataForType(parentType)?.isValueType) {
baseService = parentGroup.serviceName;
owningService = parentGroup.serviceName;
} else {
baseService = context.getBaseService(parentType);
owningService = context.getOwningService(parentType, fieldDef);
}
if (!baseService) {
throw new GraphQLError(
`Couldn't find base service for type "${parentType.name}"`,
fieldNode,
);
}
if (!owningService) {
throw new GraphQLError(
`Couldn't find owning service for field "${parentType.name}.${fieldDef.name}"`,
fieldNode,
);
}
// Is the field defined on the base service?
if (owningService === baseService) {
// Can we fetch the field from the parent group?
if (
owningService === parentGroup.serviceName ||
parentGroup.providedFields.some(matchesField(field))
) {
return parentGroup;
} else {
// We need to fetch the key fields from the parent group first, and then
// use a dependent fetch from the owning service.
let keyFields = context.getKeyFields(scope, parentGroup.serviceName);
if (
keyFields.length === 0 ||
(keyFields.length === 1 &&
keyFields[0].fieldDef.name === '__typename')
) {
// Only __typename key found.
// In some cases, the parent group does not have any @key directives.
// Fall back to owning group's keys
keyFields = context.getKeyFields(scope, owningService);
}
return parentGroup.dependentGroupForService(owningService, keyFields);
}
} else {
// It's an extension field, so we need to fetch the required fields first.
const requiredFields = context.getRequiredFields(
scope,
fieldDef,
owningService,
);
// Can we fetch the required fields from the parent group?
if (
requiredFields.every(requiredField =>
parentGroup.providedFields.some(matchesField(requiredField)),
)
) {
if (owningService === parentGroup.serviceName) {
return parentGroup;
} else {
return parentGroup.dependentGroupForService(
owningService,
requiredFields,
);
}
} else {
// We need to go through the base group first.
const keyFields = context.getKeyFields(scope, parentGroup.serviceName);
if (!keyFields) {
throw new GraphQLError(
`Couldn't find keys for type "${parentType.name}}" in service "${baseService}"`,
fieldNode,
);
}
if (baseService === parentGroup.serviceName) {
return parentGroup.dependentGroupForService(
owningService,
requiredFields,
);
}
const baseGroup = parentGroup.dependentGroupForService(
baseService,
keyFields,
);
return baseGroup.dependentGroupForService(
owningService,
requiredFields,
);
}
}
});
}
function splitFields(
context: QueryPlanningContext,
path: ResponsePath,
fields: FieldSet,
groupForField: (field: Field) => FetchGroup,
) {
for (const fieldsForResponseName of groupByResponseName(fields).values()) {
for (const fieldsForScope of groupByScope(fieldsForResponseName).values()) {
// Field nodes that share the same response name and scope are guaranteed to have the same field name and
// arguments. We only need the other nodes when merging selection sets, to take node-specific subfields and
// directives into account.
debug.group(() => debugPrintFields(fieldsForScope));
// All the fields in fieldsForScope have the same scope, so that means the same parent type and possible runtime
// types, so we effectively can just use the first one and ignore the rest.
const field = fieldsForScope[0];
const { scope, fieldDef } = field;
const parentType = scope.parentType;
// We skip `__typename` for root types.
if (fieldDef.name === TypeNameMetaFieldDef.name) {
const { schema } = context;
const roots = [
schema.getQueryType(),
schema.getMutationType(),
schema.getSubscriptionType(),
]
.filter(isNotNullOrUndefined)
.map(type => type.name);
if (roots.indexOf(parentType.name) > -1) {
debug.groupEnd("Skipping __typename for root types");
continue;
}
}
// We skip introspection fields like `__schema` and `__type`.
if (isIntrospectionType(getNamedType(fieldDef.type))) {
debug.groupEnd(`Skipping introspection type ${fieldDef.type}`);
continue;
}
if (isObjectType(parentType) && scope.possibleRuntimeTypes().includes(parentType)) {
// If parent type is an object type, we can directly look for the right
// group.
debug.log(() => `${parentType} = object and ${parentType} ∈ [${scope.possibleRuntimeTypes()}]`);
const group = groupForField(field);
debug.log(() => `Initial fetch group for fields: ${debugPrintGroup(group)}`);
group.fields.push(completeField(context, scope, group, path, fieldsForScope));
debug.groupEnd(() => `Updated fetch group: ${debugPrintGroup(group)}`);
} else {
debug.log(() => `${parentType} ≠ object or ${parentType} ∉ [${scope.possibleRuntimeTypes()}]`);
// For interfaces however, we need to look at all possible runtime types.
/**
* The following is an optimization to prevent an explosion of type
* conditions to services when it isn't needed. If all possible runtime
* types can be fufilled by only one service then we don't need to
* expand the fields into unique type conditions.
*/
// Collect all of the field defs on the possible runtime types
const possibleFieldDefs = scope.possibleRuntimeTypes().map(
runtimeType => context.getFieldDef(runtimeType, field.fieldNode),
);
// If none of the field defs have a federation property, this interface's
// implementors can all be resolved within the same service.
const hasNoExtendingFieldDefs = !possibleFieldDefs.some(
(field) => getFederationMetadataForField(field)?.graphName,
);
// With no extending field definitions, we can engage the optimization
if (hasNoExtendingFieldDefs) {
debug.group(() => `No field of ${scope.possibleRuntimeTypes()} have federation directives, avoid type explosion.`);
const group = groupForField(field);
debug.groupEnd(() => `Initial fetch group for fields: ${debugPrintGroup(group)}`);
group.fields.push(completeField(context, scope, group, path, fieldsForScope));
debug.groupEnd(() => `Updated fetch group: ${debugPrintGroup(group)}`);
continue;
}
// We keep track of which possible runtime parent types can be fetched
// from which group,
const groupsByRuntimeParentTypes = new MultiMap<
FetchGroup,
GraphQLObjectType
>();
debug.group('Computing fetch groups by runtime parent types');
for (const runtimeParentType of scope.possibleRuntimeTypes()) {
const fieldDef = context.getFieldDef(
runtimeParentType,
field.fieldNode,
);
groupsByRuntimeParentTypes.add(
groupForField({
scope: scope.refine(runtimeParentType),
fieldNode: field.fieldNode,
fieldDef,
}),
runtimeParentType,
);
}
debug.groupEnd(`Fetch groups to resolvable runtime types:`);
debug.groupedEntries(groupsByRuntimeParentTypes, debugPrintGroup, (v) => v.toString());
debug.group('Iterating on fetch groups');
// We add the field separately for each runtime parent type.
for (const [group, runtimeParentTypes] of groupsByRuntimeParentTypes) {
debug.group(() => `For initial fetch group ${debugPrintGroup(group)}:`);
for (const runtimeParentType of runtimeParentTypes) {
// We need to adjust the fields to contain the right fieldDef for
// their runtime parent type.
debug.group(`For runtime parent type ${runtimeParentType}:`);
const fieldDef = context.getFieldDef(
runtimeParentType,
field.fieldNode,
);
const fieldsWithRuntimeParentType = fieldsForScope.map(field => ({
...field,
fieldDef,
}));
group.fields.push(
completeField(
context,
scope.refine(runtimeParentType),
group,
path,
fieldsWithRuntimeParentType,
),
);
debug.groupEnd(() => `Updated fetch group: ${debugPrintGroup(group)}`);
}
debug.groupEnd();
}
debug.groupEnd(); // Group started before the immediate for loop
debug.groupEnd(); // Group started at the beginning of this 'top-level' iteration.
}
}
}
}
function completeField(
context: QueryPlanningContext,
scope: Scope,
parentGroup: FetchGroup,
path: ResponsePath,
fields: FieldSet,
): Field {
const { fieldNode, fieldDef } = fields[0];
const returnType = getNamedType(fieldDef.type);
if (!isCompositeType(returnType)) {
// FIXME: We should look at all field nodes to make sure we take directives
// into account (or remove directives for the time being).
return { scope, fieldNode, fieldDef };
} else {
// For composite types, we need to recurse.
const fieldPath = addPath(path, getResponseName(fieldNode), fieldDef.type);
const subGroup = new FetchGroup(parentGroup.serviceName);
subGroup.mergeAt = fieldPath;
subGroup.providedFields = context.getProvidedFields(
fieldDef,
parentGroup.serviceName,
);
// For abstract types, we always need to request `__typename`
if (isAbstractType(returnType)) {
subGroup.fields.push({
scope: Scope.create(context, returnType),
fieldNode: typenameField,
fieldDef: TypeNameMetaFieldDef,
});
}
const subfields = collectSubfields(context, returnType, fields);
debug.group(() => `Splitting collected sub-fields (${debugPrintFields(subfields)})`);
splitSubfields(context, fieldPath, subfields, subGroup);
debug.groupEnd();
// Because of the way we split fields, we may have created multiple
// dependent groups to the same subgraph for the same path. We therefore
// attempt to merge dependent groups of the subgroup and of the parent group
// to avoid duplicate fetches.
parentGroup.mergeDependentGroups(subGroup);
let definition: FragmentDefinitionNode;
let selectionSet = selectionSetFromFieldSet(subGroup.fields, returnType);
if (context.autoFragmentization && subGroup.fields.length > 2) {
({ definition, selectionSet } = getInternalFragment(
selectionSet,
returnType,
context,
));
parentGroup.internalFragments.add(definition);
}
// "Hoist" internalFragments of the subGroup into the parentGroup so all
// fragments can be included in the final request for the root FetchGroup
subGroup.internalFragments.forEach(fragment => {
parentGroup.internalFragments.add(fragment);
});
return {
scope,
fieldNode: {
...fieldNode,
selectionSet,
},
fieldDef,
};
}
}
function getInternalFragment(
selectionSet: SelectionSetNode,
returnType: GraphQLCompositeType,
context: QueryPlanningContext
) {
const key = JSON.stringify(selectionSet);
if (!context.internalFragments.has(key)) {
const name = `__QueryPlanFragment_${context.internalFragmentCount++}__`;
const definition: FragmentDefinitionNode = {
kind: Kind.FRAGMENT_DEFINITION,
name: {
kind: Kind.NAME,
value: name,
},
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: returnType.name,
},
},
selectionSet,
};
const fragmentSelection: SelectionSetNode = {
kind: Kind.SELECTION_SET,
selections: [
{
kind: Kind.FRAGMENT_SPREAD,
name: {
kind: Kind.NAME,
value: name,
},
},
],
};
context.internalFragments.set(key, {
name,
definition,
selectionSet: fragmentSelection,
});
}
return context.internalFragments.get(key)!;
}
// Collecting subfields collapses parent types, because it merges
// selection sets without taking the runtime parent type of the field
// into account. If we want to keep track of multiple levels of possible
// types, this is where that would need to happen.
export function collectSubfields(
context: QueryPlanningContext,
returnType: GraphQLCompositeType,
fields: FieldSet,
): FieldSet {
let subfields: FieldSet = [];
for (const field of fields) {
const selectionSet = field.fieldNode.selectionSet;
if (selectionSet) {
subfields = context.collectFields(
Scope.create(context, returnType),
selectionSet,
subfields,
);
}
}
return subfields;
}
class FetchGroup {
constructor(
public readonly serviceName: string,
public readonly fields: FieldSet = [],
public readonly internalFragments: Set<FragmentDefinitionNode> = new Set()
) {}
requiredFields: FieldSet = [];
providedFields: FieldSet = [];
mergeAt?: ResponsePath;
private dependentGroupsByService: {
[serviceName: string]: FetchGroup;
} = Object.create(null);
public otherDependentGroups: FetchGroup[] = [];
dependentGroupForService(serviceName: string, requiredFields: FieldSet) {
let group = this.dependentGroupsByService[serviceName];
if (!group) {
group = new FetchGroup(serviceName);
group.mergeAt = this.mergeAt;
this.dependentGroupsByService[serviceName] = group;
}
if (requiredFields) {
if (group.requiredFields) {
group.requiredFields.push(...requiredFields);
} else {
group.requiredFields = requiredFields;
}
this.fields.push(...requiredFields);
}
return group;
}
get dependentGroups(): FetchGroup[] {
return [
...Object.values(this.dependentGroupsByService),
...this.otherDependentGroups,
];
}
mergeDependentGroups(that: FetchGroup) {
for (const dependentGroup of that.dependentGroups) {
// In order to avoid duplicate fetches, we try to find existing dependent
// groups with the same service and merge path first.
const existingDependentGroup = this.dependentGroups.find(
(group) =>
group.serviceName === dependentGroup.serviceName &&
deepEqual(group.mergeAt, dependentGroup.mergeAt),
);
if (existingDependentGroup) {
existingDependentGroup.merge(dependentGroup);
} else {
this.otherDependentGroups.push(dependentGroup);
}
}
}
merge(otherGroup: FetchGroup) {
this.fields.push(...otherGroup.fields);
this.requiredFields.push(...otherGroup.requiredFields);
this.providedFields.push(...otherGroup.providedFields);
this.mergeDependentGroups(otherGroup);
}
}
// Provides a string representation of a `FetchGroup` suitable for debugging.
function debugPrintGroup(group: FetchGroup): string {
let str = `Fetch(${group.serviceName}, ${debugPrintFields(group.fields)}`;
if (group.dependentGroups.length !== 0) {
str += `, deps: ${debugPrintGroups(group.dependentGroups)}`
}
return str + ')';
}
// Provides a string representation of an array of `FetchGroup` suitable for debugging.
function debugPrintGroups(groups: FetchGroup[]): string {
return '[' + groups.map(debugPrintGroup).join(', ') + ']'
}
// Adapted from buildExecutionContext in graphql-js
export function buildOperationContext(
schema: GraphQLSchema,
document: DocumentNode,
operationName?: string,
): OperationContext {
let operation: OperationDefinitionNode | undefined;
const fragments: {
[fragmentName: string]: FragmentDefinitionNode;
} = Object.create(null);
document.definitions.forEach(definition => {
switch (definition.kind) {
case Kind.OPERATION_DEFINITION:
if (!operationName && operation) {
throw new GraphQLError(
'Must provide operation name if query contains ' +
'multiple operations.',
);
}
if (
!operationName ||
(definition.name && definition.name.value === operationName)
) {
operation = definition;
}
break;
case Kind.FRAGMENT_DEFINITION:
fragments[definition.name.value] = definition;
break;
}
});
if (!operation) {
if (operationName) {
throw new GraphQLError(`Unknown operation named "${operationName}".`);
} else {
throw new GraphQLError('Must provide an operation.');
}
}
return { schema, operation, fragments };
}
export function buildQueryPlanningContext(
{ operation, schema, fragments }: OperationContext,
options: BuildQueryPlanOptions,
): QueryPlanningContext {
return new QueryPlanningContext(
schema,
operation,
fragments,
options.autoFragmentization,
);
}
function addPath(path: ResponsePath, responseName: string, type: GraphQLType) {
path = [...path, responseName];
while (!isNamedType(type)) {
if (isListType(type)) {
path.push('@');
}
type = type.ofType;
}
return path;
}