@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,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.responsePathAsArray = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
tslib_1.__exportStar(require("./collectFields.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./execute.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./mapAsyncIterator.js"), exports);
|
|
8
|
+
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; } });
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mapAsyncIterator = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Given an AsyncIterable and a callback function, return an AsyncIterator
|
|
6
|
+
* which produces values mapped via calling the callback function.
|
|
7
|
+
*/
|
|
8
|
+
function mapAsyncIterator(iterable, callback) {
|
|
9
|
+
const iterator = iterable[Symbol.asyncIterator]();
|
|
10
|
+
async function mapResult(result) {
|
|
11
|
+
if (result.done) {
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
return { value: await callback(result.value), done: false };
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
/* c8 ignore start */
|
|
19
|
+
// FIXME: add test case
|
|
20
|
+
if (typeof iterator.return === 'function') {
|
|
21
|
+
try {
|
|
22
|
+
await iterator.return();
|
|
23
|
+
}
|
|
24
|
+
catch (_e) {
|
|
25
|
+
/* ignore error */
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw error;
|
|
29
|
+
/* c8 ignore stop */
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
async next() {
|
|
34
|
+
return mapResult(await iterator.next());
|
|
35
|
+
},
|
|
36
|
+
async return() {
|
|
37
|
+
// If iterator.return() does not exist, then type R must be undefined.
|
|
38
|
+
return typeof iterator.return === 'function'
|
|
39
|
+
? mapResult(await iterator.return())
|
|
40
|
+
: { value: undefined, done: true };
|
|
41
|
+
},
|
|
42
|
+
async throw(error) {
|
|
43
|
+
if (typeof iterator.throw === 'function') {
|
|
44
|
+
return mapResult(await iterator.throw(error));
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
},
|
|
48
|
+
[Symbol.asyncIterator]() {
|
|
49
|
+
return this;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
exports.mapAsyncIterator = mapAsyncIterator;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSourceEventStream = exports.subscribe = void 0;
|
|
4
|
+
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
|
+
const Path_js_1 = require("graphql/jsutils/Path.js");
|
|
9
|
+
const collectFields_js_1 = require("./collectFields.js");
|
|
10
|
+
const execute_js_1 = require("./execute.js");
|
|
11
|
+
const mapAsyncIterator_js_1 = require("./mapAsyncIterator.js");
|
|
12
|
+
const values_js_1 = require("./values.js");
|
|
13
|
+
/**
|
|
14
|
+
* Implements the "Subscribe" algorithm described in the GraphQL specification.
|
|
15
|
+
*
|
|
16
|
+
* Returns a Promise which resolves to either an AsyncIterator (if successful)
|
|
17
|
+
* or an ExecutionResult (error). The promise will be rejected if the schema or
|
|
18
|
+
* other arguments to this function are invalid, or if the resolved event stream
|
|
19
|
+
* is not an async iterable.
|
|
20
|
+
*
|
|
21
|
+
* If the client-provided arguments to this function do not result in a
|
|
22
|
+
* compliant subscription, a GraphQL Response (ExecutionResult) with
|
|
23
|
+
* descriptive errors and no data will be returned.
|
|
24
|
+
*
|
|
25
|
+
* If the source stream could not be created due to faulty subscription
|
|
26
|
+
* resolver logic or underlying systems, the promise will resolve to a single
|
|
27
|
+
* ExecutionResult containing `errors` and no `data`.
|
|
28
|
+
*
|
|
29
|
+
* If the operation succeeded, the promise resolves to an AsyncIterator, which
|
|
30
|
+
* yields a stream of ExecutionResults representing the response stream.
|
|
31
|
+
*
|
|
32
|
+
* Accepts either an object with named arguments, or individual arguments.
|
|
33
|
+
*/
|
|
34
|
+
async function subscribe(args) {
|
|
35
|
+
// Temporary for v15 to v16 migration. Remove in v17
|
|
36
|
+
(0, devAssert_js_1.devAssert)(arguments.length < 2, 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.');
|
|
37
|
+
const { schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, subscribeFieldResolver, } = args;
|
|
38
|
+
const resultOrStream = await createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver);
|
|
39
|
+
if (!(0, isAsyncIterable_js_1.isAsyncIterable)(resultOrStream)) {
|
|
40
|
+
return resultOrStream;
|
|
41
|
+
}
|
|
42
|
+
// For each payload yielded from a subscription, map it over the normal
|
|
43
|
+
// GraphQL `execute` function, with `payload` as the rootValue.
|
|
44
|
+
// This implements the "MapSourceToResponseEvent" algorithm described in
|
|
45
|
+
// the GraphQL specification. The `execute` function provides the
|
|
46
|
+
// "ExecuteSubscriptionEvent" algorithm, as it is nearly identical to the
|
|
47
|
+
// "ExecuteQuery" algorithm, for which `execute` is also used.
|
|
48
|
+
const mapSourceToResponse = (payload) => (0, execute_js_1.execute)({
|
|
49
|
+
schema,
|
|
50
|
+
document,
|
|
51
|
+
rootValue: payload,
|
|
52
|
+
contextValue,
|
|
53
|
+
variableValues,
|
|
54
|
+
operationName,
|
|
55
|
+
fieldResolver,
|
|
56
|
+
});
|
|
57
|
+
// Map every source value to a ExecutionResult value as described above.
|
|
58
|
+
return (0, mapAsyncIterator_js_1.mapAsyncIterator)(resultOrStream, mapSourceToResponse);
|
|
59
|
+
}
|
|
60
|
+
exports.subscribe = subscribe;
|
|
61
|
+
/**
|
|
62
|
+
* Implements the "CreateSourceEventStream" algorithm described in the
|
|
63
|
+
* GraphQL specification, resolving the subscription source event stream.
|
|
64
|
+
*
|
|
65
|
+
* Returns a Promise which resolves to either an AsyncIterable (if successful)
|
|
66
|
+
* or an ExecutionResult (error). The promise will be rejected if the schema or
|
|
67
|
+
* other arguments to this function are invalid, or if the resolved event stream
|
|
68
|
+
* is not an async iterable.
|
|
69
|
+
*
|
|
70
|
+
* If the client-provided arguments to this function do not result in a
|
|
71
|
+
* compliant subscription, a GraphQL Response (ExecutionResult) with
|
|
72
|
+
* descriptive errors and no data will be returned.
|
|
73
|
+
*
|
|
74
|
+
* If the the source stream could not be created due to faulty subscription
|
|
75
|
+
* resolver logic or underlying systems, the promise will resolve to a single
|
|
76
|
+
* ExecutionResult containing `errors` and no `data`.
|
|
77
|
+
*
|
|
78
|
+
* If the operation succeeded, the promise resolves to the AsyncIterable for the
|
|
79
|
+
* event stream returned by the resolver.
|
|
80
|
+
*
|
|
81
|
+
* A Source Event Stream represents a sequence of events, each of which triggers
|
|
82
|
+
* a GraphQL execution for that event.
|
|
83
|
+
*
|
|
84
|
+
* This may be useful when hosting the stateful subscription service in a
|
|
85
|
+
* different process or machine than the stateless GraphQL execution engine,
|
|
86
|
+
* or otherwise separating these two steps. For more on this, see the
|
|
87
|
+
* "Supporting Subscriptions at Scale" information in the GraphQL specification.
|
|
88
|
+
*/
|
|
89
|
+
async function createSourceEventStream(schema, document, rootValue, contextValue, variableValues, operationName, subscribeFieldResolver) {
|
|
90
|
+
// If arguments are missing or incorrectly typed, this is an internal
|
|
91
|
+
// developer mistake which should throw an early error.
|
|
92
|
+
(0, execute_js_1.assertValidExecutionArguments)(schema, document, variableValues);
|
|
93
|
+
// If a valid execution context cannot be created due to incorrect arguments,
|
|
94
|
+
// a "Response" with only errors is returned.
|
|
95
|
+
const exeContext = (0, execute_js_1.buildExecutionContext)({
|
|
96
|
+
schema,
|
|
97
|
+
document,
|
|
98
|
+
rootValue,
|
|
99
|
+
contextValue,
|
|
100
|
+
variableValues,
|
|
101
|
+
operationName,
|
|
102
|
+
subscribeFieldResolver,
|
|
103
|
+
});
|
|
104
|
+
// Return early errors if execution context failed.
|
|
105
|
+
if (!('schema' in exeContext)) {
|
|
106
|
+
return { errors: exeContext };
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const eventStream = await executeSubscription(exeContext);
|
|
110
|
+
// Assert field returned an event stream, otherwise yield an error.
|
|
111
|
+
if (!(0, isAsyncIterable_js_1.isAsyncIterable)(eventStream)) {
|
|
112
|
+
throw new Error('Subscription field must return Async Iterable. ' + `Received: ${(0, inspect_js_1.inspect)(eventStream)}.`);
|
|
113
|
+
}
|
|
114
|
+
return eventStream;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
// If it GraphQLError, report it as an ExecutionResult, containing only errors and no data.
|
|
118
|
+
// Otherwise treat the error as a system-class error and re-throw it.
|
|
119
|
+
if (error instanceof graphql_1.GraphQLError) {
|
|
120
|
+
return { errors: [error] };
|
|
121
|
+
}
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.createSourceEventStream = createSourceEventStream;
|
|
126
|
+
async function executeSubscription(exeContext) {
|
|
127
|
+
var _a;
|
|
128
|
+
const { schema, fragments, operation, variableValues, rootValue } = exeContext;
|
|
129
|
+
const rootType = schema.getSubscriptionType();
|
|
130
|
+
if (rootType == null) {
|
|
131
|
+
throw new graphql_1.GraphQLError('Schema is not configured to execute subscription operation.', { nodes: operation });
|
|
132
|
+
}
|
|
133
|
+
const rootFields = (0, collectFields_js_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
|
|
134
|
+
const [responseName, fieldNodes] = [...rootFields.entries()][0];
|
|
135
|
+
const fieldDef = (0, execute_js_1.getFieldDef)(schema, rootType, fieldNodes[0]);
|
|
136
|
+
if (!fieldDef) {
|
|
137
|
+
const fieldName = fieldNodes[0].name.value;
|
|
138
|
+
throw new graphql_1.GraphQLError(`The subscription field "${fieldName}" is not defined.`, { nodes: fieldNodes });
|
|
139
|
+
}
|
|
140
|
+
const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name);
|
|
141
|
+
const info = (0, execute_js_1.buildResolveInfo)(exeContext, fieldDef, fieldNodes, rootType, path);
|
|
142
|
+
try {
|
|
143
|
+
// Implements the "ResolveFieldEventStream" algorithm from GraphQL specification.
|
|
144
|
+
// It differs from "ResolveFieldValue" due to providing a different `resolveFn`.
|
|
145
|
+
// Build a JS object of arguments from the field.arguments AST, using the
|
|
146
|
+
// variables scope to fulfill any variable references.
|
|
147
|
+
const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues);
|
|
148
|
+
// The resolve function's optional third argument is a context value that
|
|
149
|
+
// is provided to every resolve function within an execution. It is commonly
|
|
150
|
+
// used to represent an authenticated user, or request-specific caches.
|
|
151
|
+
const contextValue = exeContext.contextValue;
|
|
152
|
+
// Call the `subscribe()` resolver or the default resolver to produce an
|
|
153
|
+
// AsyncIterable yielding raw payloads.
|
|
154
|
+
const resolveFn = (_a = fieldDef.subscribe) !== null && _a !== void 0 ? _a : exeContext.subscribeFieldResolver;
|
|
155
|
+
const eventStream = await resolveFn(rootValue, args, contextValue, info);
|
|
156
|
+
if (eventStream instanceof Error) {
|
|
157
|
+
throw eventStream;
|
|
158
|
+
}
|
|
159
|
+
return eventStream;
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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
|
+
const printPathArray_js_1 = require("graphql/jsutils/printPathArray.js");
|
|
7
|
+
const graphql_1 = require("graphql");
|
|
8
|
+
/**
|
|
9
|
+
* Prepares an object map of variableValues of the correct type based on the
|
|
10
|
+
* provided variable definitions and arbitrary input. If the input cannot be
|
|
11
|
+
* parsed to match the variable definitions, a GraphQLError will be thrown.
|
|
12
|
+
*
|
|
13
|
+
* Note: The returned value is a plain Object with a prototype, since it is
|
|
14
|
+
* exposed to user code. Care should be taken to not pull values from the
|
|
15
|
+
* Object prototype.
|
|
16
|
+
*/
|
|
17
|
+
function getVariableValues(schema, varDefNodes, inputs, options) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
const maxErrors = options === null || options === void 0 ? void 0 : options.maxErrors;
|
|
20
|
+
try {
|
|
21
|
+
const coerced = coerceVariableValues(schema, varDefNodes, inputs, error => {
|
|
22
|
+
if (maxErrors != null && errors.length >= maxErrors) {
|
|
23
|
+
throw new graphql_1.GraphQLError('Too many errors processing variables, error limit reached. Execution aborted.');
|
|
24
|
+
}
|
|
25
|
+
errors.push(error);
|
|
26
|
+
});
|
|
27
|
+
if (errors.length === 0) {
|
|
28
|
+
return { coerced };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
errors.push(error);
|
|
33
|
+
}
|
|
34
|
+
// @ts-expect-error - We know that errors is an array of GraphQLError.
|
|
35
|
+
return { errors };
|
|
36
|
+
}
|
|
37
|
+
exports.getVariableValues = getVariableValues;
|
|
38
|
+
function coerceVariableValues(schema, varDefNodes, inputs, onError) {
|
|
39
|
+
const coercedValues = {};
|
|
40
|
+
for (const varDefNode of varDefNodes) {
|
|
41
|
+
const varName = varDefNode.variable.name.value;
|
|
42
|
+
const varType = (0, graphql_1.typeFromAST)(schema, varDefNode.type);
|
|
43
|
+
if (!(0, graphql_1.isInputType)(varType)) {
|
|
44
|
+
// Must use input types for variables. This should be caught during
|
|
45
|
+
// validation, however is checked again here for safety.
|
|
46
|
+
const varTypeStr = (0, graphql_1.print)(varDefNode.type);
|
|
47
|
+
onError(new graphql_1.GraphQLError(`Variable "$${varName}" expected value of type "${varTypeStr}" which cannot be used as an input type.`, { nodes: varDefNode.type }));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (!hasOwnProperty(inputs, varName)) {
|
|
51
|
+
if (varDefNode.defaultValue) {
|
|
52
|
+
coercedValues[varName] = (0, graphql_1.valueFromAST)(varDefNode.defaultValue, varType);
|
|
53
|
+
}
|
|
54
|
+
else if ((0, graphql_1.isNonNullType)(varType)) {
|
|
55
|
+
const varTypeStr = (0, inspect_js_1.inspect)(varType);
|
|
56
|
+
onError(new graphql_1.GraphQLError(`Variable "$${varName}" of required type "${varTypeStr}" was not provided.`, {
|
|
57
|
+
nodes: varDefNode,
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const value = inputs[varName];
|
|
63
|
+
if (value === null && (0, graphql_1.isNonNullType)(varType)) {
|
|
64
|
+
const varTypeStr = (0, inspect_js_1.inspect)(varType);
|
|
65
|
+
onError(new graphql_1.GraphQLError(`Variable "$${varName}" of non-null type "${varTypeStr}" must not be null.`, {
|
|
66
|
+
nodes: varDefNode,
|
|
67
|
+
}));
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
coercedValues[varName] = (0, graphql_1.coerceInputValue)(value, varType, (path, invalidValue, error) => {
|
|
71
|
+
let prefix = `Variable "$${varName}" got invalid value ` + (0, inspect_js_1.inspect)(invalidValue);
|
|
72
|
+
if (path.length > 0) {
|
|
73
|
+
prefix += ` at "${varName}${(0, printPathArray_js_1.printPathArray)(path)}"`;
|
|
74
|
+
}
|
|
75
|
+
onError(new graphql_1.GraphQLError(prefix + '; ' + error.message, {
|
|
76
|
+
nodes: varDefNode,
|
|
77
|
+
originalError: error.originalError,
|
|
78
|
+
}));
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return coercedValues;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Prepares an object map of argument values given a list of argument
|
|
85
|
+
* definitions and list of argument AST nodes.
|
|
86
|
+
*
|
|
87
|
+
* Note: The returned value is a plain Object with a prototype, since it is
|
|
88
|
+
* exposed to user code. Care should be taken to not pull values from the
|
|
89
|
+
* Object prototype.
|
|
90
|
+
*/
|
|
91
|
+
function getArgumentValues(def, node, variableValues) {
|
|
92
|
+
var _a;
|
|
93
|
+
const coercedValues = {};
|
|
94
|
+
// FIXME: https://github.com/graphql/graphql-js/issues/2203
|
|
95
|
+
/* c8 ignore next */
|
|
96
|
+
const argumentNodes = (_a = node.arguments) !== null && _a !== void 0 ? _a : [];
|
|
97
|
+
const argNodeMap = (0, keyMap_js_1.keyMap)(argumentNodes, arg => arg.name.value);
|
|
98
|
+
for (const argDef of def.args) {
|
|
99
|
+
const name = argDef.name;
|
|
100
|
+
const argType = argDef.type;
|
|
101
|
+
const argumentNode = argNodeMap[name];
|
|
102
|
+
if (!argumentNode) {
|
|
103
|
+
if (argDef.defaultValue !== undefined) {
|
|
104
|
+
coercedValues[name] = argDef.defaultValue;
|
|
105
|
+
}
|
|
106
|
+
else if ((0, graphql_1.isNonNullType)(argType)) {
|
|
107
|
+
throw new graphql_1.GraphQLError(`Argument "${name}" of required type "${(0, inspect_js_1.inspect)(argType)}" ` + 'was not provided.', {
|
|
108
|
+
nodes: node,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const valueNode = argumentNode.value;
|
|
114
|
+
let isNull = valueNode.kind === graphql_1.Kind.NULL;
|
|
115
|
+
if (valueNode.kind === graphql_1.Kind.VARIABLE) {
|
|
116
|
+
const variableName = valueNode.name.value;
|
|
117
|
+
if (variableValues == null || !hasOwnProperty(variableValues, variableName)) {
|
|
118
|
+
if (argDef.defaultValue !== undefined) {
|
|
119
|
+
coercedValues[name] = argDef.defaultValue;
|
|
120
|
+
}
|
|
121
|
+
else if ((0, graphql_1.isNonNullType)(argType)) {
|
|
122
|
+
throw new graphql_1.GraphQLError(`Argument "${name}" of required type "${(0, inspect_js_1.inspect)(argType)}" ` +
|
|
123
|
+
`was provided the variable "$${variableName}" which was not provided a runtime value.`, { nodes: valueNode });
|
|
124
|
+
}
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
isNull = variableValues[variableName] == null;
|
|
128
|
+
}
|
|
129
|
+
if (isNull && (0, graphql_1.isNonNullType)(argType)) {
|
|
130
|
+
throw new graphql_1.GraphQLError(`Argument "${name}" of non-null type "${(0, inspect_js_1.inspect)(argType)}" ` + 'must not be null.', {
|
|
131
|
+
nodes: valueNode,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const coercedValue = (0, graphql_1.valueFromAST)(valueNode, argType, variableValues);
|
|
135
|
+
if (coercedValue === undefined) {
|
|
136
|
+
// Note: ValuesOfCorrectTypeRule validation should catch this before
|
|
137
|
+
// execution. This is a runtime check to ensure execution does not
|
|
138
|
+
// continue with an invalid argument value.
|
|
139
|
+
throw new graphql_1.GraphQLError(`Argument "${name}" has invalid value ${(0, graphql_1.print)(valueNode)}.`, { nodes: valueNode });
|
|
140
|
+
}
|
|
141
|
+
coercedValues[name] = coercedValue;
|
|
142
|
+
}
|
|
143
|
+
return coercedValues;
|
|
144
|
+
}
|
|
145
|
+
exports.getArgumentValues = getArgumentValues;
|
|
146
|
+
/**
|
|
147
|
+
* Prepares an object map of argument values given a directive definition
|
|
148
|
+
* and a AST node which may contain directives. Optionally also accepts a map
|
|
149
|
+
* of variable values.
|
|
150
|
+
*
|
|
151
|
+
* If the directive does not exist on the node, returns undefined.
|
|
152
|
+
*
|
|
153
|
+
* Note: The returned value is a plain Object with a prototype, since it is
|
|
154
|
+
* exposed to user code. Care should be taken to not pull values from the
|
|
155
|
+
* Object prototype.
|
|
156
|
+
*/
|
|
157
|
+
function getDirectiveValues(directiveDef, node, variableValues) {
|
|
158
|
+
var _a;
|
|
159
|
+
const directiveNode = (_a = node.directives) === null || _a === void 0 ? void 0 : _a.find(directive => directive.name.value === directiveDef.name);
|
|
160
|
+
if (directiveNode) {
|
|
161
|
+
return getArgumentValues(directiveDef, directiveNode, variableValues);
|
|
162
|
+
}
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
exports.getDirectiveValues = getDirectiveValues;
|
|
166
|
+
function hasOwnProperty(obj, prop) {
|
|
167
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
168
|
+
}
|
package/cjs/index.js
ADDED
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -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,109 @@
|
|
|
1
|
+
import { Kind, isAbstractType, GraphQLIncludeDirective, GraphQLSkipDirective, typeFromAST, } from 'graphql';
|
|
2
|
+
import { AccumulatorMap } from './AccumulatorMap.js';
|
|
3
|
+
import { getDirectiveValues } from './values.js';
|
|
4
|
+
/**
|
|
5
|
+
* Given a selectionSet, collects all of the fields and returns them.
|
|
6
|
+
*
|
|
7
|
+
* CollectFields requires the "runtime type" of an object. For a field that
|
|
8
|
+
* returns an Interface or Union type, the "runtime type" will be the actual
|
|
9
|
+
* object type returned by that field.
|
|
10
|
+
*
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export function collectFields(schema, fragments, variableValues, runtimeType, selectionSet) {
|
|
14
|
+
const fields = new AccumulatorMap();
|
|
15
|
+
collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, new Set());
|
|
16
|
+
return fields;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Given an array of field nodes, collects all of the subfields of the passed
|
|
20
|
+
* in fields, and returns them at the end.
|
|
21
|
+
*
|
|
22
|
+
* CollectSubFields requires the "return type" of an object. For a field that
|
|
23
|
+
* returns an Interface or Union type, the "return type" will be the actual
|
|
24
|
+
* object type returned by that field.
|
|
25
|
+
*
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export function collectSubfields(schema, fragments, variableValues, returnType, fieldNodes) {
|
|
29
|
+
const subFieldNodes = new AccumulatorMap();
|
|
30
|
+
const visitedFragmentNames = new Set();
|
|
31
|
+
for (const node of fieldNodes) {
|
|
32
|
+
if (node.selectionSet) {
|
|
33
|
+
collectFieldsImpl(schema, fragments, variableValues, returnType, node.selectionSet, subFieldNodes, visitedFragmentNames);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return subFieldNodes;
|
|
37
|
+
}
|
|
38
|
+
function collectFieldsImpl(schema, fragments, variableValues, runtimeType, selectionSet, fields, visitedFragmentNames) {
|
|
39
|
+
for (const selection of selectionSet.selections) {
|
|
40
|
+
switch (selection.kind) {
|
|
41
|
+
case Kind.FIELD: {
|
|
42
|
+
if (!shouldIncludeNode(variableValues, selection)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
fields.add(getFieldEntryKey(selection), selection);
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case Kind.INLINE_FRAGMENT: {
|
|
49
|
+
if (!shouldIncludeNode(variableValues, selection) ||
|
|
50
|
+
!doesFragmentConditionMatch(schema, selection, runtimeType)) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
collectFieldsImpl(schema, fragments, variableValues, runtimeType, selection.selectionSet, fields, visitedFragmentNames);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case Kind.FRAGMENT_SPREAD: {
|
|
57
|
+
const fragName = selection.name.value;
|
|
58
|
+
if (visitedFragmentNames.has(fragName) || !shouldIncludeNode(variableValues, selection)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
visitedFragmentNames.add(fragName);
|
|
62
|
+
const fragment = fragments[fragName];
|
|
63
|
+
if (!fragment || !doesFragmentConditionMatch(schema, fragment, runtimeType)) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
collectFieldsImpl(schema, fragments, variableValues, runtimeType, fragment.selectionSet, fields, visitedFragmentNames);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Determines if a field should be included based on the `@include` and `@skip`
|
|
74
|
+
* directives, where `@skip` has higher precedence than `@include`.
|
|
75
|
+
*/
|
|
76
|
+
function shouldIncludeNode(variableValues, node) {
|
|
77
|
+
const skip = getDirectiveValues(GraphQLSkipDirective, node, variableValues);
|
|
78
|
+
if ((skip === null || skip === void 0 ? void 0 : skip['if']) === true) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const include = getDirectiveValues(GraphQLIncludeDirective, node, variableValues);
|
|
82
|
+
if ((include === null || include === void 0 ? void 0 : include['if']) === false) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Determines if a fragment is applicable to the given type.
|
|
89
|
+
*/
|
|
90
|
+
function doesFragmentConditionMatch(schema, fragment, type) {
|
|
91
|
+
const typeConditionNode = fragment.typeCondition;
|
|
92
|
+
if (!typeConditionNode) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
const conditionalType = typeFromAST(schema, typeConditionNode);
|
|
96
|
+
if (conditionalType === type) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if (isAbstractType(conditionalType)) {
|
|
100
|
+
return schema.isSubType(conditionalType, type);
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Implements the logic to compute the key of a given field's entry
|
|
106
|
+
*/
|
|
107
|
+
function getFieldEntryKey(node) {
|
|
108
|
+
return node.alias ? node.alias.value : node.name.value;
|
|
109
|
+
}
|