@graphql-tools/executor 0.0.1 → 0.0.2-alpha-20221029152711-14f4f7a7

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 (45) hide show
  1. package/cjs/directives/defer.js +23 -0
  2. package/cjs/directives/index.js +5 -0
  3. package/cjs/directives/stream.js +28 -0
  4. package/cjs/execution/AccumulatorMap.js +21 -0
  5. package/cjs/execution/collectFields.js +126 -0
  6. package/cjs/execution/execute.js +656 -109
  7. package/cjs/execution/flattenAsyncIterable.js +91 -0
  8. package/cjs/execution/invariant.js +9 -0
  9. package/cjs/execution/promiseForObject.js +21 -0
  10. package/cjs/index.js +1 -0
  11. package/esm/directives/defer.js +20 -0
  12. package/esm/directives/index.js +2 -0
  13. package/esm/directives/stream.js +25 -0
  14. package/esm/execution/AccumulatorMap.js +17 -0
  15. package/esm/execution/collectFields.js +122 -0
  16. package/esm/execution/execute.js +653 -108
  17. package/esm/execution/flattenAsyncIterable.js +87 -0
  18. package/esm/execution/invariant.js +5 -0
  19. package/esm/execution/promiseForObject.js +17 -0
  20. package/esm/index.js +1 -0
  21. package/package.json +2 -2
  22. package/typings/directives/defer.d.cts +5 -0
  23. package/typings/directives/defer.d.ts +5 -0
  24. package/typings/directives/index.d.cts +2 -0
  25. package/typings/directives/index.d.ts +2 -0
  26. package/typings/directives/stream.d.cts +5 -0
  27. package/typings/directives/stream.d.ts +5 -0
  28. package/typings/execution/AccumulatorMap.d.cts +7 -0
  29. package/typings/execution/AccumulatorMap.d.ts +7 -0
  30. package/typings/execution/collectFields.d.cts +32 -0
  31. package/typings/execution/collectFields.d.ts +32 -0
  32. package/typings/execution/execute.d.cts +167 -22
  33. package/typings/execution/execute.d.ts +167 -22
  34. package/typings/execution/flattenAsyncIterable.d.cts +7 -0
  35. package/typings/execution/flattenAsyncIterable.d.ts +7 -0
  36. package/typings/execution/invariant.d.cts +1 -0
  37. package/typings/execution/invariant.d.ts +1 -0
  38. package/typings/execution/promiseForObject.d.cts +12 -0
  39. package/typings/execution/promiseForObject.d.ts +12 -0
  40. package/typings/index.d.cts +1 -0
  41. package/typings/index.d.ts +1 -0
  42. package/cjs/execution/subscribe.js +0 -158
  43. package/esm/execution/subscribe.js +0 -153
  44. package/typings/execution/subscribe.d.cts +0 -59
  45. package/typings/execution/subscribe.d.ts +0 -59
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GraphQLDeferDirective = void 0;
4
+ const graphql_1 = require("graphql");
5
+ /**
6
+ * Used to conditionally defer fragments.
7
+ */
8
+ exports.GraphQLDeferDirective = new graphql_1.GraphQLDirective({
9
+ name: 'defer',
10
+ description: 'Directs the executor to defer this fragment when the `if` argument is true or undefined.',
11
+ locations: [graphql_1.DirectiveLocation.FRAGMENT_SPREAD, graphql_1.DirectiveLocation.INLINE_FRAGMENT],
12
+ args: {
13
+ if: {
14
+ type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLBoolean),
15
+ description: 'Deferred when true or undefined.',
16
+ defaultValue: true,
17
+ },
18
+ label: {
19
+ type: graphql_1.GraphQLString,
20
+ description: 'Unique name',
21
+ },
22
+ },
23
+ });
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./defer.js"), exports);
5
+ tslib_1.__exportStar(require("./stream.js"), exports);
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GraphQLStreamDirective = void 0;
4
+ const graphql_1 = require("graphql");
5
+ /**
6
+ * Used to conditionally stream list fields.
7
+ */
8
+ exports.GraphQLStreamDirective = new graphql_1.GraphQLDirective({
9
+ name: 'stream',
10
+ description: 'Directs the executor to stream plural fields when the `if` argument is true or undefined.',
11
+ locations: [graphql_1.DirectiveLocation.FIELD],
12
+ args: {
13
+ if: {
14
+ type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLBoolean),
15
+ description: 'Stream when true or undefined.',
16
+ defaultValue: true,
17
+ },
18
+ label: {
19
+ type: graphql_1.GraphQLString,
20
+ description: 'Unique name',
21
+ },
22
+ initialCount: {
23
+ defaultValue: 0,
24
+ type: graphql_1.GraphQLInt,
25
+ description: 'Number of items to return immediately',
26
+ },
27
+ },
28
+ });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AccumulatorMap = void 0;
4
+ /**
5
+ * ES6 Map with additional `add` method to accumulate items.
6
+ */
7
+ class AccumulatorMap extends Map {
8
+ get [Symbol.toStringTag]() {
9
+ return 'AccumulatorMap';
10
+ }
11
+ add(key, item) {
12
+ const group = this.get(key);
13
+ if (group === undefined) {
14
+ this.set(key, [item]);
15
+ }
16
+ else {
17
+ group.push(item);
18
+ }
19
+ }
20
+ }
21
+ exports.AccumulatorMap = AccumulatorMap;
@@ -0,0 +1,126 @@
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_1 = require("./AccumulatorMap");
7
+ const directives_1 = require("../directives");
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, selectionSet) {
18
+ const fields = new AccumulatorMap_1.AccumulatorMap();
19
+ const patches = [];
20
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, patches, new Set());
21
+ return { fields, patches };
22
+ }
23
+ exports.collectFields = collectFields;
24
+ /**
25
+ * Given an array of field nodes, collects all of the subfields of the passed
26
+ * in fields, and returns them at the end.
27
+ *
28
+ * CollectSubFields requires the "return type" of an object. For a field that
29
+ * returns an Interface or Union type, the "return type" will be the actual
30
+ * object type returned by that field.
31
+ *
32
+ * @internal
33
+ */
34
+ exports.collectSubfields = (0, utils_1.memoize5)(function collectSubfields(schema, fragments, variableValues, returnType, fieldNodes) {
35
+ const subFieldNodes = new AccumulatorMap_1.AccumulatorMap();
36
+ const visitedFragmentNames = new Set();
37
+ const subPatches = [];
38
+ const subFieldsAndPatches = {
39
+ fields: subFieldNodes,
40
+ patches: subPatches,
41
+ };
42
+ for (const node of fieldNodes) {
43
+ if (node.selectionSet) {
44
+ collectFieldsImpl(schema, fragments, variableValues, returnType, node.selectionSet, subFieldNodes, subPatches, visitedFragmentNames);
45
+ }
46
+ }
47
+ return subFieldsAndPatches;
48
+ });
49
+ function collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, patches, visitedFragmentNames) {
50
+ for (const selection of selectionSet.selections) {
51
+ switch (selection.kind) {
52
+ case graphql_1.Kind.FIELD: {
53
+ if (!(0, utils_1.shouldIncludeNode)(variableValues, selection)) {
54
+ continue;
55
+ }
56
+ fields.add((0, utils_1.getFieldEntryKey)(selection), selection);
57
+ break;
58
+ }
59
+ case graphql_1.Kind.INLINE_FRAGMENT: {
60
+ if (!(0, utils_1.shouldIncludeNode)(variableValues, selection) ||
61
+ !(0, utils_1.doesFragmentConditionMatch)(schema, selection, runtimeType)) {
62
+ continue;
63
+ }
64
+ const defer = getDeferValues(variableValues, selection);
65
+ if (defer) {
66
+ const patchFields = new AccumulatorMap_1.AccumulatorMap();
67
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, patchFields, patches, visitedFragmentNames);
68
+ patches.push({
69
+ label: defer.label,
70
+ fields: patchFields,
71
+ });
72
+ }
73
+ else {
74
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, fields, patches, visitedFragmentNames);
75
+ }
76
+ break;
77
+ }
78
+ case graphql_1.Kind.FRAGMENT_SPREAD: {
79
+ const fragName = selection.name.value;
80
+ if (!(0, utils_1.shouldIncludeNode)(variableValues, selection)) {
81
+ continue;
82
+ }
83
+ const defer = getDeferValues(variableValues, selection);
84
+ if (visitedFragmentNames.has(fragName) && !defer) {
85
+ continue;
86
+ }
87
+ const fragment = fragments[fragName];
88
+ if (!fragment || !(0, utils_1.doesFragmentConditionMatch)(schema, fragment, runtimeType)) {
89
+ continue;
90
+ }
91
+ if (!defer) {
92
+ visitedFragmentNames.add(fragName);
93
+ }
94
+ if (defer) {
95
+ const patchFields = new AccumulatorMap_1.AccumulatorMap();
96
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, patchFields, patches, visitedFragmentNames);
97
+ patches.push({
98
+ label: defer.label,
99
+ fields: patchFields,
100
+ });
101
+ }
102
+ else {
103
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, fields, patches, visitedFragmentNames);
104
+ }
105
+ break;
106
+ }
107
+ }
108
+ }
109
+ }
110
+ /**
111
+ * Returns an object containing the `@defer` arguments if a field should be
112
+ * deferred based on the experimental flag, defer directive present and
113
+ * not disabled by the "if" argument.
114
+ */
115
+ function getDeferValues(variableValues, node) {
116
+ const defer = (0, graphql_1.getDirectiveValues)(directives_1.GraphQLDeferDirective, node, variableValues);
117
+ if (!defer) {
118
+ return;
119
+ }
120
+ if (defer['if'] === false) {
121
+ return;
122
+ }
123
+ return {
124
+ label: typeof defer['label'] === 'string' ? defer['label'] : undefined,
125
+ };
126
+ }