@graphql-tools/executor 1.2.6 → 2.0.0-alpha-20240606221026-cd2a4fabe51906319f8dc07745f98f37ffbcbdee
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/execution/AccumulatorMap.js +21 -0
- package/cjs/execution/BoxedPromiseOrValue.js +25 -0
- package/cjs/execution/IncrementalGraph.js +271 -0
- package/cjs/execution/IncrementalPublisher.js +274 -0
- package/cjs/execution/buildFieldPlan.js +62 -0
- package/cjs/execution/collectFields.js +174 -0
- package/cjs/execution/execute.js +548 -513
- package/cjs/execution/getBySet.js +13 -0
- package/cjs/execution/isSameSet.js +15 -0
- package/cjs/execution/promiseWithResolvers.js +18 -0
- package/cjs/execution/types.js +19 -0
- package/esm/execution/AccumulatorMap.js +17 -0
- package/esm/execution/BoxedPromiseOrValue.js +21 -0
- package/esm/execution/IncrementalGraph.js +267 -0
- package/esm/execution/IncrementalPublisher.js +270 -0
- package/esm/execution/buildFieldPlan.js +58 -0
- package/esm/execution/collectFields.js +169 -0
- package/esm/execution/execute.js +549 -514
- package/esm/execution/getBySet.js +9 -0
- package/esm/execution/isSameSet.js +11 -0
- package/esm/execution/promiseWithResolvers.js +14 -0
- package/esm/execution/types.js +12 -0
- package/package.json +2 -2
- package/typings/execution/AccumulatorMap.d.cts +7 -0
- package/typings/execution/AccumulatorMap.d.ts +7 -0
- package/typings/execution/BoxedPromiseOrValue.d.cts +15 -0
- package/typings/execution/BoxedPromiseOrValue.d.ts +15 -0
- package/typings/execution/IncrementalGraph.d.cts +32 -0
- package/typings/execution/IncrementalGraph.d.ts +32 -0
- package/typings/execution/IncrementalPublisher.d.cts +8 -0
- package/typings/execution/IncrementalPublisher.d.ts +8 -0
- package/typings/execution/buildFieldPlan.d.cts +7 -0
- package/typings/execution/buildFieldPlan.d.ts +7 -0
- package/typings/execution/collectFields.d.cts +40 -0
- package/typings/execution/collectFields.d.ts +40 -0
- package/typings/execution/execute.d.cts +8 -106
- package/typings/execution/execute.d.ts +8 -106
- package/typings/execution/getBySet.d.cts +1 -0
- package/typings/execution/getBySet.d.ts +1 -0
- package/typings/execution/isSameSet.d.cts +1 -0
- package/typings/execution/isSameSet.d.ts +1 -0
- package/typings/execution/promiseWithResolvers.d.cts +10 -0
- package/typings/execution/promiseWithResolvers.d.ts +10 -0
- package/typings/execution/types.d.cts +155 -0
- package/typings/execution/types.d.ts +155 -0
- package/cjs/execution/flattenAsyncIterable.js +0 -89
- package/esm/execution/flattenAsyncIterable.js +0 -85
- package/typings/execution/flattenAsyncIterable.d.cts +0 -7
- package/typings/execution/flattenAsyncIterable.d.ts +0 -7
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.collectSubfields = exports.collectFields = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
6
|
+
const AccumulatorMap_js_1 = require("./AccumulatorMap.js");
|
|
7
|
+
const invariant_js_1 = require("./invariant.js");
|
|
8
|
+
/**
|
|
9
|
+
* Given a selectionSet, collects all of the fields and returns them.
|
|
10
|
+
*
|
|
11
|
+
* CollectFields requires the "runtime type" of an object. For a field that
|
|
12
|
+
* returns an Interface or Union type, the "runtime type" will be the actual
|
|
13
|
+
* object type returned by that field.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
function collectFields(schema, fragments, variableValues, runtimeType, operation) {
|
|
18
|
+
const groupedFieldSet = new AccumulatorMap_js_1.AccumulatorMap();
|
|
19
|
+
const newDeferUsages = [];
|
|
20
|
+
const context = {
|
|
21
|
+
schema,
|
|
22
|
+
fragments,
|
|
23
|
+
variableValues,
|
|
24
|
+
runtimeType,
|
|
25
|
+
operation,
|
|
26
|
+
visitedFragmentNames: new Set(),
|
|
27
|
+
};
|
|
28
|
+
collectFieldsImpl(context, operation.selectionSet, groupedFieldSet, newDeferUsages);
|
|
29
|
+
return { groupedFieldSet, newDeferUsages };
|
|
30
|
+
}
|
|
31
|
+
exports.collectFields = collectFields;
|
|
32
|
+
/**
|
|
33
|
+
* Given an array of field nodes, collects all of the subfields of the passed
|
|
34
|
+
* in fields, and returns them at the end.
|
|
35
|
+
*
|
|
36
|
+
* CollectSubFields requires the "return type" of an object. For a field that
|
|
37
|
+
* returns an Interface or Union type, the "return type" will be the actual
|
|
38
|
+
* object type returned by that field.
|
|
39
|
+
*
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
function collectSubfields(schema, fragments, variableValues, operation, returnType, fieldGroup) {
|
|
43
|
+
const context = {
|
|
44
|
+
schema,
|
|
45
|
+
fragments,
|
|
46
|
+
variableValues,
|
|
47
|
+
runtimeType: returnType,
|
|
48
|
+
operation,
|
|
49
|
+
visitedFragmentNames: new Set(),
|
|
50
|
+
};
|
|
51
|
+
const subGroupedFieldSet = new AccumulatorMap_js_1.AccumulatorMap();
|
|
52
|
+
const newDeferUsages = [];
|
|
53
|
+
for (const fieldDetail of fieldGroup) {
|
|
54
|
+
const node = fieldDetail.node;
|
|
55
|
+
if (node.selectionSet) {
|
|
56
|
+
collectFieldsImpl(context, node.selectionSet, subGroupedFieldSet, newDeferUsages, fieldDetail.deferUsage);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
groupedFieldSet: subGroupedFieldSet,
|
|
61
|
+
newDeferUsages,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
exports.collectSubfields = collectSubfields;
|
|
65
|
+
function collectFieldsImpl(context, selectionSet, groupedFieldSet, newDeferUsages, deferUsage) {
|
|
66
|
+
const { schema, fragments, variableValues, runtimeType, operation, visitedFragmentNames } = context;
|
|
67
|
+
for (const selection of selectionSet.selections) {
|
|
68
|
+
switch (selection.kind) {
|
|
69
|
+
case graphql_1.Kind.FIELD: {
|
|
70
|
+
if (!shouldIncludeNode(variableValues, selection)) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
groupedFieldSet.add(getFieldEntryKey(selection), {
|
|
74
|
+
node: selection,
|
|
75
|
+
deferUsage,
|
|
76
|
+
});
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case graphql_1.Kind.INLINE_FRAGMENT: {
|
|
80
|
+
if (!shouldIncludeNode(variableValues, selection) ||
|
|
81
|
+
!doesFragmentConditionMatch(schema, selection, runtimeType)) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const newDeferUsage = getDeferUsage(operation, variableValues, selection, deferUsage);
|
|
85
|
+
if (!newDeferUsage) {
|
|
86
|
+
collectFieldsImpl(context, selection.selectionSet, groupedFieldSet, newDeferUsages, deferUsage);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
newDeferUsages.push(newDeferUsage);
|
|
90
|
+
collectFieldsImpl(context, selection.selectionSet, groupedFieldSet, newDeferUsages, newDeferUsage);
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case graphql_1.Kind.FRAGMENT_SPREAD: {
|
|
95
|
+
const fragName = selection.name.value;
|
|
96
|
+
const newDeferUsage = getDeferUsage(operation, variableValues, selection, deferUsage);
|
|
97
|
+
if (!newDeferUsage &&
|
|
98
|
+
(visitedFragmentNames.has(fragName) || !shouldIncludeNode(variableValues, selection))) {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
const fragment = fragments[fragName];
|
|
102
|
+
if (fragment == null || !doesFragmentConditionMatch(schema, fragment, runtimeType)) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!newDeferUsage) {
|
|
106
|
+
visitedFragmentNames.add(fragName);
|
|
107
|
+
collectFieldsImpl(context, fragment.selectionSet, groupedFieldSet, newDeferUsages, deferUsage);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
newDeferUsages.push(newDeferUsage);
|
|
111
|
+
collectFieldsImpl(context, fragment.selectionSet, groupedFieldSet, newDeferUsages, newDeferUsage);
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Returns an object containing the `@defer` arguments if a field should be
|
|
120
|
+
* deferred based on the experimental flag, defer directive present and
|
|
121
|
+
* not disabled by the "if" argument.
|
|
122
|
+
*/
|
|
123
|
+
function getDeferUsage(operation, variableValues, node, parentDeferUsage) {
|
|
124
|
+
const defer = (0, graphql_1.getDirectiveValues)(utils_1.GraphQLDeferDirective, node, variableValues);
|
|
125
|
+
if (!defer) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (defer['if'] === false) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
(0, invariant_js_1.invariant)(operation.operation !== 'subscription', '`@defer` directive not supported on subscription operations. Disable `@defer` by setting the `if` argument to `false`.');
|
|
132
|
+
return {
|
|
133
|
+
label: typeof defer['label'] === 'string' ? defer['label'] : undefined,
|
|
134
|
+
parentDeferUsage,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Determines if a field should be included based on the `@include` and `@skip`
|
|
139
|
+
* directives, where `@skip` has higher precedence than `@include`.
|
|
140
|
+
*/
|
|
141
|
+
function shouldIncludeNode(variableValues, node) {
|
|
142
|
+
const skip = (0, graphql_1.getDirectiveValues)(graphql_1.GraphQLSkipDirective, node, variableValues);
|
|
143
|
+
if (skip?.['if'] === true) {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
const include = (0, graphql_1.getDirectiveValues)(graphql_1.GraphQLIncludeDirective, node, variableValues);
|
|
147
|
+
if (include?.['if'] === false) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Determines if a fragment is applicable to the given type.
|
|
154
|
+
*/
|
|
155
|
+
function doesFragmentConditionMatch(schema, fragment, type) {
|
|
156
|
+
const typeConditionNode = fragment.typeCondition;
|
|
157
|
+
if (!typeConditionNode) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
const conditionalType = (0, graphql_1.typeFromAST)(schema, typeConditionNode);
|
|
161
|
+
if (conditionalType === type) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
if ((0, graphql_1.isAbstractType)(conditionalType)) {
|
|
165
|
+
return schema.isSubType(conditionalType, type);
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Implements the logic to compute the key of a given field's entry
|
|
171
|
+
*/
|
|
172
|
+
function getFieldEntryKey(node) {
|
|
173
|
+
return node.alias ? node.alias.value : node.name.value;
|
|
174
|
+
}
|