@graphql-tools/executor
Advanced tools
Comparing version 0.0.1-alpha-20221025155250-479d70be to 0.0.1-alpha-20221025161341-67605d5d
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getFieldDef = exports.createSourceEventStream = exports.subscribe = exports.defaultFieldResolver = exports.defaultTypeResolver = exports.buildResolveInfo = exports.buildExecutionContext = exports.assertValidExecutionArguments = exports.executeSync = exports.execute = void 0; | ||
const Path_js_1 = require("graphql/jsutils/Path.js"); | ||
const graphql_1 = require("graphql"); | ||
@@ -43,3 +42,3 @@ const values_js_1 = require("./values.js"); | ||
const result = executeOperation(exeContext); | ||
if (isPromise(result)) { | ||
if ((0, utils_1.isPromise)(result)) { | ||
return result.then(data => buildResponse(data, exeContext.errors), error => { | ||
@@ -65,3 +64,3 @@ exeContext.errors.push(error); | ||
// Assert that the execution was synchronous. | ||
if (isPromise(result)) { | ||
if ((0, utils_1.isPromise)(result)) { | ||
throw new Error('GraphQL execution failed to complete synchronously.'); | ||
@@ -90,3 +89,3 @@ } | ||
// Variables, if provided, must be an object. | ||
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.'); | ||
console.assert(rawVariableValues == null || (0, utils_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.'); | ||
} | ||
@@ -196,4 +195,4 @@ exports.assertValidExecutionArguments = assertValidExecutionArguments; | ||
function executeFieldsSerially(exeContext, parentType, sourceValue, path, fields) { | ||
return promiseReduce(fields.entries(), (results, [responseName, fieldNodes]) => { | ||
const fieldPath = (0, Path_js_1.addPath)(path, responseName, parentType.name); | ||
return (0, utils_1.promiseReduce)(fields.entries(), (results, [responseName, fieldNodes]) => { | ||
const fieldPath = (0, utils_1.addPath)(path, responseName, parentType.name); | ||
const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); | ||
@@ -203,3 +202,3 @@ if (result === undefined) { | ||
} | ||
if (isPromise(result)) { | ||
if ((0, utils_1.isPromise)(result)) { | ||
return result.then(resolvedResult => { | ||
@@ -222,7 +221,7 @@ results[responseName] = resolvedResult; | ||
for (const [responseName, fieldNodes] of fields.entries()) { | ||
const fieldPath = (0, Path_js_1.addPath)(path, responseName, parentType.name); | ||
const fieldPath = (0, utils_1.addPath)(path, responseName, parentType.name); | ||
const result = executeField(exeContext, parentType, sourceValue, fieldNodes, fieldPath); | ||
if (result !== undefined) { | ||
results[responseName] = result; | ||
if (isPromise(result)) { | ||
if ((0, utils_1.isPromise)(result)) { | ||
containsPromise = true; | ||
@@ -267,3 +266,3 @@ } | ||
// TODO: find a way to memoize, in case this field is within a List type. | ||
const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], exeContext.variableValues); | ||
const args = (0, utils_1.getArgumentValues)(fieldDef, fieldNodes[0], exeContext.variableValues); | ||
// The resolve function's optional third argument is a context value that | ||
@@ -275,3 +274,3 @@ // is provided to every resolve function within an execution. It is commonly | ||
let completed; | ||
if (isPromise(result)) { | ||
if ((0, utils_1.isPromise)(result)) { | ||
completed = result.then(resolved => completeValue(exeContext, returnType, fieldNodes, info, path, resolved)); | ||
@@ -282,7 +281,7 @@ } | ||
} | ||
if (isPromise(completed)) { | ||
if ((0, utils_1.isPromise)(completed)) { | ||
// Note: we don't rely on a `catch` method, but we do expect "thenable" | ||
// to take a second callback for the error case. | ||
return completed.then(undefined, rawError => { | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(path)); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(path)); | ||
return handleFieldError(error, returnType, exeContext); | ||
@@ -294,3 +293,3 @@ }); | ||
catch (rawError) { | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(path)); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(path)); | ||
return handleFieldError(error, returnType, exeContext); | ||
@@ -401,3 +400,3 @@ } | ||
while (true) { | ||
const fieldPath = (0, Path_js_1.addPath)(path, index, undefined); | ||
const fieldPath = (0, utils_1.addPath)(path, index, undefined); | ||
try { | ||
@@ -411,3 +410,3 @@ const { value, done } = await iterator.next(); | ||
const completedItem = completeValue(exeContext, itemType, fieldNodes, info, fieldPath, value); | ||
if (isPromise(completedItem)) { | ||
if ((0, utils_1.isPromise)(completedItem)) { | ||
containsPromise = true; | ||
@@ -419,3 +418,3 @@ } | ||
completedResults.push(null); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(fieldPath)); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(fieldPath)); | ||
handleFieldError(error, itemType, exeContext); | ||
@@ -426,3 +425,3 @@ } | ||
completedResults.push(null); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(fieldPath)); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(fieldPath)); | ||
handleFieldError(error, itemType, exeContext); | ||
@@ -435,20 +434,2 @@ break; | ||
} | ||
function isIterableObject(value) { | ||
return value != null && typeof value === 'object' && Symbol.iterator in value; | ||
} | ||
function isObjectLike(value) { | ||
return typeof value === 'object' && value !== null; | ||
} | ||
function isPromise(value) { | ||
return isObjectLike(value) && typeof value['then'] === 'function'; | ||
} | ||
function promiseReduce(values, callbackFn, initialValue) { | ||
let accumulator = initialValue; | ||
for (const value of values) { | ||
accumulator = isPromise(accumulator) | ||
? accumulator.then(resolved => callbackFn(resolved, value)) | ||
: callbackFn(accumulator, value); | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -464,3 +445,3 @@ * Complete a list value by completing each item in the list with the | ||
} | ||
if (!isIterableObject(result)) { | ||
if (!(0, utils_1.isIterableObject)(result)) { | ||
throw (0, utils_1.createGraphQLError)(`Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`); | ||
@@ -474,6 +455,6 @@ } | ||
// since from here on it is not ever accessed by resolver functions. | ||
const itemPath = (0, Path_js_1.addPath)(path, index, undefined); | ||
const itemPath = (0, utils_1.addPath)(path, index, undefined); | ||
try { | ||
let completedItem; | ||
if (isPromise(item)) { | ||
if ((0, utils_1.isPromise)(item)) { | ||
completedItem = item.then(resolved => completeValue(exeContext, itemType, fieldNodes, info, itemPath, resolved)); | ||
@@ -484,3 +465,3 @@ } | ||
} | ||
if (isPromise(completedItem)) { | ||
if ((0, utils_1.isPromise)(completedItem)) { | ||
containsPromise = true; | ||
@@ -490,3 +471,3 @@ // Note: we don't rely on a `catch` method, but we do expect "thenable" | ||
return completedItem.then(undefined, rawError => { | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(itemPath)); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath)); | ||
return handleFieldError(error, itemType, exeContext); | ||
@@ -498,3 +479,3 @@ }); | ||
catch (rawError) { | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, Path_js_1.pathToArray)(itemPath)); | ||
const error = (0, graphql_1.locatedError)(rawError, fieldNodes, (0, utils_1.pathToArray)(itemPath)); | ||
return handleFieldError(error, itemType, exeContext); | ||
@@ -526,3 +507,3 @@ } | ||
const runtimeType = resolveTypeFn(result, contextValue, info, returnType); | ||
if (isPromise(runtimeType)) { | ||
if ((0, utils_1.isPromise)(runtimeType)) { | ||
return runtimeType.then(resolvedRuntimeType => completeObjectValue(exeContext, ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result)); | ||
@@ -568,3 +549,3 @@ } | ||
const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info); | ||
if (isPromise(isTypeOf)) { | ||
if ((0, utils_1.isPromise)(isTypeOf)) { | ||
return isTypeOf.then(resolvedIsTypeOf => { | ||
@@ -600,3 +581,3 @@ if (!resolvedIsTypeOf) { | ||
// First, look for `__typename`. | ||
if (isObjectLike(value) && typeof value['__typename'] === 'string') { | ||
if ((0, utils_1.isObjectLike)(value) && typeof value['__typename'] === 'string') { | ||
return value['__typename']; | ||
@@ -611,3 +592,3 @@ } | ||
const isTypeOfResult = type.isTypeOf(value, contextValue, info); | ||
if (isPromise(isTypeOfResult)) { | ||
if ((0, utils_1.isPromise)(isTypeOfResult)) { | ||
promisedIsTypeOfResults[i] = isTypeOfResult; | ||
@@ -639,3 +620,3 @@ } | ||
// ensure source is a value for which property access is acceptable. | ||
if (isObjectLike(source) || typeof source === 'function') { | ||
if ((0, utils_1.isObjectLike)(source) || typeof source === 'function') { | ||
const property = source[info.fieldName]; | ||
@@ -679,3 +660,3 @@ if (typeof property === 'function') { | ||
const resultOrStream = createSourceEventStreamImpl(exeContext); | ||
if (isPromise(resultOrStream)) { | ||
if ((0, utils_1.isPromise)(resultOrStream)) { | ||
return resultOrStream.then(resolvedResultOrStream => mapSourceToResponse(exeContext, resolvedResultOrStream)); | ||
@@ -740,3 +721,3 @@ } | ||
const eventStream = executeSubscription(exeContext); | ||
if (isPromise(eventStream)) { | ||
if ((0, utils_1.isPromise)(eventStream)) { | ||
return eventStream.then(undefined, error => ({ errors: [error] })); | ||
@@ -764,3 +745,3 @@ } | ||
} | ||
const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name); | ||
const path = (0, utils_1.addPath)(undefined, responseName, rootType.name); | ||
const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, rootType, path); | ||
@@ -772,3 +753,3 @@ try { | ||
// variables scope to fulfill any variable references. | ||
const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues); | ||
const args = (0, utils_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues); | ||
// The resolve function's optional third argument is a context value that | ||
@@ -782,5 +763,5 @@ // is provided to every resolve function within an execution. It is commonly | ||
const result = resolveFn(rootValue, args, contextValue, info); | ||
if (isPromise(result)) { | ||
if ((0, utils_1.isPromise)(result)) { | ||
return result.then(assertEventStream).then(undefined, error => { | ||
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path)); | ||
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, utils_1.pathToArray)(path)); | ||
}); | ||
@@ -791,3 +772,3 @@ } | ||
catch (error) { | ||
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path)); | ||
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, utils_1.pathToArray)(path)); | ||
} | ||
@@ -794,0 +775,0 @@ } |
@@ -6,5 +6,3 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const Path_js_1 = require("graphql/jsutils/Path.js"); | ||
const execute_js_1 = require("./execute.js"); | ||
const values_js_1 = require("./values.js"); | ||
/** | ||
@@ -137,3 +135,3 @@ * Implements the "Subscribe" algorithm described in the GraphQL specification. | ||
} | ||
const path = (0, Path_js_1.addPath)(undefined, responseName, rootType.name); | ||
const path = (0, utils_1.addPath)(undefined, responseName, rootType.name); | ||
const info = (0, execute_js_1.buildResolveInfo)(exeContext, fieldDef, fieldNodes, rootType, path); | ||
@@ -145,3 +143,3 @@ try { | ||
// variables scope to fulfill any variable references. | ||
const args = (0, values_js_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues); | ||
const args = (0, utils_1.getArgumentValues)(fieldDef, fieldNodes[0], variableValues); | ||
// The resolve function's optional third argument is a context value that | ||
@@ -161,4 +159,4 @@ // is provided to every resolve function within an execution. It is commonly | ||
catch (error) { | ||
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, Path_js_1.pathToArray)(path)); | ||
throw (0, graphql_1.locatedError)(error, fieldNodes, (0, utils_1.pathToArray)(path)); | ||
} | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDirectiveValues = exports.getArgumentValues = exports.getVariableValues = void 0; | ||
const printPathArray_js_1 = require("graphql/jsutils/printPathArray.js"); | ||
exports.getVariableValues = void 0; | ||
const graphql_1 = require("graphql"); | ||
@@ -49,3 +48,3 @@ const utils_1 = require("@graphql-tools/utils"); | ||
} | ||
if (!hasOwnProperty(inputs, varName)) { | ||
if (!(0, utils_1.hasOwnProperty)(inputs, varName)) { | ||
if (varDefNode.defaultValue) { | ||
@@ -73,3 +72,3 @@ coercedValues[varName] = (0, graphql_1.valueFromAST)(varDefNode.defaultValue, varType); | ||
if (path.length > 0) { | ||
prefix += ` at "${varName}${(0, printPathArray_js_1.printPathArray)(path)}"`; | ||
prefix += ` at "${varName}${(0, utils_1.printPathArray)(path)}"`; | ||
} | ||
@@ -84,90 +83,1 @@ onError((0, utils_1.createGraphQLError)(prefix + '; ' + error.message, { | ||
} | ||
/** | ||
* Prepares an object map of argument values given a list of argument | ||
* definitions and list of argument AST nodes. | ||
* | ||
* Note: The returned value is a plain Object with a prototype, since it is | ||
* exposed to user code. Care should be taken to not pull values from the | ||
* Object prototype. | ||
*/ | ||
function getArgumentValues(def, node, variableValues) { | ||
var _a; | ||
const coercedValues = {}; | ||
// FIXME: https://github.com/graphql/graphql-js/issues/2203 | ||
/* c8 ignore next */ | ||
const argumentNodes = (_a = node.arguments) !== null && _a !== void 0 ? _a : []; | ||
const argNodeMap = Object.create(null); | ||
for (const arg of argumentNodes) { | ||
argNodeMap[arg.name.value] = arg.value; | ||
} | ||
for (const argDef of def.args) { | ||
const name = argDef.name; | ||
const argType = argDef.type; | ||
const argumentNode = argNodeMap[name]; | ||
if (!argumentNode) { | ||
if (argDef.defaultValue !== undefined) { | ||
coercedValues[name] = argDef.defaultValue; | ||
} | ||
else if ((0, graphql_1.isNonNullType)(argType)) { | ||
throw (0, utils_1.createGraphQLError)(`Argument "${name}" of required type "${(0, utils_1.inspect)(argType)}" ` + 'was not provided.', { | ||
nodes: node, | ||
}); | ||
} | ||
continue; | ||
} | ||
const valueNode = argumentNode.value; | ||
let isNull = valueNode.kind === graphql_1.Kind.NULL; | ||
if (valueNode.kind === graphql_1.Kind.VARIABLE) { | ||
const variableName = valueNode.name.value; | ||
if (variableValues == null || !hasOwnProperty(variableValues, variableName)) { | ||
if (argDef.defaultValue !== undefined) { | ||
coercedValues[name] = argDef.defaultValue; | ||
} | ||
else if ((0, graphql_1.isNonNullType)(argType)) { | ||
throw (0, utils_1.createGraphQLError)(`Argument "${name}" of required type "${(0, utils_1.inspect)(argType)}" ` + | ||
`was provided the variable "$${variableName}" which was not provided a runtime value.`, { nodes: valueNode }); | ||
} | ||
continue; | ||
} | ||
isNull = variableValues[variableName] == null; | ||
} | ||
if (isNull && (0, graphql_1.isNonNullType)(argType)) { | ||
throw (0, utils_1.createGraphQLError)(`Argument "${name}" of non-null type "${(0, utils_1.inspect)(argType)}" ` + 'must not be null.', { | ||
nodes: valueNode, | ||
}); | ||
} | ||
const coercedValue = (0, graphql_1.valueFromAST)(valueNode, argType, variableValues); | ||
if (coercedValue === undefined) { | ||
// Note: ValuesOfCorrectTypeRule validation should catch this before | ||
// execution. This is a runtime check to ensure execution does not | ||
// continue with an invalid argument value. | ||
throw (0, utils_1.createGraphQLError)(`Argument "${name}" has invalid value ${(0, graphql_1.print)(valueNode)}.`, { nodes: valueNode }); | ||
} | ||
coercedValues[name] = coercedValue; | ||
} | ||
return coercedValues; | ||
} | ||
exports.getArgumentValues = getArgumentValues; | ||
/** | ||
* Prepares an object map of argument values given a directive definition | ||
* and a AST node which may contain directives. Optionally also accepts a map | ||
* of variable values. | ||
* | ||
* If the directive does not exist on the node, returns undefined. | ||
* | ||
* Note: The returned value is a plain Object with a prototype, since it is | ||
* exposed to user code. Care should be taken to not pull values from the | ||
* Object prototype. | ||
*/ | ||
function getDirectiveValues(directiveDef, node, variableValues) { | ||
var _a; | ||
const directiveNode = (_a = node.directives) === null || _a === void 0 ? void 0 : _a.find(directive => directive.name.value === directiveDef.name); | ||
if (directiveNode) { | ||
return getArgumentValues(directiveDef, directiveNode, variableValues); | ||
} | ||
return undefined; | ||
} | ||
exports.getDirectiveValues = getDirectiveValues; | ||
function hasOwnProperty(obj, prop) { | ||
return Object.prototype.hasOwnProperty.call(obj, prop); | ||
} |
@@ -1,5 +0,4 @@ | ||
import { addPath, pathToArray } from 'graphql/jsutils/Path.js'; | ||
import { locatedError, OperationTypeNode, Kind, isAbstractType, isLeafType, isListType, isNonNullType, isObjectType, assertValidSchema, SchemaMetaFieldDef, TypeMetaFieldDef, TypeNameMetaFieldDef, } from 'graphql'; | ||
import { getArgumentValues, getVariableValues } from './values.js'; | ||
import { collectFields, collectSubFields, createGraphQLError, getRootTypeMap, inspect, isAsyncIterable, mapAsyncIterator, } from '@graphql-tools/utils'; | ||
import { getVariableValues } from './values.js'; | ||
import { addPath, collectFields, collectSubFields, createGraphQLError, getArgumentValues, getRootTypeMap, inspect, isAsyncIterable, isIterableObject, isObjectLike, isPromise, mapAsyncIterator, pathToArray, promiseReduce, } from '@graphql-tools/utils'; | ||
/** | ||
@@ -413,20 +412,2 @@ * Implements the "Executing requests" section of the GraphQL specification. | ||
} | ||
function isIterableObject(value) { | ||
return value != null && typeof value === 'object' && Symbol.iterator in value; | ||
} | ||
function isObjectLike(value) { | ||
return typeof value === 'object' && value !== null; | ||
} | ||
function isPromise(value) { | ||
return isObjectLike(value) && typeof value['then'] === 'function'; | ||
} | ||
function promiseReduce(values, callbackFn, initialValue) { | ||
let accumulator = initialValue; | ||
for (const value of values) { | ||
accumulator = isPromise(accumulator) | ||
? accumulator.then(resolved => callbackFn(resolved, value)) | ||
: callbackFn(accumulator, value); | ||
} | ||
return accumulator; | ||
} | ||
/** | ||
@@ -433,0 +414,0 @@ * Complete a list value by completing each item in the list with the |
@@ -1,6 +0,4 @@ | ||
import { collectFields, mapAsyncIterator, inspect, isAsyncIterable, createGraphQLError, } from '@graphql-tools/utils'; | ||
import { collectFields, mapAsyncIterator, inspect, isAsyncIterable, createGraphQLError, addPath, pathToArray, getArgumentValues, } from '@graphql-tools/utils'; | ||
import { GraphQLError, locatedError } from 'graphql'; | ||
import { addPath, pathToArray } from 'graphql/jsutils/Path.js'; | ||
import { assertValidExecutionArguments, buildExecutionContext, buildResolveInfo, execute, getFieldDef, } from './execute.js'; | ||
import { getArgumentValues } from './values.js'; | ||
/** | ||
@@ -7,0 +5,0 @@ * Implements the "Subscribe" algorithm described in the GraphQL specification. |
@@ -1,4 +0,3 @@ | ||
import { printPathArray } from 'graphql/jsutils/printPathArray.js'; | ||
import { Kind, print, isInputType, isNonNullType, coerceInputValue, typeFromAST, valueFromAST, } from 'graphql'; | ||
import { createGraphQLError, inspect } from '@graphql-tools/utils'; | ||
import { print, isInputType, isNonNullType, coerceInputValue, typeFromAST, valueFromAST, } from 'graphql'; | ||
import { createGraphQLError, hasOwnProperty, inspect, printPathArray } from '@graphql-tools/utils'; | ||
/** | ||
@@ -78,88 +77,1 @@ * Prepares an object map of variableValues of the correct type based on the | ||
} | ||
/** | ||
* Prepares an object map of argument values given a list of argument | ||
* definitions and list of argument AST nodes. | ||
* | ||
* Note: The returned value is a plain Object with a prototype, since it is | ||
* exposed to user code. Care should be taken to not pull values from the | ||
* Object prototype. | ||
*/ | ||
export function getArgumentValues(def, node, variableValues) { | ||
var _a; | ||
const coercedValues = {}; | ||
// FIXME: https://github.com/graphql/graphql-js/issues/2203 | ||
/* c8 ignore next */ | ||
const argumentNodes = (_a = node.arguments) !== null && _a !== void 0 ? _a : []; | ||
const argNodeMap = Object.create(null); | ||
for (const arg of argumentNodes) { | ||
argNodeMap[arg.name.value] = arg.value; | ||
} | ||
for (const argDef of def.args) { | ||
const name = argDef.name; | ||
const argType = argDef.type; | ||
const argumentNode = argNodeMap[name]; | ||
if (!argumentNode) { | ||
if (argDef.defaultValue !== undefined) { | ||
coercedValues[name] = argDef.defaultValue; | ||
} | ||
else if (isNonNullType(argType)) { | ||
throw createGraphQLError(`Argument "${name}" of required type "${inspect(argType)}" ` + 'was not provided.', { | ||
nodes: node, | ||
}); | ||
} | ||
continue; | ||
} | ||
const valueNode = argumentNode.value; | ||
let isNull = valueNode.kind === Kind.NULL; | ||
if (valueNode.kind === Kind.VARIABLE) { | ||
const variableName = valueNode.name.value; | ||
if (variableValues == null || !hasOwnProperty(variableValues, variableName)) { | ||
if (argDef.defaultValue !== undefined) { | ||
coercedValues[name] = argDef.defaultValue; | ||
} | ||
else if (isNonNullType(argType)) { | ||
throw createGraphQLError(`Argument "${name}" of required type "${inspect(argType)}" ` + | ||
`was provided the variable "$${variableName}" which was not provided a runtime value.`, { nodes: valueNode }); | ||
} | ||
continue; | ||
} | ||
isNull = variableValues[variableName] == null; | ||
} | ||
if (isNull && isNonNullType(argType)) { | ||
throw createGraphQLError(`Argument "${name}" of non-null type "${inspect(argType)}" ` + 'must not be null.', { | ||
nodes: valueNode, | ||
}); | ||
} | ||
const coercedValue = valueFromAST(valueNode, argType, variableValues); | ||
if (coercedValue === undefined) { | ||
// Note: ValuesOfCorrectTypeRule validation should catch this before | ||
// execution. This is a runtime check to ensure execution does not | ||
// continue with an invalid argument value. | ||
throw createGraphQLError(`Argument "${name}" has invalid value ${print(valueNode)}.`, { nodes: valueNode }); | ||
} | ||
coercedValues[name] = coercedValue; | ||
} | ||
return coercedValues; | ||
} | ||
/** | ||
* Prepares an object map of argument values given a directive definition | ||
* and a AST node which may contain directives. Optionally also accepts a map | ||
* of variable values. | ||
* | ||
* If the directive does not exist on the node, returns undefined. | ||
* | ||
* Note: The returned value is a plain Object with a prototype, since it is | ||
* exposed to user code. Care should be taken to not pull values from the | ||
* Object prototype. | ||
*/ | ||
export function getDirectiveValues(directiveDef, node, variableValues) { | ||
var _a; | ||
const directiveNode = (_a = node.directives) === null || _a === void 0 ? void 0 : _a.find(directive => directive.name.value === directiveDef.name); | ||
if (directiveNode) { | ||
return getArgumentValues(directiveDef, directiveNode, variableValues); | ||
} | ||
return undefined; | ||
} | ||
function hasOwnProperty(obj, prop) { | ||
return Object.prototype.hasOwnProperty.call(obj, prop); | ||
} |
{ | ||
"name": "@graphql-tools/executor", | ||
"version": "0.0.1-alpha-20221025155250-479d70be", | ||
"version": "0.0.1-alpha-20221025161341-67605d5d", | ||
"sideEffects": false, | ||
@@ -5,0 +5,0 @@ "peerDependencies": { |
@@ -1,4 +0,3 @@ | ||
import type { Path } from 'graphql/jsutils/Path.js'; | ||
import { GraphQLFormattedError, GraphQLError, DocumentNode, FieldNode, FragmentDefinitionNode, OperationDefinitionNode, GraphQLField, GraphQLFieldResolver, GraphQLObjectType, GraphQLResolveInfo, GraphQLTypeResolver, GraphQLSchema } from 'graphql'; | ||
import { Maybe } from '@graphql-tools/utils'; | ||
import { Maybe, Path, PromiseOrValue } from '@graphql-tools/utils'; | ||
/** | ||
@@ -114,3 +113,2 @@ * Terminology | ||
export declare function buildResolveInfo(exeContext: ExecutionContext, fieldDef: GraphQLField<unknown, unknown>, fieldNodes: ReadonlyArray<FieldNode>, parentType: GraphQLObjectType, path: Path): GraphQLResolveInfo; | ||
declare type PromiseOrValue<T> = Promise<T> | T; | ||
/** | ||
@@ -197,2 +195,1 @@ * If a resolveType function is not given, then a default resolve behavior is | ||
export declare function getFieldDef(schema: GraphQLSchema, parentType: GraphQLObjectType, fieldNode: FieldNode): Maybe<GraphQLField<unknown, unknown>>; | ||
export {}; |
@@ -1,3 +0,2 @@ | ||
import { GraphQLError, DirectiveNode, FieldNode, VariableDefinitionNode, GraphQLField, GraphQLDirective, GraphQLSchema } from 'graphql'; | ||
import { Maybe } from '@graphql-tools/utils'; | ||
import { GraphQLError, VariableDefinitionNode, GraphQLSchema } from 'graphql'; | ||
declare type CoercedVariableValues = { | ||
@@ -26,29 +25,2 @@ errors: ReadonlyArray<GraphQLError>; | ||
}): CoercedVariableValues; | ||
/** | ||
* Prepares an object map of argument values given a list of argument | ||
* definitions and list of argument AST nodes. | ||
* | ||
* Note: The returned value is a plain Object with a prototype, since it is | ||
* exposed to user code. Care should be taken to not pull values from the | ||
* Object prototype. | ||
*/ | ||
export declare function getArgumentValues(def: GraphQLField<unknown, unknown> | GraphQLDirective, node: FieldNode | DirectiveNode, variableValues?: Maybe<Record<string, unknown>>): { | ||
[argument: string]: unknown; | ||
}; | ||
/** | ||
* Prepares an object map of argument values given a directive definition | ||
* and a AST node which may contain directives. Optionally also accepts a map | ||
* of variable values. | ||
* | ||
* If the directive does not exist on the node, returns undefined. | ||
* | ||
* Note: The returned value is a plain Object with a prototype, since it is | ||
* exposed to user code. Care should be taken to not pull values from the | ||
* Object prototype. | ||
*/ | ||
export declare function getDirectiveValues(directiveDef: GraphQLDirective, node: { | ||
readonly directives?: ReadonlyArray<DirectiveNode>; | ||
}, variableValues?: Maybe<Record<string, unknown>>): undefined | { | ||
[argument: string]: unknown; | ||
}; | ||
export {}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
123204
2304