@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,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flattenAsyncIterable = void 0;
4
+ /**
5
+ * Given an AsyncIterable of AsyncIterables, flatten all yielded results into a
6
+ * single AsyncIterable.
7
+ */
8
+ function flattenAsyncIterable(iterable) {
9
+ // You might think this whole function could be replaced with
10
+ //
11
+ // async function* flattenAsyncIterable(iterable) {
12
+ // for await (const subIterator of iterable) {
13
+ // yield* subIterator;
14
+ // }
15
+ // }
16
+ //
17
+ // but calling `.return()` on the iterator it returns won't interrupt the `for await`.
18
+ const topIterator = iterable[Symbol.asyncIterator]();
19
+ let currentNestedIterator;
20
+ let waitForCurrentNestedIterator;
21
+ let done = false;
22
+ async function next() {
23
+ if (done) {
24
+ return { value: undefined, done: true };
25
+ }
26
+ try {
27
+ if (!currentNestedIterator) {
28
+ // Somebody else is getting it already.
29
+ if (waitForCurrentNestedIterator) {
30
+ await waitForCurrentNestedIterator;
31
+ return await next();
32
+ }
33
+ // Nobody else is getting it. We should!
34
+ let resolve;
35
+ waitForCurrentNestedIterator = new Promise(r => {
36
+ resolve = r;
37
+ });
38
+ const topIteratorResult = await topIterator.next();
39
+ if (topIteratorResult.done) {
40
+ // Given that done only ever transitions from false to true,
41
+ // require-atomic-updates is being unnecessarily cautious.
42
+ done = true;
43
+ return await next();
44
+ }
45
+ // eslint is making a reasonable point here, but we've explicitly protected
46
+ // ourself from the race condition by ensuring that only the single call
47
+ // that assigns to waitForCurrentNestedIterator is allowed to assign to
48
+ // currentNestedIterator or waitForCurrentNestedIterator.
49
+ currentNestedIterator = topIteratorResult.value[Symbol.asyncIterator]();
50
+ waitForCurrentNestedIterator = undefined;
51
+ resolve();
52
+ return await next();
53
+ }
54
+ const rememberCurrentNestedIterator = currentNestedIterator;
55
+ const nestedIteratorResult = await currentNestedIterator.next();
56
+ if (!nestedIteratorResult.done) {
57
+ return nestedIteratorResult;
58
+ }
59
+ // The nested iterator is done. If it's still the current one, make it not
60
+ // current. (If it's not the current one, somebody else has made us move on.)
61
+ if (currentNestedIterator === rememberCurrentNestedIterator) {
62
+ currentNestedIterator = undefined;
63
+ }
64
+ return await next();
65
+ }
66
+ catch (err) {
67
+ done = true;
68
+ throw err;
69
+ }
70
+ }
71
+ return {
72
+ next,
73
+ async return() {
74
+ var _a, _b;
75
+ done = true;
76
+ await Promise.all([(_a = currentNestedIterator === null || currentNestedIterator === void 0 ? void 0 : currentNestedIterator.return) === null || _a === void 0 ? void 0 : _a.call(currentNestedIterator), (_b = topIterator.return) === null || _b === void 0 ? void 0 : _b.call(topIterator)]);
77
+ return { value: undefined, done: true };
78
+ },
79
+ async throw(error) {
80
+ var _a, _b;
81
+ done = true;
82
+ await Promise.all([(_a = currentNestedIterator === null || currentNestedIterator === void 0 ? void 0 : currentNestedIterator.throw) === null || _a === void 0 ? void 0 : _a.call(currentNestedIterator, error), (_b = topIterator.throw) === null || _b === void 0 ? void 0 : _b.call(topIterator, error)]);
83
+ /* c8 ignore next */
84
+ throw error;
85
+ },
86
+ [Symbol.asyncIterator]() {
87
+ return this;
88
+ },
89
+ };
90
+ }
91
+ exports.flattenAsyncIterable = flattenAsyncIterable;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.invariant = void 0;
4
+ function invariant(condition, message) {
5
+ if (!condition) {
6
+ throw new Error(message != null ? message : 'Unexpected invariant triggered.');
7
+ }
8
+ }
9
+ exports.invariant = invariant;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.promiseForObject = void 0;
4
+ /**
5
+ * This function transforms a JS object `Record<string, Promise<T>>` into
6
+ * a `Promise<Record<string, T>>`
7
+ *
8
+ * This is akin to bluebird's `Promise.props`, but implemented only using
9
+ * `Promise.all` so it will work with any implementation of ES6 promises.
10
+ */
11
+ async function promiseForObject(object) {
12
+ const keys = Object.keys(object);
13
+ const values = Object.values(object);
14
+ const resolvedValues = await Promise.all(values);
15
+ const resolvedObject = Object.create(null);
16
+ for (let i = 0; i < keys.length; ++i) {
17
+ resolvedObject[keys[i]] = resolvedValues[i];
18
+ }
19
+ return resolvedObject;
20
+ }
21
+ exports.promiseForObject = promiseForObject;
package/cjs/index.js CHANGED
@@ -2,3 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./execution/index.js"), exports);
5
+ tslib_1.__exportStar(require("./directives/index.js"), exports);
@@ -0,0 +1,20 @@
1
+ import { DirectiveLocation, GraphQLBoolean, GraphQLDirective, GraphQLNonNull, GraphQLString } from 'graphql';
2
+ /**
3
+ * Used to conditionally defer fragments.
4
+ */
5
+ export const GraphQLDeferDirective = new GraphQLDirective({
6
+ name: 'defer',
7
+ description: 'Directs the executor to defer this fragment when the `if` argument is true or undefined.',
8
+ locations: [DirectiveLocation.FRAGMENT_SPREAD, DirectiveLocation.INLINE_FRAGMENT],
9
+ args: {
10
+ if: {
11
+ type: new GraphQLNonNull(GraphQLBoolean),
12
+ description: 'Deferred when true or undefined.',
13
+ defaultValue: true,
14
+ },
15
+ label: {
16
+ type: GraphQLString,
17
+ description: 'Unique name',
18
+ },
19
+ },
20
+ });
@@ -0,0 +1,2 @@
1
+ export * from './defer.js';
2
+ export * from './stream.js';
@@ -0,0 +1,25 @@
1
+ import { DirectiveLocation, GraphQLBoolean, GraphQLDirective, GraphQLInt, GraphQLNonNull, GraphQLString, } from 'graphql';
2
+ /**
3
+ * Used to conditionally stream list fields.
4
+ */
5
+ export const GraphQLStreamDirective = new GraphQLDirective({
6
+ name: 'stream',
7
+ description: 'Directs the executor to stream plural fields when the `if` argument is true or undefined.',
8
+ locations: [DirectiveLocation.FIELD],
9
+ args: {
10
+ if: {
11
+ type: new GraphQLNonNull(GraphQLBoolean),
12
+ description: 'Stream when true or undefined.',
13
+ defaultValue: true,
14
+ },
15
+ label: {
16
+ type: GraphQLString,
17
+ description: 'Unique name',
18
+ },
19
+ initialCount: {
20
+ defaultValue: 0,
21
+ type: GraphQLInt,
22
+ description: 'Number of items to return immediately',
23
+ },
24
+ },
25
+ });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * ES6 Map with additional `add` method to accumulate items.
3
+ */
4
+ export class AccumulatorMap extends Map {
5
+ get [Symbol.toStringTag]() {
6
+ return 'AccumulatorMap';
7
+ }
8
+ add(key, item) {
9
+ const group = this.get(key);
10
+ if (group === undefined) {
11
+ this.set(key, [item]);
12
+ }
13
+ else {
14
+ group.push(item);
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,122 @@
1
+ import { Kind, getDirectiveValues, } from 'graphql';
2
+ import { doesFragmentConditionMatch, getFieldEntryKey, memoize5, shouldIncludeNode } from '@graphql-tools/utils';
3
+ import { AccumulatorMap } from './AccumulatorMap';
4
+ import { GraphQLDeferDirective } from '../directives';
5
+ /**
6
+ * Given a selectionSet, collects all of the fields and returns them.
7
+ *
8
+ * CollectFields requires the "runtime type" of an object. For a field that
9
+ * returns an Interface or Union type, the "runtime type" will be the actual
10
+ * object type returned by that field.
11
+ *
12
+ * @internal
13
+ */
14
+ export function collectFields(schema, fragments, variableValues, runtimeType, selectionSet) {
15
+ const fields = new AccumulatorMap();
16
+ const patches = [];
17
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, patches, new Set());
18
+ return { fields, patches };
19
+ }
20
+ /**
21
+ * Given an array of field nodes, collects all of the subfields of the passed
22
+ * in fields, and returns them at the end.
23
+ *
24
+ * CollectSubFields requires the "return type" of an object. For a field that
25
+ * returns an Interface or Union type, the "return type" will be the actual
26
+ * object type returned by that field.
27
+ *
28
+ * @internal
29
+ */
30
+ export const collectSubfields = memoize5(function collectSubfields(schema, fragments, variableValues, returnType, fieldNodes) {
31
+ const subFieldNodes = new AccumulatorMap();
32
+ const visitedFragmentNames = new Set();
33
+ const subPatches = [];
34
+ const subFieldsAndPatches = {
35
+ fields: subFieldNodes,
36
+ patches: subPatches,
37
+ };
38
+ for (const node of fieldNodes) {
39
+ if (node.selectionSet) {
40
+ collectFieldsImpl(schema, fragments, variableValues, returnType, node.selectionSet, subFieldNodes, subPatches, visitedFragmentNames);
41
+ }
42
+ }
43
+ return subFieldsAndPatches;
44
+ });
45
+ function collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, patches, visitedFragmentNames) {
46
+ for (const selection of selectionSet.selections) {
47
+ switch (selection.kind) {
48
+ case Kind.FIELD: {
49
+ if (!shouldIncludeNode(variableValues, selection)) {
50
+ continue;
51
+ }
52
+ fields.add(getFieldEntryKey(selection), selection);
53
+ break;
54
+ }
55
+ case Kind.INLINE_FRAGMENT: {
56
+ if (!shouldIncludeNode(variableValues, selection) ||
57
+ !doesFragmentConditionMatch(schema, selection, runtimeType)) {
58
+ continue;
59
+ }
60
+ const defer = getDeferValues(variableValues, selection);
61
+ if (defer) {
62
+ const patchFields = new AccumulatorMap();
63
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, patchFields, patches, visitedFragmentNames);
64
+ patches.push({
65
+ label: defer.label,
66
+ fields: patchFields,
67
+ });
68
+ }
69
+ else {
70
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, fields, patches, visitedFragmentNames);
71
+ }
72
+ break;
73
+ }
74
+ case Kind.FRAGMENT_SPREAD: {
75
+ const fragName = selection.name.value;
76
+ if (!shouldIncludeNode(variableValues, selection)) {
77
+ continue;
78
+ }
79
+ const defer = getDeferValues(variableValues, selection);
80
+ if (visitedFragmentNames.has(fragName) && !defer) {
81
+ continue;
82
+ }
83
+ const fragment = fragments[fragName];
84
+ if (!fragment || !doesFragmentConditionMatch(schema, fragment, runtimeType)) {
85
+ continue;
86
+ }
87
+ if (!defer) {
88
+ visitedFragmentNames.add(fragName);
89
+ }
90
+ if (defer) {
91
+ const patchFields = new AccumulatorMap();
92
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, patchFields, patches, visitedFragmentNames);
93
+ patches.push({
94
+ label: defer.label,
95
+ fields: patchFields,
96
+ });
97
+ }
98
+ else {
99
+ collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, fields, patches, visitedFragmentNames);
100
+ }
101
+ break;
102
+ }
103
+ }
104
+ }
105
+ }
106
+ /**
107
+ * Returns an object containing the `@defer` arguments if a field should be
108
+ * deferred based on the experimental flag, defer directive present and
109
+ * not disabled by the "if" argument.
110
+ */
111
+ function getDeferValues(variableValues, node) {
112
+ const defer = getDirectiveValues(GraphQLDeferDirective, node, variableValues);
113
+ if (!defer) {
114
+ return;
115
+ }
116
+ if (defer['if'] === false) {
117
+ return;
118
+ }
119
+ return {
120
+ label: typeof defer['label'] === 'string' ? defer['label'] : undefined,
121
+ };
122
+ }