@graphql-tools/executor 0.0.1-alpha-20221025014654-e3be5659
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/README.md +3 -0
- package/cjs/execution/AccumulatorMap.js +21 -0
- package/cjs/execution/collectFields.js +114 -0
- package/cjs/execution/execute.js +792 -0
- package/cjs/execution/index.js +10 -0
- package/cjs/execution/mapAsyncIterator.js +53 -0
- package/cjs/execution/subscribe.js +164 -0
- package/cjs/execution/values.js +168 -0
- package/cjs/index.js +4 -0
- package/cjs/package.json +1 -0
- package/esm/execution/AccumulatorMap.js +17 -0
- package/esm/execution/collectFields.js +109 -0
- package/esm/execution/execute.js +779 -0
- package/esm/execution/index.js +5 -0
- package/esm/execution/mapAsyncIterator.js +49 -0
- package/esm/execution/subscribe.js +159 -0
- package/esm/execution/values.js +162 -0
- package/esm/index.js +1 -0
- package/package.json +58 -0
- package/typings/execution/AccumulatorMap.d.cts +7 -0
- package/typings/execution/AccumulatorMap.d.ts +7 -0
- package/typings/execution/collectFields.d.cts +27 -0
- package/typings/execution/collectFields.d.ts +27 -0
- package/typings/execution/execute.d.cts +196 -0
- package/typings/execution/execute.d.ts +196 -0
- package/typings/execution/index.d.cts +5 -0
- package/typings/execution/index.d.ts +5 -0
- package/typings/execution/mapAsyncIterator.d.cts +6 -0
- package/typings/execution/mapAsyncIterator.d.ts +6 -0
- package/typings/execution/subscribe.d.cts +56 -0
- package/typings/execution/subscribe.d.ts +56 -0
- package/typings/execution/values.d.cts +54 -0
- package/typings/execution/values.d.ts +54 -0
- package/typings/index.d.cts +1 -0
- package/typings/index.d.ts +1 -0
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
+
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
|
+
const graphql_1 = require("graphql");
|
|
16
|
+
const collectFields_js_1 = require("./collectFields.js");
|
|
17
|
+
const mapAsyncIterator_js_1 = require("./mapAsyncIterator.js");
|
|
18
|
+
const values_js_1 = require("./values.js");
|
|
19
|
+
// This file contains a lot of such errors but we plan to refactor it anyway
|
|
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));
|
|
27
|
+
/**
|
|
28
|
+
* Implements the "Executing requests" section of the GraphQL specification.
|
|
29
|
+
*
|
|
30
|
+
* Returns either a synchronous ExecutionResult (if all encountered resolvers
|
|
31
|
+
* are synchronous), or a Promise of an ExecutionResult that will eventually be
|
|
32
|
+
* resolved and never rejected.
|
|
33
|
+
*
|
|
34
|
+
* If the arguments to this function do not result in a legal execution context,
|
|
35
|
+
* a GraphQLError will be thrown immediately explaining the invalid input.
|
|
36
|
+
*/
|
|
37
|
+
function execute(args) {
|
|
38
|
+
// If a valid execution context cannot be created due to incorrect arguments,
|
|
39
|
+
// a "Response" with only errors is returned.
|
|
40
|
+
const exeContext = buildExecutionContext(args);
|
|
41
|
+
// Return early errors if execution context failed.
|
|
42
|
+
if (!('schema' in exeContext)) {
|
|
43
|
+
return { errors: exeContext };
|
|
44
|
+
}
|
|
45
|
+
return executeImpl(exeContext);
|
|
46
|
+
}
|
|
47
|
+
exports.execute = execute;
|
|
48
|
+
function executeImpl(exeContext) {
|
|
49
|
+
// Return a Promise that will eventually resolve to the data described by
|
|
50
|
+
// The "Response" section of the GraphQL specification.
|
|
51
|
+
//
|
|
52
|
+
// If errors are encountered while executing a GraphQL field, only that
|
|
53
|
+
// field and its descendants will be omitted, and sibling fields will still
|
|
54
|
+
// be executed. An execution which encounters errors will still result in a
|
|
55
|
+
// resolved Promise.
|
|
56
|
+
//
|
|
57
|
+
// Errors from sub-fields of a NonNull type may propagate to the top level,
|
|
58
|
+
// at which point we still log the error and null the parent field, which
|
|
59
|
+
// in this case is the entire response.
|
|
60
|
+
try {
|
|
61
|
+
const result = executeOperation(exeContext);
|
|
62
|
+
if ((0, isPromise_js_1.isPromise)(result)) {
|
|
63
|
+
return result.then(data => buildResponse(data, exeContext.errors), error => {
|
|
64
|
+
exeContext.errors.push(error);
|
|
65
|
+
return buildResponse(null, exeContext.errors);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return buildResponse(result, exeContext.errors);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
exeContext.errors.push(error);
|
|
72
|
+
return buildResponse(null, exeContext.errors);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Also implements the "Executing requests" section of the GraphQL specification.
|
|
77
|
+
* However, it guarantees to complete synchronously (or throw an error) assuming
|
|
78
|
+
* that all field resolvers are also synchronous.
|
|
79
|
+
*/
|
|
80
|
+
function executeSync(args) {
|
|
81
|
+
const result = execute(args);
|
|
82
|
+
// Assert that the execution was synchronous.
|
|
83
|
+
if ((0, isPromise_js_1.isPromise)(result)) {
|
|
84
|
+
throw new Error('GraphQL execution failed to complete synchronously.');
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
exports.executeSync = executeSync;
|
|
89
|
+
/**
|
|
90
|
+
* Given a completed execution context and data, build the `{ errors, data }`
|
|
91
|
+
* response defined by the "Response" section of the GraphQL specification.
|
|
92
|
+
*/
|
|
93
|
+
function buildResponse(data, errors) {
|
|
94
|
+
return errors.length === 0 ? { data } : { errors, data };
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Essential assertions before executing to provide developer feedback for
|
|
98
|
+
* improper use of the GraphQL library.
|
|
99
|
+
*
|
|
100
|
+
* @internal
|
|
101
|
+
*/
|
|
102
|
+
function assertValidExecutionArguments(schema, document, rawVariableValues) {
|
|
103
|
+
(0, devAssert_js_1.devAssert)(!!document, 'Must provide document.');
|
|
104
|
+
// If the schema used for execution is invalid, throw an error.
|
|
105
|
+
(0, graphql_1.assertValidSchema)(schema);
|
|
106
|
+
// Variables, if provided, must be an object.
|
|
107
|
+
(0, devAssert_js_1.devAssert)(rawVariableValues == null || (0, isObjectLike_js_1.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
|
+
}
|
|
109
|
+
exports.assertValidExecutionArguments = assertValidExecutionArguments;
|
|
110
|
+
/**
|
|
111
|
+
* Constructs a ExecutionContext object from the arguments passed to
|
|
112
|
+
* execute, which we will pass throughout the other execution methods.
|
|
113
|
+
*
|
|
114
|
+
* Throws a GraphQLError if a valid execution context cannot be created.
|
|
115
|
+
*
|
|
116
|
+
* TODO: consider no longer exporting this function
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
function buildExecutionContext(args) {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
const { schema, document, rootValue, contextValue, variableValues: rawVariableValues, operationName, fieldResolver, typeResolver, subscribeFieldResolver, } = args;
|
|
122
|
+
// If the schema used for execution is invalid, throw an error.
|
|
123
|
+
(0, graphql_1.assertValidSchema)(schema);
|
|
124
|
+
let operation;
|
|
125
|
+
const fragments = Object.create(null);
|
|
126
|
+
for (const definition of document.definitions) {
|
|
127
|
+
switch (definition.kind) {
|
|
128
|
+
case graphql_1.Kind.OPERATION_DEFINITION:
|
|
129
|
+
if (operationName == null) {
|
|
130
|
+
if (operation !== undefined) {
|
|
131
|
+
return [new graphql_1.GraphQLError('Must provide operation name if query contains multiple operations.')];
|
|
132
|
+
}
|
|
133
|
+
operation = definition;
|
|
134
|
+
}
|
|
135
|
+
else if (((_a = definition.name) === null || _a === void 0 ? void 0 : _a.value) === operationName) {
|
|
136
|
+
operation = definition;
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
case graphql_1.Kind.FRAGMENT_DEFINITION:
|
|
140
|
+
fragments[definition.name.value] = definition;
|
|
141
|
+
break;
|
|
142
|
+
default:
|
|
143
|
+
// ignore non-executable definitions
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (!operation) {
|
|
147
|
+
if (operationName != null) {
|
|
148
|
+
return [new graphql_1.GraphQLError(`Unknown operation named "${operationName}".`)];
|
|
149
|
+
}
|
|
150
|
+
return [new graphql_1.GraphQLError('Must provide an operation.')];
|
|
151
|
+
}
|
|
152
|
+
// FIXME: https://github.com/graphql/graphql-js/issues/2203
|
|
153
|
+
/* c8 ignore next */
|
|
154
|
+
const variableDefinitions = (_b = operation.variableDefinitions) !== null && _b !== void 0 ? _b : [];
|
|
155
|
+
const coercedVariableValues = (0, values_js_1.getVariableValues)(schema, variableDefinitions, rawVariableValues !== null && rawVariableValues !== void 0 ? rawVariableValues : {}, {
|
|
156
|
+
maxErrors: 50,
|
|
157
|
+
});
|
|
158
|
+
if (coercedVariableValues.errors) {
|
|
159
|
+
return coercedVariableValues.errors;
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
schema,
|
|
163
|
+
fragments,
|
|
164
|
+
rootValue,
|
|
165
|
+
contextValue,
|
|
166
|
+
operation,
|
|
167
|
+
variableValues: coercedVariableValues.coerced,
|
|
168
|
+
fieldResolver: fieldResolver !== null && fieldResolver !== void 0 ? fieldResolver : exports.defaultFieldResolver,
|
|
169
|
+
typeResolver: typeResolver !== null && typeResolver !== void 0 ? typeResolver : exports.defaultTypeResolver,
|
|
170
|
+
subscribeFieldResolver: subscribeFieldResolver !== null && subscribeFieldResolver !== void 0 ? subscribeFieldResolver : exports.defaultFieldResolver,
|
|
171
|
+
errors: [],
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
exports.buildExecutionContext = buildExecutionContext;
|
|
175
|
+
function buildPerEventExecutionContext(exeContext, payload) {
|
|
176
|
+
return {
|
|
177
|
+
...exeContext,
|
|
178
|
+
rootValue: payload,
|
|
179
|
+
errors: [],
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Implements the "Executing operations" section of the spec.
|
|
184
|
+
*/
|
|
185
|
+
function executeOperation(exeContext) {
|
|
186
|
+
const { operation, schema, fragments, variableValues, rootValue } = exeContext;
|
|
187
|
+
const rootType = schema.getRootType(operation.operation);
|
|
188
|
+
if (rootType == null) {
|
|
189
|
+
throw new graphql_1.GraphQLError(`Schema is not configured to execute ${operation.operation} operation.`, {
|
|
190
|
+
nodes: operation,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
const rootFields = (0, collectFields_js_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
|
|
194
|
+
const path = undefined;
|
|
195
|
+
switch (operation.operation) {
|
|
196
|
+
case graphql_1.OperationTypeNode.QUERY:
|
|
197
|
+
return executeFields(exeContext, rootType, rootValue, path, rootFields);
|
|
198
|
+
case graphql_1.OperationTypeNode.MUTATION:
|
|
199
|
+
return executeFieldsSerially(exeContext, rootType, rootValue, path, rootFields);
|
|
200
|
+
case graphql_1.OperationTypeNode.SUBSCRIPTION:
|
|
201
|
+
// TODO: deprecate `subscribe` and move all logic here
|
|
202
|
+
// Temporary solution until we finish merging execute and subscribe together
|
|
203
|
+
return executeFields(exeContext, rootType, rootValue, path, rootFields);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Implements the "Executing selection sets" section of the spec
|
|
208
|
+
* for fields that must be executed serially.
|
|
209
|
+
*/
|
|
210
|
+
function executeFieldsSerially(exeContext, parentType, sourceValue, path, fields) {
|
|
211
|
+
return (0, promiseReduce_js_1.promiseReduce)(fields.entries(), (results, [responseName, fieldNodes]) => {
|
|
212
|
+
const fieldPath = (0, Path_js_1.addPath)(path, responseName, parentType.name);
|
|
213
|
+
const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath);
|
|
214
|
+
if (result === undefined) {
|
|
215
|
+
return results;
|
|
216
|
+
}
|
|
217
|
+
if ((0, isPromise_js_1.isPromise)(result)) {
|
|
218
|
+
return result.then(resolvedResult => {
|
|
219
|
+
results[responseName] = resolvedResult;
|
|
220
|
+
return results;
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
results[responseName] = result;
|
|
224
|
+
return results;
|
|
225
|
+
}, Object.create(null));
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Implements the "Executing selection sets" section of the spec
|
|
229
|
+
* for fields that may be executed in parallel.
|
|
230
|
+
*/
|
|
231
|
+
function executeFields(exeContext, parentType, sourceValue, path, fields) {
|
|
232
|
+
const results = Object.create(null);
|
|
233
|
+
let containsPromise = false;
|
|
234
|
+
for (const [responseName, fieldNodes] of fields.entries()) {
|
|
235
|
+
const fieldPath = (0, Path_js_1.addPath)(path, responseName, parentType.name);
|
|
236
|
+
const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath);
|
|
237
|
+
if (result !== undefined) {
|
|
238
|
+
results[responseName] = result;
|
|
239
|
+
if ((0, isPromise_js_1.isPromise)(result)) {
|
|
240
|
+
containsPromise = true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// If there are no promises, we can just return the object
|
|
245
|
+
if (!containsPromise) {
|
|
246
|
+
return results;
|
|
247
|
+
}
|
|
248
|
+
// Otherwise, results is a map from field name to the result of resolving that
|
|
249
|
+
// field, which is possibly a promise. Return a promise that will return this
|
|
250
|
+
// same map, but with any promises replaced with the values they resolved to.
|
|
251
|
+
return (0, promiseForObject_js_1.promiseForObject)(results);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Implements the "Executing fields" section of the spec
|
|
255
|
+
* In particular, this function figures out the value that the field returns by
|
|
256
|
+
* calling its resolve function, then calls completeValue to complete promises,
|
|
257
|
+
* serialize scalars, or execute the sub-selection-set for objects.
|
|
258
|
+
*/
|
|
259
|
+
function executeField(exeContext, parentType, source, fieldNodes, path) {
|
|
260
|
+
var _a;
|
|
261
|
+
const fieldDef = getFieldDef(exeContext.schema, parentType, fieldNodes[0]);
|
|
262
|
+
if (!fieldDef) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
const returnType = fieldDef.type;
|
|
266
|
+
const resolveFn = (_a = fieldDef.resolve) !== null && _a !== void 0 ? _a : exeContext.fieldResolver;
|
|
267
|
+
const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path);
|
|
268
|
+
// Get the resolve function, regardless of if its result is normal or abrupt (error).
|
|
269
|
+
try {
|
|
270
|
+
// Build a JS object of arguments from the field.arguments AST, using the
|
|
271
|
+
// variables scope to fulfill any variable references.
|
|
272
|
+
// TODO: find a way to memoize, in case this field is within a List type.
|
|
273
|
+
const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], exeContext.variableValues);
|
|
274
|
+
// The resolve function's optional third argument is a context value that
|
|
275
|
+
// is provided to every resolve function within an execution. It is commonly
|
|
276
|
+
// used to represent an authenticated user, or request-specific caches.
|
|
277
|
+
const contextValue = exeContext.contextValue;
|
|
278
|
+
const result = resolveFn(source, args, contextValue, info);
|
|
279
|
+
let completed;
|
|
280
|
+
if ((0, isPromise_js_1.isPromise)(result)) {
|
|
281
|
+
completed = result.then(resolved => completeValue(exeContext, returnType, fieldNodes, info, path, resolved));
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
completed = completeValue(exeContext, returnType, fieldNodes, info, path, result);
|
|
285
|
+
}
|
|
286
|
+
if ((0, isPromise_js_1.isPromise)(completed)) {
|
|
287
|
+
// Note: we don't rely on a `catch` method, but we do expect "thenable"
|
|
288
|
+
// to take a second callback for the error case.
|
|
289
|
+
return completed.then(undefined, rawError => {
|
|
290
|
+
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(path));
|
|
291
|
+
return handleFieldError(error, returnType, exeContext);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
return completed;
|
|
295
|
+
}
|
|
296
|
+
catch (rawError) {
|
|
297
|
+
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(path));
|
|
298
|
+
return handleFieldError(error, returnType, exeContext);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* TODO: consider no longer exporting this function
|
|
303
|
+
* @internal
|
|
304
|
+
*/
|
|
305
|
+
function buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path) {
|
|
306
|
+
// The resolve function's optional fourth argument is a collection of
|
|
307
|
+
// information about the current execution state.
|
|
308
|
+
return {
|
|
309
|
+
fieldName: fieldDef.name,
|
|
310
|
+
fieldNodes,
|
|
311
|
+
returnType: fieldDef.type,
|
|
312
|
+
parentType,
|
|
313
|
+
path,
|
|
314
|
+
schema: exeContext.schema,
|
|
315
|
+
fragments: exeContext.fragments,
|
|
316
|
+
rootValue: exeContext.rootValue,
|
|
317
|
+
operation: exeContext.operation,
|
|
318
|
+
variableValues: exeContext.variableValues,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
exports.buildResolveInfo = buildResolveInfo;
|
|
322
|
+
function handleFieldError(error, returnType, exeContext) {
|
|
323
|
+
// If the field type is non-nullable, then it is resolved without any
|
|
324
|
+
// protection from errors, however it still properly locates the error.
|
|
325
|
+
if ((0, graphql_1.isNonNullType)(returnType)) {
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
328
|
+
// Otherwise, error protection is applied, logging the error and resolving
|
|
329
|
+
// a null value for this field if one is encountered.
|
|
330
|
+
exeContext.errors.push(error);
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Implements the instructions for completeValue as defined in the
|
|
335
|
+
* "Value Completion" section of the spec.
|
|
336
|
+
*
|
|
337
|
+
* If the field type is Non-Null, then this recursively completes the value
|
|
338
|
+
* for the inner type. It throws a field error if that completion returns null,
|
|
339
|
+
* as per the "Nullability" section of the spec.
|
|
340
|
+
*
|
|
341
|
+
* If the field type is a List, then this recursively completes the value
|
|
342
|
+
* for the inner type on each item in the list.
|
|
343
|
+
*
|
|
344
|
+
* If the field type is a Scalar or Enum, ensures the completed value is a legal
|
|
345
|
+
* value of the type by calling the `serialize` method of GraphQL type
|
|
346
|
+
* definition.
|
|
347
|
+
*
|
|
348
|
+
* If the field is an abstract type, determine the runtime type of the value
|
|
349
|
+
* and then complete based on that type
|
|
350
|
+
*
|
|
351
|
+
* Otherwise, the field type expects a sub-selection set, and will complete the
|
|
352
|
+
* value by executing all sub-selections.
|
|
353
|
+
*/
|
|
354
|
+
function completeValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
355
|
+
// If result is an Error, throw a located error.
|
|
356
|
+
if (result instanceof Error) {
|
|
357
|
+
throw result;
|
|
358
|
+
}
|
|
359
|
+
// If field type is NonNull, complete for inner type, and throw field error
|
|
360
|
+
// if result is null.
|
|
361
|
+
if ((0, graphql_1.isNonNullType)(returnType)) {
|
|
362
|
+
const completed = completeValue(exeContext, returnType.ofType, fieldNodes, info, path, result);
|
|
363
|
+
if (completed === null) {
|
|
364
|
+
throw new Error(`Cannot return null for non-nullable field ${info.parentType.name}.${info.fieldName}.`);
|
|
365
|
+
}
|
|
366
|
+
return completed;
|
|
367
|
+
}
|
|
368
|
+
// If result value is null or undefined then return null.
|
|
369
|
+
if (result == null) {
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
// If field type is List, complete each item in the list with the inner type
|
|
373
|
+
if ((0, graphql_1.isListType)(returnType)) {
|
|
374
|
+
return completeListValue(exeContext, returnType, fieldNodes, info, path, result);
|
|
375
|
+
}
|
|
376
|
+
// If field type is a leaf type, Scalar or Enum, serialize to a valid value,
|
|
377
|
+
// returning null if serialization is not possible.
|
|
378
|
+
if ((0, graphql_1.isLeafType)(returnType)) {
|
|
379
|
+
return completeLeafValue(returnType, result);
|
|
380
|
+
}
|
|
381
|
+
// If field type is an abstract type, Interface or Union, determine the
|
|
382
|
+
// runtime Object type and complete for that type.
|
|
383
|
+
if ((0, graphql_1.isAbstractType)(returnType)) {
|
|
384
|
+
return completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result);
|
|
385
|
+
}
|
|
386
|
+
// If field type is Object, execute and complete all sub-selections.
|
|
387
|
+
if ((0, graphql_1.isObjectType)(returnType)) {
|
|
388
|
+
return completeObjectValue(exeContext, returnType, fieldNodes, info, path, result);
|
|
389
|
+
}
|
|
390
|
+
/* c8 ignore next 6 */
|
|
391
|
+
// Not reachable, all possible output types have been considered.
|
|
392
|
+
(0, invariant_js_1.invariant)(false, 'Cannot complete value of unexpected output type: ' + (0, inspect_js_1.inspect)(returnType));
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Complete a async iterator value by completing the result and calling
|
|
396
|
+
* recursively until all the results are completed.
|
|
397
|
+
*/
|
|
398
|
+
async function completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info, path, iterator) {
|
|
399
|
+
let containsPromise = false;
|
|
400
|
+
const completedResults = [];
|
|
401
|
+
let index = 0;
|
|
402
|
+
while (true) {
|
|
403
|
+
const fieldPath = (0, Path_js_1.addPath)(path, index, undefined);
|
|
404
|
+
try {
|
|
405
|
+
const { value, done } = await iterator.next();
|
|
406
|
+
if (done) {
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
// TODO can the error checking logic be consolidated with completeListValue?
|
|
411
|
+
const completedItem = completeValue(exeContext, itemType, fieldNodes, info, fieldPath, value);
|
|
412
|
+
if ((0, isPromise_js_1.isPromise)(completedItem)) {
|
|
413
|
+
containsPromise = true;
|
|
414
|
+
}
|
|
415
|
+
completedResults.push(completedItem);
|
|
416
|
+
}
|
|
417
|
+
catch (rawError) {
|
|
418
|
+
completedResults.push(null);
|
|
419
|
+
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(fieldPath));
|
|
420
|
+
handleFieldError(error, itemType, exeContext);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
catch (rawError) {
|
|
424
|
+
completedResults.push(null);
|
|
425
|
+
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(fieldPath));
|
|
426
|
+
handleFieldError(error, itemType, exeContext);
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
index += 1;
|
|
430
|
+
}
|
|
431
|
+
return containsPromise ? Promise.all(completedResults) : completedResults;
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Complete a list value by completing each item in the list with the
|
|
435
|
+
* inner type
|
|
436
|
+
*/
|
|
437
|
+
function completeListValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
438
|
+
const itemType = returnType.ofType;
|
|
439
|
+
if ((0, isAsyncIterable_js_1.isAsyncIterable)(result)) {
|
|
440
|
+
const iterator = result[Symbol.asyncIterator]();
|
|
441
|
+
return completeAsyncIteratorValue(exeContext, itemType, fieldNodes, info, path, iterator);
|
|
442
|
+
}
|
|
443
|
+
if (!(0, isIterableObject_js_1.isIterableObject)(result)) {
|
|
444
|
+
throw new graphql_1.GraphQLError(`Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`);
|
|
445
|
+
}
|
|
446
|
+
// This is specified as a simple map, however we're optimizing the path
|
|
447
|
+
// where the list contains no Promises by avoiding creating another Promise.
|
|
448
|
+
let containsPromise = false;
|
|
449
|
+
const completedResults = Array.from(result, (item, index) => {
|
|
450
|
+
// No need to modify the info object containing the path,
|
|
451
|
+
// since from here on it is not ever accessed by resolver functions.
|
|
452
|
+
const itemPath = (0, Path_js_1.addPath)(path, index, undefined);
|
|
453
|
+
try {
|
|
454
|
+
let completedItem;
|
|
455
|
+
if ((0, isPromise_js_1.isPromise)(item)) {
|
|
456
|
+
completedItem = item.then(resolved => completeValue(exeContext, itemType, fieldNodes, info, itemPath, resolved));
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
completedItem = completeValue(exeContext, itemType, fieldNodes, info, itemPath, item);
|
|
460
|
+
}
|
|
461
|
+
if ((0, isPromise_js_1.isPromise)(completedItem)) {
|
|
462
|
+
containsPromise = true;
|
|
463
|
+
// Note: we don't rely on a `catch` method, but we do expect "thenable"
|
|
464
|
+
// to take a second callback for the error case.
|
|
465
|
+
return completedItem.then(undefined, rawError => {
|
|
466
|
+
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(itemPath));
|
|
467
|
+
return handleFieldError(error, itemType, exeContext);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
return completedItem;
|
|
471
|
+
}
|
|
472
|
+
catch (rawError) {
|
|
473
|
+
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(itemPath));
|
|
474
|
+
return handleFieldError(error, itemType, exeContext);
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
return containsPromise ? Promise.all(completedResults) : completedResults;
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Complete a Scalar or Enum by serializing to a valid value, returning
|
|
481
|
+
* null if serialization is not possible.
|
|
482
|
+
*/
|
|
483
|
+
function completeLeafValue(returnType, result) {
|
|
484
|
+
const serializedResult = returnType.serialize(result);
|
|
485
|
+
if (serializedResult == null) {
|
|
486
|
+
throw new Error(`Expected \`${(0, inspect_js_1.inspect)(returnType)}.serialize(${(0, inspect_js_1.inspect)(result)})\` to ` +
|
|
487
|
+
`return non-nullable value, returned: ${(0, inspect_js_1.inspect)(serializedResult)}`);
|
|
488
|
+
}
|
|
489
|
+
return serializedResult;
|
|
490
|
+
}
|
|
491
|
+
/**
|
|
492
|
+
* Complete a value of an abstract type by determining the runtime object type
|
|
493
|
+
* of that value, then complete the value for that type.
|
|
494
|
+
*/
|
|
495
|
+
function completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
496
|
+
var _a;
|
|
497
|
+
const resolveTypeFn = (_a = returnType.resolveType) !== null && _a !== void 0 ? _a : exeContext.typeResolver;
|
|
498
|
+
const contextValue = exeContext.contextValue;
|
|
499
|
+
const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
|
|
500
|
+
if ((0, isPromise_js_1.isPromise)(runtimeType)) {
|
|
501
|
+
return runtimeType.then(resolvedRuntimeType => completeObjectValue(exeContext, ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result));
|
|
502
|
+
}
|
|
503
|
+
return completeObjectValue(exeContext, ensureValidRuntimeType(runtimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result);
|
|
504
|
+
}
|
|
505
|
+
function ensureValidRuntimeType(runtimeTypeName, exeContext, returnType, fieldNodes, info, result) {
|
|
506
|
+
if (runtimeTypeName == null) {
|
|
507
|
+
throw new graphql_1.GraphQLError(`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
|
+
}
|
|
509
|
+
// releases before 16.0.0 supported returning `GraphQLObjectType` from `resolveType`
|
|
510
|
+
// TODO: remove in 17.0.0 release
|
|
511
|
+
if ((0, graphql_1.isObjectType)(runtimeTypeName)) {
|
|
512
|
+
throw new graphql_1.GraphQLError('Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.');
|
|
513
|
+
}
|
|
514
|
+
if (typeof runtimeTypeName !== 'string') {
|
|
515
|
+
throw new graphql_1.GraphQLError(`Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
|
|
516
|
+
`value ${(0, inspect_js_1.inspect)(result)}, received "${(0, inspect_js_1.inspect)(runtimeTypeName)}".`);
|
|
517
|
+
}
|
|
518
|
+
const runtimeType = exeContext.schema.getType(runtimeTypeName);
|
|
519
|
+
if (runtimeType == null) {
|
|
520
|
+
throw new graphql_1.GraphQLError(`Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`, { nodes: fieldNodes });
|
|
521
|
+
}
|
|
522
|
+
if (!(0, graphql_1.isObjectType)(runtimeType)) {
|
|
523
|
+
throw new graphql_1.GraphQLError(`Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`, { nodes: fieldNodes });
|
|
524
|
+
}
|
|
525
|
+
if (!exeContext.schema.isSubType(returnType, runtimeType)) {
|
|
526
|
+
throw new graphql_1.GraphQLError(`Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`, { nodes: fieldNodes });
|
|
527
|
+
}
|
|
528
|
+
return runtimeType;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Complete an Object value by executing all sub-selections.
|
|
532
|
+
*/
|
|
533
|
+
function completeObjectValue(exeContext, returnType, fieldNodes, info, path, result) {
|
|
534
|
+
// Collect sub-fields to execute to complete this value.
|
|
535
|
+
const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes);
|
|
536
|
+
// If there is an isTypeOf predicate function, call it with the
|
|
537
|
+
// current result. If isTypeOf returns false, then raise an error rather
|
|
538
|
+
// than continuing execution.
|
|
539
|
+
if (returnType.isTypeOf) {
|
|
540
|
+
const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
|
|
541
|
+
if ((0, isPromise_js_1.isPromise)(isTypeOf)) {
|
|
542
|
+
return isTypeOf.then(resolvedIsTypeOf => {
|
|
543
|
+
if (!resolvedIsTypeOf) {
|
|
544
|
+
throw invalidReturnTypeError(returnType, result, fieldNodes);
|
|
545
|
+
}
|
|
546
|
+
return executeFields(exeContext, returnType, result, path, subFieldNodes);
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
if (!isTypeOf) {
|
|
550
|
+
throw invalidReturnTypeError(returnType, result, fieldNodes);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return executeFields(exeContext, returnType, result, path, subFieldNodes);
|
|
554
|
+
}
|
|
555
|
+
function invalidReturnTypeError(returnType, result, fieldNodes) {
|
|
556
|
+
return new graphql_1.GraphQLError(`Expected value of type "${returnType.name}" but got: ${(0, inspect_js_1.inspect)(result)}.`, {
|
|
557
|
+
nodes: fieldNodes,
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* If a resolveType function is not given, then a default resolve behavior is
|
|
562
|
+
* used which attempts two strategies:
|
|
563
|
+
*
|
|
564
|
+
* First, See if the provided value has a `__typename` field defined, if so, use
|
|
565
|
+
* that value as name of the resolved type.
|
|
566
|
+
*
|
|
567
|
+
* Otherwise, test each possible type for the abstract type by calling
|
|
568
|
+
* isTypeOf for the object being coerced, returning the first type that matches.
|
|
569
|
+
*/
|
|
570
|
+
const defaultTypeResolver = function (value, contextValue, info, abstractType) {
|
|
571
|
+
// First, look for `__typename`.
|
|
572
|
+
if ((0, isObjectLike_js_1.isObjectLike)(value) && typeof value['__typename'] === 'string') {
|
|
573
|
+
return value['__typename'];
|
|
574
|
+
}
|
|
575
|
+
// Otherwise, test each possible type.
|
|
576
|
+
const possibleTypes = info.schema.getPossibleTypes(abstractType);
|
|
577
|
+
const promisedIsTypeOfResults = [];
|
|
578
|
+
for (let i = 0; i < possibleTypes.length; i++) {
|
|
579
|
+
const type = possibleTypes[i];
|
|
580
|
+
if (type.isTypeOf) {
|
|
581
|
+
const isTypeOfResult = type.isTypeOf(value, contextValue, info);
|
|
582
|
+
if ((0, isPromise_js_1.isPromise)(isTypeOfResult)) {
|
|
583
|
+
promisedIsTypeOfResults[i] = isTypeOfResult;
|
|
584
|
+
}
|
|
585
|
+
else if (isTypeOfResult) {
|
|
586
|
+
return type.name;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (promisedIsTypeOfResults.length) {
|
|
591
|
+
return Promise.all(promisedIsTypeOfResults).then(isTypeOfResults => {
|
|
592
|
+
for (let i = 0; i < isTypeOfResults.length; i++) {
|
|
593
|
+
if (isTypeOfResults[i]) {
|
|
594
|
+
return possibleTypes[i].name;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
exports.defaultTypeResolver = defaultTypeResolver;
|
|
601
|
+
/**
|
|
602
|
+
* If a resolve function is not given, then a default resolve behavior is used
|
|
603
|
+
* which takes the property of the source object of the same name as the field
|
|
604
|
+
* and returns it as the result, or if it's a function, returns the result
|
|
605
|
+
* of calling that function while passing along args and context value.
|
|
606
|
+
*/
|
|
607
|
+
const defaultFieldResolver = function (source, args, contextValue, info) {
|
|
608
|
+
// ensure source is a value for which property access is acceptable.
|
|
609
|
+
if ((0, isObjectLike_js_1.isObjectLike)(source) || typeof source === 'function') {
|
|
610
|
+
const property = source[info.fieldName];
|
|
611
|
+
if (typeof property === 'function') {
|
|
612
|
+
return source[info.fieldName](args, contextValue, info);
|
|
613
|
+
}
|
|
614
|
+
return property;
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
exports.defaultFieldResolver = defaultFieldResolver;
|
|
618
|
+
/**
|
|
619
|
+
* Implements the "Subscribe" algorithm described in the GraphQL specification.
|
|
620
|
+
*
|
|
621
|
+
* Returns a Promise which resolves to either an AsyncIterator (if successful)
|
|
622
|
+
* or an ExecutionResult (error). The promise will be rejected if the schema or
|
|
623
|
+
* other arguments to this function are invalid, or if the resolved event stream
|
|
624
|
+
* is not an async iterable.
|
|
625
|
+
*
|
|
626
|
+
* If the client-provided arguments to this function do not result in a
|
|
627
|
+
* compliant subscription, a GraphQL Response (ExecutionResult) with
|
|
628
|
+
* descriptive errors and no data will be returned.
|
|
629
|
+
*
|
|
630
|
+
* If the source stream could not be created due to faulty subscription
|
|
631
|
+
* resolver logic or underlying systems, the promise will resolve to a single
|
|
632
|
+
* ExecutionResult containing `errors` and no `data`.
|
|
633
|
+
*
|
|
634
|
+
* If the operation succeeded, the promise resolves to an AsyncIterator, which
|
|
635
|
+
* yields a stream of ExecutionResults representing the response stream.
|
|
636
|
+
*
|
|
637
|
+
* Accepts either an object with named arguments, or individual arguments.
|
|
638
|
+
*/
|
|
639
|
+
function subscribe(args) {
|
|
640
|
+
// If a valid execution context cannot be created due to incorrect arguments,
|
|
641
|
+
// a "Response" with only errors is returned.
|
|
642
|
+
const exeContext = buildExecutionContext(args);
|
|
643
|
+
// Return early errors if execution context failed.
|
|
644
|
+
if (!('schema' in exeContext)) {
|
|
645
|
+
return { errors: exeContext };
|
|
646
|
+
}
|
|
647
|
+
const resultOrStream = createSourceEventStreamImpl(exeContext);
|
|
648
|
+
if ((0, isPromise_js_1.isPromise)(resultOrStream)) {
|
|
649
|
+
return resultOrStream.then(resolvedResultOrStream => mapSourceToResponse(exeContext, resolvedResultOrStream));
|
|
650
|
+
}
|
|
651
|
+
return mapSourceToResponse(exeContext, resultOrStream);
|
|
652
|
+
}
|
|
653
|
+
exports.subscribe = subscribe;
|
|
654
|
+
function mapSourceToResponse(exeContext, resultOrStream) {
|
|
655
|
+
if (!(0, isAsyncIterable_js_1.isAsyncIterable)(resultOrStream)) {
|
|
656
|
+
return resultOrStream;
|
|
657
|
+
}
|
|
658
|
+
// For each payload yielded from a subscription, map it over the normal
|
|
659
|
+
// GraphQL `execute` function, with `payload` as the rootValue.
|
|
660
|
+
// This implements the "MapSourceToResponseEvent" algorithm described in
|
|
661
|
+
// the GraphQL specification. The `execute` function provides the
|
|
662
|
+
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
|
|
663
|
+
// "ExecuteQuery" algorithm, for which `execute` is also used.
|
|
664
|
+
return (0, mapAsyncIterator_js_1.mapAsyncIterator)(resultOrStream, (payload) => executeImpl(buildPerEventExecutionContext(exeContext, payload)));
|
|
665
|
+
}
|
|
666
|
+
/**
|
|
667
|
+
* Implements the "CreateSourceEventStream" algorithm described in the
|
|
668
|
+
* GraphQL specification, resolving the subscription source event stream.
|
|
669
|
+
*
|
|
670
|
+
* Returns a Promise which resolves to either an AsyncIterable (if successful)
|
|
671
|
+
* or an ExecutionResult (error). The promise will be rejected if the schema or
|
|
672
|
+
* other arguments to this function are invalid, or if the resolved event stream
|
|
673
|
+
* is not an async iterable.
|
|
674
|
+
*
|
|
675
|
+
* If the client-provided arguments to this function do not result in a
|
|
676
|
+
* compliant subscription, a GraphQL Response (ExecutionResult) with
|
|
677
|
+
* descriptive errors and no data will be returned.
|
|
678
|
+
*
|
|
679
|
+
* If the the source stream could not be created due to faulty subscription
|
|
680
|
+
* resolver logic or underlying systems, the promise will resolve to a single
|
|
681
|
+
* ExecutionResult containing `errors` and no `data`.
|
|
682
|
+
*
|
|
683
|
+
* If the operation succeeded, the promise resolves to the AsyncIterable for the
|
|
684
|
+
* event stream returned by the resolver.
|
|
685
|
+
*
|
|
686
|
+
* A Source Event Stream represents a sequence of events, each of which triggers
|
|
687
|
+
* a GraphQL execution for that event.
|
|
688
|
+
*
|
|
689
|
+
* This may be useful when hosting the stateful subscription service in a
|
|
690
|
+
* different process or machine than the stateless GraphQL execution engine,
|
|
691
|
+
* or otherwise separating these two steps. For more on this, see the
|
|
692
|
+
* "Supporting Subscriptions at Scale" information in the GraphQL specification.
|
|
693
|
+
*/
|
|
694
|
+
function createSourceEventStream(args) {
|
|
695
|
+
// If a valid execution context cannot be created due to incorrect arguments,
|
|
696
|
+
// a "Response" with only errors is returned.
|
|
697
|
+
const exeContext = buildExecutionContext(args);
|
|
698
|
+
// Return early errors if execution context failed.
|
|
699
|
+
if (!('schema' in exeContext)) {
|
|
700
|
+
return { errors: exeContext };
|
|
701
|
+
}
|
|
702
|
+
return createSourceEventStreamImpl(exeContext);
|
|
703
|
+
}
|
|
704
|
+
exports.createSourceEventStream = createSourceEventStream;
|
|
705
|
+
function createSourceEventStreamImpl(exeContext) {
|
|
706
|
+
try {
|
|
707
|
+
const eventStream = executeSubscription(exeContext);
|
|
708
|
+
if ((0, isPromise_js_1.isPromise)(eventStream)) {
|
|
709
|
+
return eventStream.then(undefined, error => ({ errors: [error] }));
|
|
710
|
+
}
|
|
711
|
+
return eventStream;
|
|
712
|
+
}
|
|
713
|
+
catch (error) {
|
|
714
|
+
return { errors: [error] };
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
function executeSubscription(exeContext) {
|
|
718
|
+
var _a;
|
|
719
|
+
const { schema, fragments, operation, variableValues, rootValue } = exeContext;
|
|
720
|
+
const rootType = schema.getSubscriptionType();
|
|
721
|
+
if (rootType == null) {
|
|
722
|
+
throw new graphql_1.GraphQLError('Schema is not configured to execute subscription operation.', { nodes: operation });
|
|
723
|
+
}
|
|
724
|
+
const rootFields = (0, collectFields_js_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
|
|
725
|
+
const [responseName, fieldNodes] = [...rootFields.entries()][0];
|
|
726
|
+
const fieldName = fieldNodes[0].name.value;
|
|
727
|
+
const fieldDef = getFieldDef(schema, rootType, fieldNodes[0]);
|
|
728
|
+
if (!fieldDef) {
|
|
729
|
+
throw new graphql_1.GraphQLError(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes });
|
|
730
|
+
}
|
|
731
|
+
const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name);
|
|
732
|
+
const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, rootType, path);
|
|
733
|
+
try {
|
|
734
|
+
// Implements the "ResolveFieldEventStream" algorithm from GraphQL specification.
|
|
735
|
+
// It differs from "ResolveFieldValue" due to providing a different `resolveFn`.
|
|
736
|
+
// Build a JS object of arguments from the field.arguments AST, using the
|
|
737
|
+
// variables scope to fulfill any variable references.
|
|
738
|
+
const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues);
|
|
739
|
+
// The resolve function's optional third argument is a context value that
|
|
740
|
+
// is provided to every resolve function within an execution. It is commonly
|
|
741
|
+
// used to represent an authenticated user, or request-specific caches.
|
|
742
|
+
const contextValue = exeContext.contextValue;
|
|
743
|
+
// Call the `subscribe()` resolver or the default resolver to produce an
|
|
744
|
+
// AsyncIterable yielding raw payloads.
|
|
745
|
+
const resolveFn = (_a = fieldDef.subscribe) !== null && _a !== void 0 ? _a : exeContext.subscribeFieldResolver;
|
|
746
|
+
const result = resolveFn(rootValue, args, contextValue, info);
|
|
747
|
+
if ((0, isPromise_js_1.isPromise)(result)) {
|
|
748
|
+
return result.then(assertEventStream).then(undefined, error => {
|
|
749
|
+
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path));
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
return assertEventStream(result);
|
|
753
|
+
}
|
|
754
|
+
catch (error) {
|
|
755
|
+
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path));
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
function assertEventStream(result) {
|
|
759
|
+
if (result instanceof Error) {
|
|
760
|
+
throw result;
|
|
761
|
+
}
|
|
762
|
+
// Assert field returned an event stream, otherwise yield an error.
|
|
763
|
+
if (!(0, isAsyncIterable_js_1.isAsyncIterable)(result)) {
|
|
764
|
+
throw new graphql_1.GraphQLError('Subscription field must return Async Iterable. ' + `Received: ${(0, inspect_js_1.inspect)(result)}.`);
|
|
765
|
+
}
|
|
766
|
+
return result;
|
|
767
|
+
}
|
|
768
|
+
/**
|
|
769
|
+
* This method looks up the field on the given type definition.
|
|
770
|
+
* It has special casing for the three introspection fields,
|
|
771
|
+
* __schema, __type and __typename. __typename is special because
|
|
772
|
+
* it can always be queried as a field, even in situations where no
|
|
773
|
+
* other fields are allowed, like on a Union. __schema and __type
|
|
774
|
+
* could get automatically added to the query type, but that would
|
|
775
|
+
* require mutating type definitions, which would cause issues.
|
|
776
|
+
*
|
|
777
|
+
* @internal
|
|
778
|
+
*/
|
|
779
|
+
function getFieldDef(schema, parentType, fieldNode) {
|
|
780
|
+
const fieldName = fieldNode.name.value;
|
|
781
|
+
if (fieldName === graphql_1.SchemaMetaFieldDef.name && schema.getQueryType() === parentType) {
|
|
782
|
+
return graphql_1.SchemaMetaFieldDef;
|
|
783
|
+
}
|
|
784
|
+
else if (fieldName === graphql_1.TypeMetaFieldDef.name && schema.getQueryType() === parentType) {
|
|
785
|
+
return graphql_1.TypeMetaFieldDef;
|
|
786
|
+
}
|
|
787
|
+
else if (fieldName === graphql_1.TypeNameMetaFieldDef.name) {
|
|
788
|
+
return graphql_1.TypeNameMetaFieldDef;
|
|
789
|
+
}
|
|
790
|
+
return parentType.getFields()[fieldName];
|
|
791
|
+
}
|
|
792
|
+
exports.getFieldDef = getFieldDef;
|