@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.
Files changed (49) hide show
  1. package/cjs/execution/AccumulatorMap.js +21 -0
  2. package/cjs/execution/BoxedPromiseOrValue.js +25 -0
  3. package/cjs/execution/IncrementalGraph.js +271 -0
  4. package/cjs/execution/IncrementalPublisher.js +274 -0
  5. package/cjs/execution/buildFieldPlan.js +62 -0
  6. package/cjs/execution/collectFields.js +174 -0
  7. package/cjs/execution/execute.js +548 -513
  8. package/cjs/execution/getBySet.js +13 -0
  9. package/cjs/execution/isSameSet.js +15 -0
  10. package/cjs/execution/promiseWithResolvers.js +18 -0
  11. package/cjs/execution/types.js +19 -0
  12. package/esm/execution/AccumulatorMap.js +17 -0
  13. package/esm/execution/BoxedPromiseOrValue.js +21 -0
  14. package/esm/execution/IncrementalGraph.js +267 -0
  15. package/esm/execution/IncrementalPublisher.js +270 -0
  16. package/esm/execution/buildFieldPlan.js +58 -0
  17. package/esm/execution/collectFields.js +169 -0
  18. package/esm/execution/execute.js +549 -514
  19. package/esm/execution/getBySet.js +9 -0
  20. package/esm/execution/isSameSet.js +11 -0
  21. package/esm/execution/promiseWithResolvers.js +14 -0
  22. package/esm/execution/types.js +12 -0
  23. package/package.json +2 -2
  24. package/typings/execution/AccumulatorMap.d.cts +7 -0
  25. package/typings/execution/AccumulatorMap.d.ts +7 -0
  26. package/typings/execution/BoxedPromiseOrValue.d.cts +15 -0
  27. package/typings/execution/BoxedPromiseOrValue.d.ts +15 -0
  28. package/typings/execution/IncrementalGraph.d.cts +32 -0
  29. package/typings/execution/IncrementalGraph.d.ts +32 -0
  30. package/typings/execution/IncrementalPublisher.d.cts +8 -0
  31. package/typings/execution/IncrementalPublisher.d.ts +8 -0
  32. package/typings/execution/buildFieldPlan.d.cts +7 -0
  33. package/typings/execution/buildFieldPlan.d.ts +7 -0
  34. package/typings/execution/collectFields.d.cts +40 -0
  35. package/typings/execution/collectFields.d.ts +40 -0
  36. package/typings/execution/execute.d.cts +8 -106
  37. package/typings/execution/execute.d.ts +8 -106
  38. package/typings/execution/getBySet.d.cts +1 -0
  39. package/typings/execution/getBySet.d.ts +1 -0
  40. package/typings/execution/isSameSet.d.cts +1 -0
  41. package/typings/execution/isSameSet.d.ts +1 -0
  42. package/typings/execution/promiseWithResolvers.d.cts +10 -0
  43. package/typings/execution/promiseWithResolvers.d.ts +10 -0
  44. package/typings/execution/types.d.cts +155 -0
  45. package/typings/execution/types.d.ts +155 -0
  46. package/cjs/execution/flattenAsyncIterable.js +0 -89
  47. package/esm/execution/flattenAsyncIterable.js +0 -85
  48. package/typings/execution/flattenAsyncIterable.d.cts +0 -7
  49. 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
+ }