@graphql-tools/executor 0.0.1-alpha-20221025014654-e3be5659 → 0.0.1-alpha-20221025154355-1d847f5a
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/execute.js +73 -68
- package/cjs/execution/index.js +0 -5
- package/cjs/execution/subscribe.js +9 -13
- package/cjs/execution/values.js +17 -15
- package/esm/execution/execute.js +47 -42
- package/esm/execution/index.js +0 -3
- package/esm/execution/subscribe.js +5 -9
- package/esm/execution/values.js +15 -13
- package/package.json +4 -1
- package/typings/execution/execute.d.cts +7 -7
- package/typings/execution/execute.d.ts +7 -7
- package/typings/execution/index.d.cts +0 -3
- package/typings/execution/index.d.ts +0 -3
- package/typings/execution/subscribe.d.cts +2 -2
- package/typings/execution/subscribe.d.ts +2 -2
- package/typings/execution/values.d.cts +3 -4
- package/typings/execution/values.d.ts +3 -4
- package/cjs/execution/AccumulatorMap.js +0 -21
- package/cjs/execution/collectFields.js +0 -114
- package/cjs/execution/mapAsyncIterator.js +0 -53
- package/esm/execution/AccumulatorMap.js +0 -17
- package/esm/execution/collectFields.js +0 -109
- package/esm/execution/mapAsyncIterator.js +0 -49
- package/typings/execution/AccumulatorMap.d.cts +0 -7
- package/typings/execution/AccumulatorMap.d.ts +0 -7
- package/typings/execution/collectFields.d.cts +0 -27
- package/typings/execution/collectFields.d.ts +0 -27
- package/typings/execution/mapAsyncIterator.d.cts +0 -6
- package/typings/execution/mapAsyncIterator.d.ts +0 -6
package/cjs/execution/execute.js
CHANGED
|
@@ -1,29 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getFieldDef = exports.createSourceEventStream = exports.subscribe = exports.defaultFieldResolver = exports.defaultTypeResolver = exports.buildResolveInfo = exports.buildExecutionContext = exports.assertValidExecutionArguments = exports.executeSync = exports.execute = void 0;
|
|
4
|
-
const devAssert_js_1 = require("graphql/jsutils/devAssert.js");
|
|
5
|
-
const inspect_js_1 = require("graphql/jsutils/inspect.js");
|
|
6
|
-
const invariant_js_1 = require("graphql/jsutils/invariant.js");
|
|
7
|
-
const isAsyncIterable_js_1 = require("graphql/jsutils/isAsyncIterable.js");
|
|
8
|
-
const isIterableObject_js_1 = require("graphql/jsutils/isIterableObject.js");
|
|
9
|
-
const isObjectLike_js_1 = require("graphql/jsutils/isObjectLike.js");
|
|
10
|
-
const isPromise_js_1 = require("graphql/jsutils/isPromise.js");
|
|
11
|
-
const memoize3_js_1 = require("graphql/jsutils/memoize3.js");
|
|
12
4
|
const Path_js_1 = require("graphql/jsutils/Path.js");
|
|
13
|
-
const promiseForObject_js_1 = require("graphql/jsutils/promiseForObject.js");
|
|
14
|
-
const promiseReduce_js_1 = require("graphql/jsutils/promiseReduce.js");
|
|
15
5
|
const graphql_1 = require("graphql");
|
|
16
|
-
const collectFields_js_1 = require("./collectFields.js");
|
|
17
|
-
const mapAsyncIterator_js_1 = require("./mapAsyncIterator.js");
|
|
18
6
|
const values_js_1 = require("./values.js");
|
|
19
|
-
|
|
20
|
-
// so just disable it for entire file.
|
|
21
|
-
/**
|
|
22
|
-
* A memoized collection of relevant subfields with regard to the return
|
|
23
|
-
* type. Memoizing ensures the subfields are not repeatedly calculated, which
|
|
24
|
-
* saves overhead when resolving lists of values.
|
|
25
|
-
*/
|
|
26
|
-
const collectSubfields = (0, memoize3_js_1.memoize3)((exeContext, returnType, fieldNodes) => (0, collectFields_js_1.collectSubfields)(exeContext.schema, exeContext.fragments, exeContext.variableValues, returnType, fieldNodes));
|
|
7
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
27
8
|
/**
|
|
28
9
|
* Implements the "Executing requests" section of the GraphQL specification.
|
|
29
10
|
*
|
|
@@ -59,7 +40,7 @@ function executeImpl(exeContext) {
|
|
|
59
40
|
// in this case is the entire response.
|
|
60
41
|
try {
|
|
61
42
|
const result = executeOperation(exeContext);
|
|
62
|
-
if (
|
|
43
|
+
if (isPromise(result)) {
|
|
63
44
|
return result.then(data => buildResponse(data, exeContext.errors), error => {
|
|
64
45
|
exeContext.errors.push(error);
|
|
65
46
|
return buildResponse(null, exeContext.errors);
|
|
@@ -80,7 +61,7 @@ function executeImpl(exeContext) {
|
|
|
80
61
|
function executeSync(args) {
|
|
81
62
|
const result = execute(args);
|
|
82
63
|
// Assert that the execution was synchronous.
|
|
83
|
-
if (
|
|
64
|
+
if (isPromise(result)) {
|
|
84
65
|
throw new Error('GraphQL execution failed to complete synchronously.');
|
|
85
66
|
}
|
|
86
67
|
return result;
|
|
@@ -100,11 +81,11 @@ function buildResponse(data, errors) {
|
|
|
100
81
|
* @internal
|
|
101
82
|
*/
|
|
102
83
|
function assertValidExecutionArguments(schema, document, rawVariableValues) {
|
|
103
|
-
|
|
84
|
+
console.assert(!!document, 'Must provide document.');
|
|
104
85
|
// If the schema used for execution is invalid, throw an error.
|
|
105
86
|
(0, graphql_1.assertValidSchema)(schema);
|
|
106
87
|
// Variables, if provided, must be an object.
|
|
107
|
-
|
|
88
|
+
console.assert(rawVariableValues == null || isObjectLike(rawVariableValues), 'Variables must be provided as an Object where each property is a variable value. Perhaps look to see if an unparsed JSON string was provided.');
|
|
108
89
|
}
|
|
109
90
|
exports.assertValidExecutionArguments = assertValidExecutionArguments;
|
|
110
91
|
/**
|
|
@@ -128,7 +109,7 @@ function buildExecutionContext(args) {
|
|
|
128
109
|
case graphql_1.Kind.OPERATION_DEFINITION:
|
|
129
110
|
if (operationName == null) {
|
|
130
111
|
if (operation !== undefined) {
|
|
131
|
-
return [
|
|
112
|
+
return [(0, utils_1.createGraphQLError)('Must provide operation name if query contains multiple operations.')];
|
|
132
113
|
}
|
|
133
114
|
operation = definition;
|
|
134
115
|
}
|
|
@@ -145,9 +126,9 @@ function buildExecutionContext(args) {
|
|
|
145
126
|
}
|
|
146
127
|
if (!operation) {
|
|
147
128
|
if (operationName != null) {
|
|
148
|
-
return [
|
|
129
|
+
return [(0, utils_1.createGraphQLError)(`Unknown operation named "${operationName}".`)];
|
|
149
130
|
}
|
|
150
|
-
return [
|
|
131
|
+
return [(0, utils_1.createGraphQLError)('Must provide an operation.')];
|
|
151
132
|
}
|
|
152
133
|
// FIXME: https://github.com/graphql/graphql-js/issues/2203
|
|
153
134
|
/* c8 ignore next */
|
|
@@ -186,11 +167,11 @@ function executeOperation(exeContext) {
|
|
|
186
167
|
const { operation, schema, fragments, variableValues, rootValue } = exeContext;
|
|
187
168
|
const rootType = schema.getRootType(operation.operation);
|
|
188
169
|
if (rootType == null) {
|
|
189
|
-
throw
|
|
170
|
+
throw (0, utils_1.createGraphQLError)(`Schema is not configured to execute ${operation.operation} operation.`, {
|
|
190
171
|
nodes: operation,
|
|
191
172
|
});
|
|
192
173
|
}
|
|
193
|
-
const rootFields = (0,
|
|
174
|
+
const rootFields = (0, utils_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
|
|
194
175
|
const path = undefined;
|
|
195
176
|
switch (operation.operation) {
|
|
196
177
|
case graphql_1.OperationTypeNode.QUERY:
|
|
@@ -208,13 +189,13 @@ function executeOperation(exeContext) {
|
|
|
208
189
|
* for fields that must be executed serially.
|
|
209
190
|
*/
|
|
210
191
|
function executeFieldsSerially(exeContext, parentType, sourceValue, path, fields) {
|
|
211
|
-
return
|
|
192
|
+
return promiseReduce(fields.entries(), (results, [responseName, fieldNodes]) => {
|
|
212
193
|
const fieldPath = (0, Path_js_1.addPath)(path, responseName, parentType.name);
|
|
213
194
|
const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath);
|
|
214
195
|
if (result === undefined) {
|
|
215
196
|
return results;
|
|
216
197
|
}
|
|
217
|
-
if (
|
|
198
|
+
if (isPromise(result)) {
|
|
218
199
|
return result.then(resolvedResult => {
|
|
219
200
|
results[responseName] = resolvedResult;
|
|
220
201
|
return results;
|
|
@@ -236,7 +217,7 @@ function executeFields(exeContext, parentType, sourceValue, path, fields) {
|
|
|
236
217
|
const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath);
|
|
237
218
|
if (result !== undefined) {
|
|
238
219
|
results[responseName] = result;
|
|
239
|
-
if (
|
|
220
|
+
if (isPromise(result)) {
|
|
240
221
|
containsPromise = true;
|
|
241
222
|
}
|
|
242
223
|
}
|
|
@@ -248,7 +229,13 @@ function executeFields(exeContext, parentType, sourceValue, path, fields) {
|
|
|
248
229
|
// Otherwise, results is a map from field name to the result of resolving that
|
|
249
230
|
// field, which is possibly a promise. Return a promise that will return this
|
|
250
231
|
// same map, but with any promises replaced with the values they resolved to.
|
|
251
|
-
return (
|
|
232
|
+
return Promise.all(Object.values(results)).then(resolvedValues => {
|
|
233
|
+
const resolvedObject = Object.create(null);
|
|
234
|
+
for (const [i, key] of Object.keys(results).entries()) {
|
|
235
|
+
resolvedObject[key] = resolvedValues[i];
|
|
236
|
+
}
|
|
237
|
+
return resolvedObject;
|
|
238
|
+
});
|
|
252
239
|
}
|
|
253
240
|
/**
|
|
254
241
|
* Implements the "Executing fields" section of the spec
|
|
@@ -277,13 +264,13 @@ function executeField(exeContext, parentType, source, fieldNodes, path) {
|
|
|
277
264
|
const contextValue = exeContext.contextValue;
|
|
278
265
|
const result = resolveFn(source, args, contextValue, info);
|
|
279
266
|
let completed;
|
|
280
|
-
if (
|
|
267
|
+
if (isPromise(result)) {
|
|
281
268
|
completed = result.then(resolved => completeValue(exeContext, returnType, fieldNodes, info, path, resolved));
|
|
282
269
|
}
|
|
283
270
|
else {
|
|
284
271
|
completed = completeValue(exeContext, returnType, fieldNodes, info, path, result);
|
|
285
272
|
}
|
|
286
|
-
if (
|
|
273
|
+
if (isPromise(completed)) {
|
|
287
274
|
// Note: we don't rely on a `catch` method, but we do expect "thenable"
|
|
288
275
|
// to take a second callback for the error case.
|
|
289
276
|
return completed.then(undefined, rawError => {
|
|
@@ -389,7 +376,7 @@ function completeValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
|
389
376
|
}
|
|
390
377
|
/* c8 ignore next 6 */
|
|
391
378
|
// Not reachable, all possible output types have been considered.
|
|
392
|
-
|
|
379
|
+
console.assert(false, 'Cannot complete value of unexpected output type: ' + (0, utils_1.inspect)(returnType));
|
|
393
380
|
}
|
|
394
381
|
/**
|
|
395
382
|
* Complete a async iterator value by completing the result and calling
|
|
@@ -409,7 +396,7 @@ async function completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info
|
|
|
409
396
|
try {
|
|
410
397
|
// TODO can the error checking logic be consolidated with completeListValue?
|
|
411
398
|
const completedItem = completeValue(exeContext, itemType, fieldNodes, info, fieldPath, value);
|
|
412
|
-
if (
|
|
399
|
+
if (isPromise(completedItem)) {
|
|
413
400
|
containsPromise = true;
|
|
414
401
|
}
|
|
415
402
|
completedResults.push(completedItem);
|
|
@@ -430,18 +417,36 @@ async function completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info
|
|
|
430
417
|
}
|
|
431
418
|
return containsPromise ? Promise.all(completedResults) : completedResults;
|
|
432
419
|
}
|
|
420
|
+
function isIterableObject(value) {
|
|
421
|
+
return value != null && typeof value === 'object' && Symbol.iterator in value;
|
|
422
|
+
}
|
|
423
|
+
function isObjectLike(value) {
|
|
424
|
+
return typeof value === 'object' && value !== null;
|
|
425
|
+
}
|
|
426
|
+
function isPromise(value) {
|
|
427
|
+
return isObjectLike(value) && typeof value['then'] === 'function';
|
|
428
|
+
}
|
|
429
|
+
function promiseReduce(values, callbackFn, initialValue) {
|
|
430
|
+
let accumulator = initialValue;
|
|
431
|
+
for (const value of values) {
|
|
432
|
+
accumulator = isPromise(accumulator)
|
|
433
|
+
? accumulator.then(resolved => callbackFn(resolved, value))
|
|
434
|
+
: callbackFn(accumulator, value);
|
|
435
|
+
}
|
|
436
|
+
return accumulator;
|
|
437
|
+
}
|
|
433
438
|
/**
|
|
434
439
|
* Complete a list value by completing each item in the list with the
|
|
435
440
|
* inner type
|
|
436
441
|
*/
|
|
437
442
|
function completeListValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
438
443
|
const itemType = returnType.ofType;
|
|
439
|
-
if ((0,
|
|
444
|
+
if ((0, utils_1.isAsyncIterable)(result)) {
|
|
440
445
|
const iterator = result[Symbol.asyncIterator]();
|
|
441
446
|
return completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info, path, iterator);
|
|
442
447
|
}
|
|
443
|
-
if (!
|
|
444
|
-
throw
|
|
448
|
+
if (!isIterableObject(result)) {
|
|
449
|
+
throw (0, utils_1.createGraphQLError)(`Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`);
|
|
445
450
|
}
|
|
446
451
|
// This is specified as a simple map, however we're optimizing the path
|
|
447
452
|
// where the list contains no Promises by avoiding creating another Promise.
|
|
@@ -452,13 +457,13 @@ function completeListValue(exeContext, returnType, fieldNodes, info, path, resul
|
|
|
452
457
|
const itemPath = (0, Path_js_1.addPath)(path, index, undefined);
|
|
453
458
|
try {
|
|
454
459
|
let completedItem;
|
|
455
|
-
if (
|
|
460
|
+
if (isPromise(item)) {
|
|
456
461
|
completedItem = item.then(resolved => completeValue(exeContext, itemType, fieldNodes, info, itemPath, resolved));
|
|
457
462
|
}
|
|
458
463
|
else {
|
|
459
464
|
completedItem = completeValue(exeContext, itemType, fieldNodes, info, itemPath, item);
|
|
460
465
|
}
|
|
461
|
-
if (
|
|
466
|
+
if (isPromise(completedItem)) {
|
|
462
467
|
containsPromise = true;
|
|
463
468
|
// Note: we don't rely on a `catch` method, but we do expect "thenable"
|
|
464
469
|
// to take a second callback for the error case.
|
|
@@ -483,8 +488,8 @@ function completeListValue(exeContext, returnType, fieldNodes, info, path, resul
|
|
|
483
488
|
function completeLeafValue(returnType, result) {
|
|
484
489
|
const serializedResult = returnType.serialize(result);
|
|
485
490
|
if (serializedResult == null) {
|
|
486
|
-
throw new Error(`Expected \`${(0,
|
|
487
|
-
`return non-nullable value, returned: ${(0,
|
|
491
|
+
throw new Error(`Expected \`${(0, utils_1.inspect)(returnType)}.serialize(${(0, utils_1.inspect)(result)})\` to ` +
|
|
492
|
+
`return non-nullable value, returned: ${(0, utils_1.inspect)(serializedResult)}`);
|
|
488
493
|
}
|
|
489
494
|
return serializedResult;
|
|
490
495
|
}
|
|
@@ -497,33 +502,33 @@ function completeAbstractValue(exeContext, returnType, fieldNodes, info, path, r
|
|
|
497
502
|
const resolveTypeFn = (_a = returnType.resolveType) !== null && _a !== void 0 ? _a : exeContext.typeResolver;
|
|
498
503
|
const contextValue = exeContext.contextValue;
|
|
499
504
|
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
|
|
500
|
-
if (
|
|
505
|
+
if (isPromise(runtimeType)) {
|
|
501
506
|
return runtimeType.then(resolvedRuntimeType => completeObjectValue(exeContext, ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result));
|
|
502
507
|
}
|
|
503
508
|
return completeObjectValue(exeContext, ensureValidRuntimeType(runtimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result);
|
|
504
509
|
}
|
|
505
510
|
function ensureValidRuntimeType(runtimeTypeName, exeContext, returnType, fieldNodes, info, result) {
|
|
506
511
|
if (runtimeTypeName == null) {
|
|
507
|
-
throw
|
|
512
|
+
throw (0, utils_1.createGraphQLError)(`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}". Either the "${returnType.name}" type should provide a "resolveType" function or each possible type should provide an "isTypeOf" function.`, { nodes: fieldNodes });
|
|
508
513
|
}
|
|
509
514
|
// releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType`
|
|
510
515
|
// TODO: remove in 17.0.0 release
|
|
511
516
|
if ((0, graphql_1.isObjectType)(runtimeTypeName)) {
|
|
512
|
-
throw
|
|
517
|
+
throw (0, utils_1.createGraphQLError)('Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.');
|
|
513
518
|
}
|
|
514
519
|
if (typeof runtimeTypeName !== 'string') {
|
|
515
|
-
throw
|
|
516
|
-
`value ${(0,
|
|
520
|
+
throw (0, utils_1.createGraphQLError)(`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
|
|
521
|
+
`value ${(0, utils_1.inspect)(result)}, received "${(0, utils_1.inspect)(runtimeTypeName)}".`);
|
|
517
522
|
}
|
|
518
523
|
const runtimeType = exeContext.schema.getType(runtimeTypeName);
|
|
519
524
|
if (runtimeType == null) {
|
|
520
|
-
throw
|
|
525
|
+
throw (0, utils_1.createGraphQLError)(`Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`, { nodes: fieldNodes });
|
|
521
526
|
}
|
|
522
527
|
if (!(0, graphql_1.isObjectType)(runtimeType)) {
|
|
523
|
-
throw
|
|
528
|
+
throw (0, utils_1.createGraphQLError)(`Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`, { nodes: fieldNodes });
|
|
524
529
|
}
|
|
525
530
|
if (!exeContext.schema.isSubType(returnType, runtimeType)) {
|
|
526
|
-
throw
|
|
531
|
+
throw (0, utils_1.createGraphQLError)(`Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`, { nodes: fieldNodes });
|
|
527
532
|
}
|
|
528
533
|
return runtimeType;
|
|
529
534
|
}
|
|
@@ -532,13 +537,13 @@ function ensureValidRuntimeType(runtimeTypeName, exeContext, returnType, fieldNo
|
|
|
532
537
|
*/
|
|
533
538
|
function completeObjectValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
534
539
|
// Collect sub-fields to execute to complete this value.
|
|
535
|
-
const subFieldNodes =
|
|
540
|
+
const subFieldNodes = (0, utils_1.collectSubFields)(exeContext.schema, exeContext.fragments, exeContext.variableValues, returnType, fieldNodes);
|
|
536
541
|
// If there is an isTypeOf predicate function, call it with the
|
|
537
542
|
// current result. If isTypeOf returns false, then raise an error rather
|
|
538
543
|
// than continuing execution.
|
|
539
544
|
if (returnType.isTypeOf) {
|
|
540
545
|
const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
|
|
541
|
-
if (
|
|
546
|
+
if (isPromise(isTypeOf)) {
|
|
542
547
|
return isTypeOf.then(resolvedIsTypeOf => {
|
|
543
548
|
if (!resolvedIsTypeOf) {
|
|
544
549
|
throw invalidReturnTypeError(returnType, result, fieldNodes);
|
|
@@ -553,7 +558,7 @@ function completeObjectValue(exeContext, returnType, fieldNodes, info, path, res
|
|
|
553
558
|
return executeFields(exeContext, returnType, result, path, subFieldNodes);
|
|
554
559
|
}
|
|
555
560
|
function invalidReturnTypeError(returnType, result, fieldNodes) {
|
|
556
|
-
return
|
|
561
|
+
return (0, utils_1.createGraphQLError)(`Expected value of type "${returnType.name}" but got: ${(0, utils_1.inspect)(result)}.`, {
|
|
557
562
|
nodes: fieldNodes,
|
|
558
563
|
});
|
|
559
564
|
}
|
|
@@ -569,7 +574,7 @@ function invalidReturnTypeError(returnType, result, fieldNodes) {
|
|
|
569
574
|
*/
|
|
570
575
|
const defaultTypeResolver = function (value, contextValue, info, abstractType) {
|
|
571
576
|
// First, look for `__typename`.
|
|
572
|
-
if (
|
|
577
|
+
if (isObjectLike(value) && typeof value['__typename'] === 'string') {
|
|
573
578
|
return value['__typename'];
|
|
574
579
|
}
|
|
575
580
|
// Otherwise, test each possible type.
|
|
@@ -579,7 +584,7 @@ const defaultTypeResolver = function (value, contextValue, info, abstractType) {
|
|
|
579
584
|
const type = possibleTypes[i];
|
|
580
585
|
if (type.isTypeOf) {
|
|
581
586
|
const isTypeOfResult = type.isTypeOf(value, contextValue, info);
|
|
582
|
-
if (
|
|
587
|
+
if (isPromise(isTypeOfResult)) {
|
|
583
588
|
promisedIsTypeOfResults[i] = isTypeOfResult;
|
|
584
589
|
}
|
|
585
590
|
else if (isTypeOfResult) {
|
|
@@ -606,7 +611,7 @@ exports.defaultTypeResolver = defaultTypeResolver;
|
|
|
606
611
|
*/
|
|
607
612
|
const defaultFieldResolver = function (source, args, contextValue, info) {
|
|
608
613
|
// ensure source is a value for which property access is acceptable.
|
|
609
|
-
if (
|
|
614
|
+
if (isObjectLike(source) || typeof source === 'function') {
|
|
610
615
|
const property = source[info.fieldName];
|
|
611
616
|
if (typeof property === 'function') {
|
|
612
617
|
return source[info.fieldName](args, contextValue, info);
|
|
@@ -645,14 +650,14 @@ function subscribe(args) {
|
|
|
645
650
|
return { errors: exeContext };
|
|
646
651
|
}
|
|
647
652
|
const resultOrStream = createSourceEventStreamImpl(exeContext);
|
|
648
|
-
if (
|
|
653
|
+
if (isPromise(resultOrStream)) {
|
|
649
654
|
return resultOrStream.then(resolvedResultOrStream => mapSourceToResponse(exeContext, resolvedResultOrStream));
|
|
650
655
|
}
|
|
651
656
|
return mapSourceToResponse(exeContext, resultOrStream);
|
|
652
657
|
}
|
|
653
658
|
exports.subscribe = subscribe;
|
|
654
659
|
function mapSourceToResponse(exeContext, resultOrStream) {
|
|
655
|
-
if (!(0,
|
|
660
|
+
if (!(0, utils_1.isAsyncIterable)(resultOrStream)) {
|
|
656
661
|
return resultOrStream;
|
|
657
662
|
}
|
|
658
663
|
// For each payload yielded from a subscription, map it over the normal
|
|
@@ -661,7 +666,7 @@ function mapSourceToResponse(exeContext, resultOrStream) {
|
|
|
661
666
|
// the GraphQL specification. The `execute` function provides the
|
|
662
667
|
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
|
|
663
668
|
// "ExecuteQuery" algorithm, for which `execute` is also used.
|
|
664
|
-
return (0,
|
|
669
|
+
return (0, utils_1.mapAsyncIterator)(resultOrStream[Symbol.asyncIterator](), (payload) => executeImpl(buildPerEventExecutionContext(exeContext, payload)));
|
|
665
670
|
}
|
|
666
671
|
/**
|
|
667
672
|
* Implements the "CreateSourceEventStream" algorithm described in the
|
|
@@ -705,7 +710,7 @@ exports.createSourceEventStream = createSourceEventStream;
|
|
|
705
710
|
function createSourceEventStreamImpl(exeContext) {
|
|
706
711
|
try {
|
|
707
712
|
const eventStream = executeSubscription(exeContext);
|
|
708
|
-
if (
|
|
713
|
+
if (isPromise(eventStream)) {
|
|
709
714
|
return eventStream.then(undefined, error => ({ errors: [error] }));
|
|
710
715
|
}
|
|
711
716
|
return eventStream;
|
|
@@ -719,14 +724,14 @@ function executeSubscription(exeContext) {
|
|
|
719
724
|
const { schema, fragments, operation, variableValues, rootValue } = exeContext;
|
|
720
725
|
const rootType = schema.getSubscriptionType();
|
|
721
726
|
if (rootType == null) {
|
|
722
|
-
throw
|
|
727
|
+
throw (0, utils_1.createGraphQLError)('Schema is not configured to execute subscription operation.', { nodes: operation });
|
|
723
728
|
}
|
|
724
|
-
const rootFields = (0,
|
|
729
|
+
const rootFields = (0, utils_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
|
|
725
730
|
const [responseName, fieldNodes] = [...rootFields.entries()][0];
|
|
726
731
|
const fieldName = fieldNodes[0].name.value;
|
|
727
732
|
const fieldDef = getFieldDef(schema, rootType, fieldNodes[0]);
|
|
728
733
|
if (!fieldDef) {
|
|
729
|
-
throw
|
|
734
|
+
throw (0, utils_1.createGraphQLError)(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes });
|
|
730
735
|
}
|
|
731
736
|
const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name);
|
|
732
737
|
const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, rootType, path);
|
|
@@ -744,7 +749,7 @@ function executeSubscription(exeContext) {
|
|
|
744
749
|
// AsyncIterable yielding raw payloads.
|
|
745
750
|
const resolveFn = (_a = fieldDef.subscribe) !== null && _a !== void 0 ? _a : exeContext.subscribeFieldResolver;
|
|
746
751
|
const result = resolveFn(rootValue, args, contextValue, info);
|
|
747
|
-
if (
|
|
752
|
+
if (isPromise(result)) {
|
|
748
753
|
return result.then(assertEventStream).then(undefined, error => {
|
|
749
754
|
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path));
|
|
750
755
|
});
|
|
@@ -760,8 +765,8 @@ function assertEventStream(result) {
|
|
|
760
765
|
throw result;
|
|
761
766
|
}
|
|
762
767
|
// Assert field returned an event stream, otherwise yield an error.
|
|
763
|
-
if (!(0,
|
|
764
|
-
throw
|
|
768
|
+
if (!(0, utils_1.isAsyncIterable)(result)) {
|
|
769
|
+
throw (0, utils_1.createGraphQLError)('Subscription field must return Async Iterable. ' + `Received: ${(0, utils_1.inspect)(result)}.`);
|
|
765
770
|
}
|
|
766
771
|
return result;
|
|
767
772
|
}
|
package/cjs/execution/index.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.responsePathAsArray = void 0;
|
|
4
3
|
const tslib_1 = require("tslib");
|
|
5
|
-
tslib_1.__exportStar(require("./collectFields.js"), exports);
|
|
6
4
|
tslib_1.__exportStar(require("./execute.js"), exports);
|
|
7
|
-
tslib_1.__exportStar(require("./mapAsyncIterator.js"), exports);
|
|
8
5
|
tslib_1.__exportStar(require("./values.js"), exports);
|
|
9
|
-
var Path_js_1 = require("graphql/jsutils/Path.js");
|
|
10
|
-
Object.defineProperty(exports, "responsePathAsArray", { enumerable: true, get: function () { return Path_js_1.pathToArray; } });
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createSourceEventStream = exports.subscribe = void 0;
|
|
4
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
4
5
|
const graphql_1 = require("graphql");
|
|
5
|
-
const devAssert_js_1 = require("graphql/jsutils/devAssert.js");
|
|
6
|
-
const inspect_js_1 = require("graphql/jsutils/inspect.js");
|
|
7
|
-
const isAsyncIterable_js_1 = require("graphql/jsutils/isAsyncIterable.js");
|
|
8
6
|
const Path_js_1 = require("graphql/jsutils/Path.js");
|
|
9
|
-
const collectFields_js_1 = require("./collectFields.js");
|
|
10
7
|
const execute_js_1 = require("./execute.js");
|
|
11
|
-
const mapAsyncIterator_js_1 = require("./mapAsyncIterator.js");
|
|
12
8
|
const values_js_1 = require("./values.js");
|
|
13
9
|
/**
|
|
14
10
|
* Implements the "Subscribe" algorithm described in the GraphQL specification.
|
|
@@ -33,10 +29,10 @@ const values_js_1 = require("./values.js");
|
|
|
33
29
|
*/
|
|
34
30
|
async function subscribe(args) {
|
|
35
31
|
// Temporary for v15 to v16 migration. Remove in v17
|
|
36
|
-
|
|
32
|
+
console.assert(arguments.length < 2, 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.');
|
|
37
33
|
const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver, } = args;
|
|
38
34
|
const resultOrStream = await createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver);
|
|
39
|
-
if (!(0,
|
|
35
|
+
if (!(0, utils_1.isAsyncIterable)(resultOrStream)) {
|
|
40
36
|
return resultOrStream;
|
|
41
37
|
}
|
|
42
38
|
// For each payload yielded from a subscription, map it over the normal
|
|
@@ -55,7 +51,7 @@ async function subscribe(args) {
|
|
|
55
51
|
fieldResolver,
|
|
56
52
|
});
|
|
57
53
|
// Map every source value to a ExecutionResult value as described above.
|
|
58
|
-
return (0,
|
|
54
|
+
return (0, utils_1.mapAsyncIterator)(resultOrStream[Symbol.asyncIterator](), mapSourceToResponse);
|
|
59
55
|
}
|
|
60
56
|
exports.subscribe = subscribe;
|
|
61
57
|
/**
|
|
@@ -108,8 +104,8 @@ async function createSourceEventStream(schema, document, rootValue, contextValue
|
|
|
108
104
|
try {
|
|
109
105
|
const eventStream = await executeSubscription(exeContext);
|
|
110
106
|
// Assert field returned an event stream, otherwise yield an error.
|
|
111
|
-
if (!(0,
|
|
112
|
-
throw new Error('Subscription field must return Async Iterable. ' + `Received: ${(0,
|
|
107
|
+
if (!(0, utils_1.isAsyncIterable)(eventStream)) {
|
|
108
|
+
throw new Error('Subscription field must return Async Iterable. ' + `Received: ${(0, utils_1.inspect)(eventStream)}.`);
|
|
113
109
|
}
|
|
114
110
|
return eventStream;
|
|
115
111
|
}
|
|
@@ -128,14 +124,14 @@ async function executeSubscription(exeContext) {
|
|
|
128
124
|
const { schema, fragments, operation, variableValues, rootValue } = exeContext;
|
|
129
125
|
const rootType = schema.getSubscriptionType();
|
|
130
126
|
if (rootType == null) {
|
|
131
|
-
throw
|
|
127
|
+
throw (0, utils_1.createGraphQLError)('Schema is not configured to execute subscription operation.', { nodes: operation });
|
|
132
128
|
}
|
|
133
|
-
const rootFields = (0,
|
|
129
|
+
const rootFields = (0, utils_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
|
|
134
130
|
const [responseName, fieldNodes] = [...rootFields.entries()][0];
|
|
135
131
|
const fieldDef = (0, execute_js_1.getFieldDef)(schema, rootType, fieldNodes[0]);
|
|
136
132
|
if (!fieldDef) {
|
|
137
133
|
const fieldName = fieldNodes[0].name.value;
|
|
138
|
-
throw
|
|
134
|
+
throw (0, utils_1.createGraphQLError)(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes });
|
|
139
135
|
}
|
|
140
136
|
const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name);
|
|
141
137
|
const info = (0, execute_js_1.buildResolveInfo)(exeContext, fieldDef, fieldNodes, rootType, path);
|
package/cjs/execution/values.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getDirectiveValues = exports.getArgumentValues = exports.getVariableValues = void 0;
|
|
4
|
-
const inspect_js_1 = require("graphql/jsutils/inspect.js");
|
|
5
|
-
const keyMap_js_1 = require("graphql/jsutils/keyMap.js");
|
|
6
4
|
const printPathArray_js_1 = require("graphql/jsutils/printPathArray.js");
|
|
7
5
|
const graphql_1 = require("graphql");
|
|
6
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
8
7
|
/**
|
|
9
8
|
* Prepares an object map of variableValues of the correct type based on the
|
|
10
9
|
* provided variable definitions and arbitrary input. If the input cannot be
|
|
@@ -20,7 +19,7 @@ function getVariableValues(schema, varDefNodes, inputs, options) {
|
|
|
20
19
|
try {
|
|
21
20
|
const coerced = coerceVariableValues(schema, varDefNodes, inputs, error => {
|
|
22
21
|
if (maxErrors != null && errors.length >= maxErrors) {
|
|
23
|
-
throw
|
|
22
|
+
throw (0, utils_1.createGraphQLError)('Too many errors processing variables, error limit reached. Execution aborted.');
|
|
24
23
|
}
|
|
25
24
|
errors.push(error);
|
|
26
25
|
});
|
|
@@ -44,7 +43,7 @@ function coerceVariableValues(schema, varDefNodes, inputs, onError) {
|
|
|
44
43
|
// Must use input types for variables. This should be caught during
|
|
45
44
|
// validation, however is checked again here for safety.
|
|
46
45
|
const varTypeStr = (0, graphql_1.print)(varDefNode.type);
|
|
47
|
-
onError(
|
|
46
|
+
onError((0, utils_1.createGraphQLError)(`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`, { nodes: varDefNode.type }));
|
|
48
47
|
continue;
|
|
49
48
|
}
|
|
50
49
|
if (!hasOwnProperty(inputs, varName)) {
|
|
@@ -52,8 +51,8 @@ function coerceVariableValues(schema, varDefNodes, inputs, onError) {
|
|
|
52
51
|
coercedValues[varName] = (0, graphql_1.valueFromAST)(varDefNode.defaultValue, varType);
|
|
53
52
|
}
|
|
54
53
|
else if ((0, graphql_1.isNonNullType)(varType)) {
|
|
55
|
-
const varTypeStr = (0,
|
|
56
|
-
onError(
|
|
54
|
+
const varTypeStr = (0, utils_1.inspect)(varType);
|
|
55
|
+
onError((0, utils_1.createGraphQLError)(`Variable "$${varName}" of required type "${varTypeStr}" was not provided.`, {
|
|
57
56
|
nodes: varDefNode,
|
|
58
57
|
}));
|
|
59
58
|
}
|
|
@@ -61,18 +60,18 @@ function coerceVariableValues(schema, varDefNodes, inputs, onError) {
|
|
|
61
60
|
}
|
|
62
61
|
const value = inputs[varName];
|
|
63
62
|
if (value === null && (0, graphql_1.isNonNullType)(varType)) {
|
|
64
|
-
const varTypeStr = (0,
|
|
65
|
-
onError(
|
|
63
|
+
const varTypeStr = (0, utils_1.inspect)(varType);
|
|
64
|
+
onError((0, utils_1.createGraphQLError)(`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`, {
|
|
66
65
|
nodes: varDefNode,
|
|
67
66
|
}));
|
|
68
67
|
continue;
|
|
69
68
|
}
|
|
70
69
|
coercedValues[varName] = (0, graphql_1.coerceInputValue)(value, varType, (path, invalidValue, error) => {
|
|
71
|
-
let prefix = `Variable "$${varName}" got invalid value ` + (0,
|
|
70
|
+
let prefix = `Variable "$${varName}" got invalid value ` + (0, utils_1.inspect)(invalidValue);
|
|
72
71
|
if (path.length > 0) {
|
|
73
72
|
prefix += ` at "${varName}${(0, printPathArray_js_1.printPathArray)(path)}"`;
|
|
74
73
|
}
|
|
75
|
-
onError(
|
|
74
|
+
onError((0, utils_1.createGraphQLError)(prefix + '; ' + error.message, {
|
|
76
75
|
nodes: varDefNode,
|
|
77
76
|
originalError: error.originalError,
|
|
78
77
|
}));
|
|
@@ -94,7 +93,10 @@ function getArgumentValues(def, node, variableValues) {
|
|
|
94
93
|
// FIXME: https://github.com/graphql/graphql-js/issues/2203
|
|
95
94
|
/* c8 ignore next */
|
|
96
95
|
const argumentNodes = (_a = node.arguments) !== null && _a !== void 0 ? _a : [];
|
|
97
|
-
const argNodeMap =
|
|
96
|
+
const argNodeMap = Object.create(null);
|
|
97
|
+
for (const arg of argumentNodes) {
|
|
98
|
+
argNodeMap[arg.name.value] = arg.value;
|
|
99
|
+
}
|
|
98
100
|
for (const argDef of def.args) {
|
|
99
101
|
const name = argDef.name;
|
|
100
102
|
const argType = argDef.type;
|
|
@@ -104,7 +106,7 @@ function getArgumentValues(def, node, variableValues) {
|
|
|
104
106
|
coercedValues[name] = argDef.defaultValue;
|
|
105
107
|
}
|
|
106
108
|
else if ((0, graphql_1.isNonNullType)(argType)) {
|
|
107
|
-
throw
|
|
109
|
+
throw (0, utils_1.createGraphQLError)(`Argument "${name}" of required type "${(0, utils_1.inspect)(argType)}" ` + 'was not provided.', {
|
|
108
110
|
nodes: node,
|
|
109
111
|
});
|
|
110
112
|
}
|
|
@@ -119,7 +121,7 @@ function getArgumentValues(def, node, variableValues) {
|
|
|
119
121
|
coercedValues[name] = argDef.defaultValue;
|
|
120
122
|
}
|
|
121
123
|
else if ((0, graphql_1.isNonNullType)(argType)) {
|
|
122
|
-
throw
|
|
124
|
+
throw (0, utils_1.createGraphQLError)(`Argument "${name}" of required type "${(0, utils_1.inspect)(argType)}" ` +
|
|
123
125
|
`was provided the variable "$${variableName}" which was not provided a runtime value.`, { nodes: valueNode });
|
|
124
126
|
}
|
|
125
127
|
continue;
|
|
@@ -127,7 +129,7 @@ function getArgumentValues(def, node, variableValues) {
|
|
|
127
129
|
isNull = variableValues[variableName] == null;
|
|
128
130
|
}
|
|
129
131
|
if (isNull && (0, graphql_1.isNonNullType)(argType)) {
|
|
130
|
-
throw
|
|
132
|
+
throw (0, utils_1.createGraphQLError)(`Argument "${name}" of non-null type "${(0, utils_1.inspect)(argType)}" ` + 'must not be null.', {
|
|
131
133
|
nodes: valueNode,
|
|
132
134
|
});
|
|
133
135
|
}
|
|
@@ -136,7 +138,7 @@ function getArgumentValues(def, node, variableValues) {
|
|
|
136
138
|
// Note: ValuesOfCorrectTypeRule validation should catch this before
|
|
137
139
|
// execution. This is a runtime check to ensure execution does not
|
|
138
140
|
// continue with an invalid argument value.
|
|
139
|
-
throw
|
|
141
|
+
throw (0, utils_1.createGraphQLError)(`Argument "${name}" has invalid value ${(0, graphql_1.print)(valueNode)}.`, { nodes: valueNode });
|
|
140
142
|
}
|
|
141
143
|
coercedValues[name] = coercedValue;
|
|
142
144
|
}
|