123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.execute = execute;
- exports.assertValidExecutionArguments = assertValidExecutionArguments;
- exports.buildExecutionContext = buildExecutionContext;
- exports.collectFields = collectFields;
- exports.buildResolveInfo = buildResolveInfo;
- exports.resolveFieldValueOrError = resolveFieldValueOrError;
- exports.getFieldDef = getFieldDef;
- exports.defaultFieldResolver = exports.defaultTypeResolver = void 0;
- var _iterall = require("iterall");
- var _inspect = _interopRequireDefault(require("../jsutils/inspect"));
- var _memoize = _interopRequireDefault(require("../jsutils/memoize3"));
- var _invariant = _interopRequireDefault(require("../jsutils/invariant"));
- var _devAssert = _interopRequireDefault(require("../jsutils/devAssert"));
- var _isInvalid = _interopRequireDefault(require("../jsutils/isInvalid"));
- var _isNullish = _interopRequireDefault(require("../jsutils/isNullish"));
- var _isPromise = _interopRequireDefault(require("../jsutils/isPromise"));
- var _isObjectLike = _interopRequireDefault(require("../jsutils/isObjectLike"));
- var _promiseReduce = _interopRequireDefault(require("../jsutils/promiseReduce"));
- var _promiseForObject = _interopRequireDefault(require("../jsutils/promiseForObject"));
- var _Path = require("../jsutils/Path");
- var _GraphQLError = require("../error/GraphQLError");
- var _locatedError = require("../error/locatedError");
- var _kinds = require("../language/kinds");
- var _validate = require("../type/validate");
- var _introspection = require("../type/introspection");
- var _directives = require("../type/directives");
- var _definition = require("../type/definition");
- var _typeFromAST = require("../utilities/typeFromAST");
- var _getOperationRootType = require("../utilities/getOperationRootType");
- var _values = require("./values");
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
- function execute(argsOrSchema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver) {
-
-
- return arguments.length === 1 ? executeImpl(argsOrSchema) : executeImpl({
- schema: argsOrSchema,
- document: document,
- rootValue: rootValue,
- contextValue: contextValue,
- variableValues: variableValues,
- operationName: operationName,
- fieldResolver: fieldResolver,
- typeResolver: typeResolver
- });
- }
- function executeImpl(args) {
- var schema = args.schema,
- document = args.document,
- rootValue = args.rootValue,
- contextValue = args.contextValue,
- variableValues = args.variableValues,
- operationName = args.operationName,
- fieldResolver = args.fieldResolver,
- typeResolver = args.typeResolver;
- assertValidExecutionArguments(schema, document, variableValues);
-
- var exeContext = buildExecutionContext(schema, document, rootValue, contextValue, variableValues, operationName, fieldResolver, typeResolver);
- if (Array.isArray(exeContext)) {
- return {
- errors: exeContext
- };
- }
-
-
-
-
-
-
- var data = executeOperation(exeContext, exeContext.operation, rootValue);
- return buildResponse(exeContext, data);
- }
- function buildResponse(exeContext, data) {
- if ((0, _isPromise.default)(data)) {
- return data.then(function (resolved) {
- return buildResponse(exeContext, resolved);
- });
- }
- return exeContext.errors.length === 0 ? {
- data: data
- } : {
- errors: exeContext.errors,
- data: data
- };
- }
- function assertValidExecutionArguments(schema, document, rawVariableValues) {
- document || (0, _devAssert.default)(0, 'Must provide document');
- (0, _validate.assertValidSchema)(schema);
- rawVariableValues == null || (0, _isObjectLike.default)(rawVariableValues) || (0, _devAssert.default)(0, '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.');
- }
- function buildExecutionContext(schema, document, rootValue, contextValue, rawVariableValues, operationName, fieldResolver, typeResolver) {
- var operation;
- var hasMultipleAssumedOperations = false;
- var fragments = Object.create(null);
- for (var _i2 = 0, _document$definitions2 = document.definitions; _i2 < _document$definitions2.length; _i2++) {
- var definition = _document$definitions2[_i2];
- switch (definition.kind) {
- case _kinds.Kind.OPERATION_DEFINITION:
- if (!operationName && operation) {
- hasMultipleAssumedOperations = true;
- } else if (!operationName || definition.name && definition.name.value === operationName) {
- operation = definition;
- }
- break;
- case _kinds.Kind.FRAGMENT_DEFINITION:
- fragments[definition.name.value] = definition;
- break;
- }
- }
- if (!operation) {
- if (operationName) {
- return [new _GraphQLError.GraphQLError("Unknown operation named \"".concat(operationName, "\"."))];
- }
- return [new _GraphQLError.GraphQLError('Must provide an operation.')];
- }
- if (hasMultipleAssumedOperations) {
- return [new _GraphQLError.GraphQLError('Must provide operation name if query contains multiple operations.')];
- }
- var coercedVariableValues = (0, _values.getVariableValues)(schema, operation.variableDefinitions || [], rawVariableValues || {}, {
- maxErrors: 50
- });
- if (coercedVariableValues.errors) {
- return coercedVariableValues.errors;
- }
- return {
- schema: schema,
- fragments: fragments,
- rootValue: rootValue,
- contextValue: contextValue,
- operation: operation,
- variableValues: coercedVariableValues.coerced,
- fieldResolver: fieldResolver || defaultFieldResolver,
- typeResolver: typeResolver || defaultTypeResolver,
- errors: []
- };
- }
- function executeOperation(exeContext, operation, rootValue) {
- var type = (0, _getOperationRootType.getOperationRootType)(exeContext.schema, operation);
- var fields = collectFields(exeContext, type, operation.selectionSet, Object.create(null), Object.create(null));
- var path = undefined;
-
-
-
-
- try {
- var result = operation.operation === 'mutation' ? executeFieldsSerially(exeContext, type, rootValue, path, fields) : executeFields(exeContext, type, rootValue, path, fields);
- if ((0, _isPromise.default)(result)) {
- return result.then(undefined, function (error) {
- exeContext.errors.push(error);
- return Promise.resolve(null);
- });
- }
- return result;
- } catch (error) {
- exeContext.errors.push(error);
- return null;
- }
- }
- function executeFieldsSerially(exeContext, parentType, sourceValue, path, fields) {
- return (0, _promiseReduce.default)(Object.keys(fields), function (results, responseName) {
- var fieldNodes = fields[responseName];
- var fieldPath = (0, _Path.addPath)(path, responseName);
- var result = resolveField(exeContext, parentType, sourceValue, fieldNodes, fieldPath);
- if (result === undefined) {
- return results;
- }
- if ((0, _isPromise.default)(result)) {
- return result.then(function (resolvedResult) {
- results[responseName] = resolvedResult;
- return results;
- });
- }
- results[responseName] = result;
- return results;
- }, Object.create(null));
- }
- function executeFields(exeContext, parentType, sourceValue, path, fields) {
- var results = Object.create(null);
- var containsPromise = false;
- for (var _i4 = 0, _Object$keys2 = Object.keys(fields); _i4 < _Object$keys2.length; _i4++) {
- var responseName = _Object$keys2[_i4];
- var fieldNodes = fields[responseName];
- var fieldPath = (0, _Path.addPath)(path, responseName);
- var result = resolveField(exeContext, parentType, sourceValue, fieldNodes, fieldPath);
- if (result !== undefined) {
- results[responseName] = result;
- if (!containsPromise && (0, _isPromise.default)(result)) {
- containsPromise = true;
- }
- }
- }
- if (!containsPromise) {
- return results;
- }
-
-
- return (0, _promiseForObject.default)(results);
- }
- function collectFields(exeContext, runtimeType, selectionSet, fields, visitedFragmentNames) {
- for (var _i6 = 0, _selectionSet$selecti2 = selectionSet.selections; _i6 < _selectionSet$selecti2.length; _i6++) {
- var selection = _selectionSet$selecti2[_i6];
- switch (selection.kind) {
- case _kinds.Kind.FIELD:
- {
- if (!shouldIncludeNode(exeContext, selection)) {
- continue;
- }
- var name = getFieldEntryKey(selection);
- if (!fields[name]) {
- fields[name] = [];
- }
- fields[name].push(selection);
- break;
- }
- case _kinds.Kind.INLINE_FRAGMENT:
- {
- if (!shouldIncludeNode(exeContext, selection) || !doesFragmentConditionMatch(exeContext, selection, runtimeType)) {
- continue;
- }
- collectFields(exeContext, runtimeType, selection.selectionSet, fields, visitedFragmentNames);
- break;
- }
- case _kinds.Kind.FRAGMENT_SPREAD:
- {
- var fragName = selection.name.value;
- if (visitedFragmentNames[fragName] || !shouldIncludeNode(exeContext, selection)) {
- continue;
- }
- visitedFragmentNames[fragName] = true;
- var fragment = exeContext.fragments[fragName];
- if (!fragment || !doesFragmentConditionMatch(exeContext, fragment, runtimeType)) {
- continue;
- }
- collectFields(exeContext, runtimeType, fragment.selectionSet, fields, visitedFragmentNames);
- break;
- }
- }
- }
- return fields;
- }
- function shouldIncludeNode(exeContext, node) {
- var skip = (0, _values.getDirectiveValues)(_directives.GraphQLSkipDirective, node, exeContext.variableValues);
- if (skip && skip.if === true) {
- return false;
- }
- var include = (0, _values.getDirectiveValues)(_directives.GraphQLIncludeDirective, node, exeContext.variableValues);
- if (include && include.if === false) {
- return false;
- }
- return true;
- }
- function doesFragmentConditionMatch(exeContext, fragment, type) {
- var typeConditionNode = fragment.typeCondition;
- if (!typeConditionNode) {
- return true;
- }
- var conditionalType = (0, _typeFromAST.typeFromAST)(exeContext.schema, typeConditionNode);
- if (conditionalType === type) {
- return true;
- }
- if ((0, _definition.isAbstractType)(conditionalType)) {
- return exeContext.schema.isPossibleType(conditionalType, type);
- }
- return false;
- }
- function getFieldEntryKey(node) {
- return node.alias ? node.alias.value : node.name.value;
- }
- function resolveField(exeContext, parentType, source, fieldNodes, path) {
- var fieldNode = fieldNodes[0];
- var fieldName = fieldNode.name.value;
- var fieldDef = getFieldDef(exeContext.schema, parentType, fieldName);
- if (!fieldDef) {
- return;
- }
- var resolveFn = fieldDef.resolve || exeContext.fieldResolver;
- var info = buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path);
-
- var result = resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, source, info);
- return completeValueCatchingError(exeContext, fieldDef.type, fieldNodes, info, path, result);
- }
- function buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path) {
-
-
- return {
- fieldName: fieldDef.name,
- fieldNodes: fieldNodes,
- returnType: fieldDef.type,
- parentType: parentType,
- path: path,
- schema: exeContext.schema,
- fragments: exeContext.fragments,
- rootValue: exeContext.rootValue,
- operation: exeContext.operation,
- variableValues: exeContext.variableValues
- };
- }
- function resolveFieldValueOrError(exeContext, fieldDef, fieldNodes, resolveFn, source, info) {
- try {
-
-
-
- var args = (0, _values.getArgumentValues)(fieldDef, fieldNodes[0], exeContext.variableValues);
-
-
- var _contextValue = exeContext.contextValue;
- var result = resolveFn(source, args, _contextValue, info);
- return (0, _isPromise.default)(result) ? result.then(undefined, asErrorInstance) : result;
- } catch (error) {
- return asErrorInstance(error);
- }
- }
- function asErrorInstance(error) {
- if (error instanceof Error) {
- return error;
- }
- return new Error('Unexpected error value: ' + (0, _inspect.default)(error));
- }
- function completeValueCatchingError(exeContext, returnType, fieldNodes, info, path, result) {
- try {
- var completed;
- if ((0, _isPromise.default)(result)) {
- completed = result.then(function (resolved) {
- return completeValue(exeContext, returnType, fieldNodes, info, path, resolved);
- });
- } else {
- completed = completeValue(exeContext, returnType, fieldNodes, info, path, result);
- }
- if ((0, _isPromise.default)(completed)) {
-
-
- return completed.then(undefined, function (error) {
- return handleFieldError(error, fieldNodes, path, returnType, exeContext);
- });
- }
- return completed;
- } catch (error) {
- return handleFieldError(error, fieldNodes, path, returnType, exeContext);
- }
- }
- function handleFieldError(rawError, fieldNodes, path, returnType, exeContext) {
- var error = (0, _locatedError.locatedError)(asErrorInstance(rawError), fieldNodes, (0, _Path.pathToArray)(path));
-
- if ((0, _definition.isNonNullType)(returnType)) {
- throw error;
- }
-
- exeContext.errors.push(error);
- return null;
- }
- function completeValue(exeContext, returnType, fieldNodes, info, path, result) {
-
- if (result instanceof Error) {
- throw result;
- }
-
- if ((0, _definition.isNonNullType)(returnType)) {
- var completed = completeValue(exeContext, returnType.ofType, fieldNodes, info, path, result);
- if (completed === null) {
- throw new Error("Cannot return null for non-nullable field ".concat(info.parentType.name, ".").concat(info.fieldName, "."));
- }
- return completed;
- }
- if ((0, _isNullish.default)(result)) {
- return null;
- }
- if ((0, _definition.isListType)(returnType)) {
- return completeListValue(exeContext, returnType, fieldNodes, info, path, result);
- }
-
- if ((0, _definition.isLeafType)(returnType)) {
- return completeLeafValue(returnType, result);
- }
-
- if ((0, _definition.isAbstractType)(returnType)) {
- return completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result);
- }
-
- if ((0, _definition.isObjectType)(returnType)) {
- return completeObjectValue(exeContext, returnType, fieldNodes, info, path, result);
- }
-
- (0, _invariant.default)(false, 'Cannot complete value of unexpected output type: ' + (0, _inspect.default)(returnType));
- }
- function completeListValue(exeContext, returnType, fieldNodes, info, path, result) {
- if (!(0, _iterall.isCollection)(result)) {
- throw new _GraphQLError.GraphQLError("Expected Iterable, but did not find one for field ".concat(info.parentType.name, ".").concat(info.fieldName, "."));
- }
-
- var itemType = returnType.ofType;
- var containsPromise = false;
- var completedResults = [];
- (0, _iterall.forEach)(result, function (item, index) {
-
-
- var fieldPath = (0, _Path.addPath)(path, index);
- var completedItem = completeValueCatchingError(exeContext, itemType, fieldNodes, info, fieldPath, item);
- if (!containsPromise && (0, _isPromise.default)(completedItem)) {
- containsPromise = true;
- }
- completedResults.push(completedItem);
- });
- return containsPromise ? Promise.all(completedResults) : completedResults;
- }
- function completeLeafValue(returnType, result) {
- var serializedResult = returnType.serialize(result);
- if ((0, _isInvalid.default)(serializedResult)) {
- throw new Error("Expected a value of type \"".concat((0, _inspect.default)(returnType), "\" but ") + "received: ".concat((0, _inspect.default)(result)));
- }
- return serializedResult;
- }
- function completeAbstractValue(exeContext, returnType, fieldNodes, info, path, result) {
- var resolveTypeFn = returnType.resolveType || exeContext.typeResolver;
- var contextValue = exeContext.contextValue;
- var runtimeType = resolveTypeFn(result, contextValue, info, returnType);
- if ((0, _isPromise.default)(runtimeType)) {
- return runtimeType.then(function (resolvedRuntimeType) {
- return completeObjectValue(exeContext, ensureValidRuntimeType(resolvedRuntimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result);
- });
- }
- return completeObjectValue(exeContext, ensureValidRuntimeType(runtimeType, exeContext, returnType, fieldNodes, info, result), fieldNodes, info, path, result);
- }
- function ensureValidRuntimeType(runtimeTypeOrName, exeContext, returnType, fieldNodes, info, result) {
- var runtimeType = typeof runtimeTypeOrName === 'string' ? exeContext.schema.getType(runtimeTypeOrName) : runtimeTypeOrName;
- if (!(0, _definition.isObjectType)(runtimeType)) {
- throw new _GraphQLError.GraphQLError("Abstract type ".concat(returnType.name, " must resolve to an Object type at runtime for field ").concat(info.parentType.name, ".").concat(info.fieldName, " with ") + "value ".concat((0, _inspect.default)(result), ", received \"").concat((0, _inspect.default)(runtimeType), "\". ") + "Either the ".concat(returnType.name, " type should provide a \"resolveType\" function or each possible type should provide an \"isTypeOf\" function."), fieldNodes);
- }
- if (!exeContext.schema.isPossibleType(returnType, runtimeType)) {
- throw new _GraphQLError.GraphQLError("Runtime Object type \"".concat(runtimeType.name, "\" is not a possible type for \"").concat(returnType.name, "\"."), fieldNodes);
- }
- return runtimeType;
- }
- function completeObjectValue(exeContext, returnType, fieldNodes, info, path, result) {
-
-
-
- if (returnType.isTypeOf) {
- var isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
- if ((0, _isPromise.default)(isTypeOf)) {
- return isTypeOf.then(function (resolvedIsTypeOf) {
- if (!resolvedIsTypeOf) {
- throw invalidReturnTypeError(returnType, result, fieldNodes);
- }
- return collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result);
- });
- }
- if (!isTypeOf) {
- throw invalidReturnTypeError(returnType, result, fieldNodes);
- }
- }
- return collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result);
- }
- function invalidReturnTypeError(returnType, result, fieldNodes) {
- return new _GraphQLError.GraphQLError("Expected value of type \"".concat(returnType.name, "\" but got: ").concat((0, _inspect.default)(result), "."), fieldNodes);
- }
- function collectAndExecuteSubfields(exeContext, returnType, fieldNodes, path, result) {
-
- var subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes);
- return executeFields(exeContext, returnType, result, path, subFieldNodes);
- }
- var collectSubfields = (0, _memoize.default)(_collectSubfields);
- function _collectSubfields(exeContext, returnType, fieldNodes) {
- var subFieldNodes = Object.create(null);
- var visitedFragmentNames = Object.create(null);
- for (var _i8 = 0; _i8 < fieldNodes.length; _i8++) {
- var node = fieldNodes[_i8];
- if (node.selectionSet) {
- subFieldNodes = collectFields(exeContext, returnType, node.selectionSet, subFieldNodes, visitedFragmentNames);
- }
- }
- return subFieldNodes;
- }
- var defaultTypeResolver = function defaultTypeResolver(value, contextValue, info, abstractType) {
-
- if ((0, _isObjectLike.default)(value) && typeof value.__typename === 'string') {
- return value.__typename;
- }
- var possibleTypes = info.schema.getPossibleTypes(abstractType);
- var promisedIsTypeOfResults = [];
- for (var i = 0; i < possibleTypes.length; i++) {
- var type = possibleTypes[i];
- if (type.isTypeOf) {
- var isTypeOfResult = type.isTypeOf(value, contextValue, info);
- if ((0, _isPromise.default)(isTypeOfResult)) {
- promisedIsTypeOfResults[i] = isTypeOfResult;
- } else if (isTypeOfResult) {
- return type;
- }
- }
- }
- if (promisedIsTypeOfResults.length) {
- return Promise.all(promisedIsTypeOfResults).then(function (isTypeOfResults) {
- for (var _i9 = 0; _i9 < isTypeOfResults.length; _i9++) {
- if (isTypeOfResults[_i9]) {
- return possibleTypes[_i9];
- }
- }
- });
- }
- };
- exports.defaultTypeResolver = defaultTypeResolver;
- var defaultFieldResolver = function defaultFieldResolver(source, args, contextValue, info) {
-
- if ((0, _isObjectLike.default)(source) || typeof source === 'function') {
- var property = source[info.fieldName];
- if (typeof property === 'function') {
- return source[info.fieldName](args, contextValue, info);
- }
- return property;
- }
- };
- exports.defaultFieldResolver = defaultFieldResolver;
- function getFieldDef(schema, parentType, fieldName) {
- if (fieldName === _introspection.SchemaMetaFieldDef.name && schema.getQueryType() === parentType) {
- return _introspection.SchemaMetaFieldDef;
- } else if (fieldName === _introspection.TypeMetaFieldDef.name && schema.getQueryType() === parentType) {
- return _introspection.TypeMetaFieldDef;
- } else if (fieldName === _introspection.TypeNameMetaFieldDef.name) {
- return _introspection.TypeNameMetaFieldDef;
- }
- return parentType.getFields()[fieldName];
- }
|