1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009 |
- import { devAssert } from '../jsutils/devAssert.mjs';
- import { inspect } from '../jsutils/inspect.mjs';
- import { invariant } from '../jsutils/invariant.mjs';
- import { isIterableObject } from '../jsutils/isIterableObject.mjs';
- import { isObjectLike } from '../jsutils/isObjectLike.mjs';
- import { isPromise } from '../jsutils/isPromise.mjs';
- import { memoize3 } from '../jsutils/memoize3.mjs';
- import { addPath, pathToArray } from '../jsutils/Path.mjs';
- import { promiseForObject } from '../jsutils/promiseForObject.mjs';
- import { promiseReduce } from '../jsutils/promiseReduce.mjs';
- import { GraphQLError } from '../error/GraphQLError.mjs';
- import { locatedError } from '../error/locatedError.mjs';
- import { OperationTypeNode } from '../language/ast.mjs';
- import { Kind } from '../language/kinds.mjs';
- import {
- isAbstractType,
- isLeafType,
- isListType,
- isNonNullType,
- isObjectType,
- } from '../type/definition.mjs';
- import {
- SchemaMetaFieldDef,
- TypeMetaFieldDef,
- TypeNameMetaFieldDef,
- } from '../type/introspection.mjs';
- import { assertValidSchema } from '../type/validate.mjs';
- import {
- collectFields,
- collectSubfields as _collectSubfields,
- } from './collectFields.mjs';
- import { getArgumentValues, getVariableValues } from './values.mjs';
- const collectSubfields = memoize3((exeContext, returnType, fieldNodes) =>
- _collectSubfields(
- exeContext.schema,
- exeContext.fragments,
- exeContext.variableValues,
- returnType,
- fieldNodes,
- ),
- );
- export function execute(args) {
-
- arguments.length < 2 ||
- devAssert(
- false,
- 'graphql@16 dropped long-deprecated support for positional arguments, please pass an object instead.',
- );
- const { schema, document, variableValues, rootValue } = args;
- assertValidExecutionArguments(schema, document, variableValues);
-
- const exeContext = buildExecutionContext(args);
- if (!('schema' in exeContext)) {
- return {
- errors: exeContext,
- };
- }
-
-
-
-
-
-
-
-
-
-
- try {
- const { operation } = exeContext;
- const result = executeOperation(exeContext, operation, rootValue);
- if (isPromise(result)) {
- return result.then(
- (data) => buildResponse(data, exeContext.errors),
- (error) => {
- exeContext.errors.push(error);
- return buildResponse(null, exeContext.errors);
- },
- );
- }
- return buildResponse(result, exeContext.errors);
- } catch (error) {
- exeContext.errors.push(error);
- return buildResponse(null, exeContext.errors);
- }
- }
- export function executeSync(args) {
- const result = execute(args);
- if (isPromise(result)) {
- throw new Error('GraphQL execution failed to complete synchronously.');
- }
- return result;
- }
- function buildResponse(data, errors) {
- return errors.length === 0
- ? {
- data,
- }
- : {
- errors,
- data,
- };
- }
- export function assertValidExecutionArguments(
- schema,
- document,
- rawVariableValues,
- ) {
- document || devAssert(false, 'Must provide document.');
- assertValidSchema(schema);
- rawVariableValues == null ||
- isObjectLike(rawVariableValues) ||
- devAssert(
- false,
- '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.',
- );
- }
- export function buildExecutionContext(args) {
- var _definition$name, _operation$variableDe;
- const {
- schema,
- document,
- rootValue,
- contextValue,
- variableValues: rawVariableValues,
- operationName,
- fieldResolver,
- typeResolver,
- subscribeFieldResolver,
- } = args;
- let operation;
- const fragments = Object.create(null);
- for (const definition of document.definitions) {
- switch (definition.kind) {
- case Kind.OPERATION_DEFINITION:
- if (operationName == null) {
- if (operation !== undefined) {
- return [
- new GraphQLError(
- 'Must provide operation name if query contains multiple operations.',
- ),
- ];
- }
- operation = definition;
- } else if (
- ((_definition$name = definition.name) === null ||
- _definition$name === void 0
- ? void 0
- : _definition$name.value) === operationName
- ) {
- operation = definition;
- }
- break;
- case Kind.FRAGMENT_DEFINITION:
- fragments[definition.name.value] = definition;
- break;
- default:
- }
- }
- if (!operation) {
- if (operationName != null) {
- return [new GraphQLError(`Unknown operation named "${operationName}".`)];
- }
- return [new GraphQLError('Must provide an operation.')];
- }
-
- const variableDefinitions =
- (_operation$variableDe = operation.variableDefinitions) !== null &&
- _operation$variableDe !== void 0
- ? _operation$variableDe
- : [];
- const coercedVariableValues = getVariableValues(
- schema,
- variableDefinitions,
- rawVariableValues !== null && rawVariableValues !== void 0
- ? rawVariableValues
- : {},
- {
- maxErrors: 50,
- },
- );
- if (coercedVariableValues.errors) {
- return coercedVariableValues.errors;
- }
- return {
- schema,
- fragments,
- rootValue,
- contextValue,
- operation,
- variableValues: coercedVariableValues.coerced,
- fieldResolver:
- fieldResolver !== null && fieldResolver !== void 0
- ? fieldResolver
- : defaultFieldResolver,
- typeResolver:
- typeResolver !== null && typeResolver !== void 0
- ? typeResolver
- : defaultTypeResolver,
- subscribeFieldResolver:
- subscribeFieldResolver !== null && subscribeFieldResolver !== void 0
- ? subscribeFieldResolver
- : defaultFieldResolver,
- errors: [],
- };
- }
- function executeOperation(exeContext, operation, rootValue) {
- const rootType = exeContext.schema.getRootType(operation.operation);
- if (rootType == null) {
- throw new GraphQLError(
- `Schema is not configured to execute ${operation.operation} operation.`,
- {
- nodes: operation,
- },
- );
- }
- const rootFields = collectFields(
- exeContext.schema,
- exeContext.fragments,
- exeContext.variableValues,
- rootType,
- operation.selectionSet,
- );
- const path = undefined;
- switch (operation.operation) {
- case OperationTypeNode.QUERY:
- return executeFields(exeContext, rootType, rootValue, path, rootFields);
- case OperationTypeNode.MUTATION:
- return executeFieldsSerially(
- exeContext,
- rootType,
- rootValue,
- path,
- rootFields,
- );
- case OperationTypeNode.SUBSCRIPTION:
-
-
- return executeFields(exeContext, rootType, rootValue, path, rootFields);
- }
- }
- function executeFieldsSerially(
- exeContext,
- parentType,
- sourceValue,
- path,
- fields,
- ) {
- return promiseReduce(
- fields.entries(),
- (results, [responseName, fieldNodes]) => {
- const fieldPath = addPath(path, responseName, parentType.name);
- const result = executeField(
- exeContext,
- parentType,
- sourceValue,
- fieldNodes,
- fieldPath,
- );
- if (result === undefined) {
- return results;
- }
- if (isPromise(result)) {
- return result.then((resolvedResult) => {
- results[responseName] = resolvedResult;
- return results;
- });
- }
- results[responseName] = result;
- return results;
- },
- Object.create(null),
- );
- }
- function executeFields(exeContext, parentType, sourceValue, path, fields) {
- const results = Object.create(null);
- let containsPromise = false;
- try {
- for (const [responseName, fieldNodes] of fields.entries()) {
- const fieldPath = addPath(path, responseName, parentType.name);
- const result = executeField(
- exeContext,
- parentType,
- sourceValue,
- fieldNodes,
- fieldPath,
- );
- if (result !== undefined) {
- results[responseName] = result;
- if (isPromise(result)) {
- containsPromise = true;
- }
- }
- }
- } catch (error) {
- if (containsPromise) {
-
- return promiseForObject(results).finally(() => {
- throw error;
- });
- }
- throw error;
- }
- if (!containsPromise) {
- return results;
- }
-
-
- return promiseForObject(results);
- }
- function executeField(exeContext, parentType, source, fieldNodes, path) {
- var _fieldDef$resolve;
- const fieldDef = getFieldDef(exeContext.schema, parentType, fieldNodes[0]);
- if (!fieldDef) {
- return;
- }
- const returnType = fieldDef.type;
- const resolveFn =
- (_fieldDef$resolve = fieldDef.resolve) !== null &&
- _fieldDef$resolve !== void 0
- ? _fieldDef$resolve
- : exeContext.fieldResolver;
- const info = buildResolveInfo(
- exeContext,
- fieldDef,
- fieldNodes,
- parentType,
- path,
- );
- try {
-
-
-
- const args = getArgumentValues(
- fieldDef,
- fieldNodes[0],
- exeContext.variableValues,
- );
-
-
- const contextValue = exeContext.contextValue;
- const result = resolveFn(source, args, contextValue, info);
- let completed;
- if (isPromise(result)) {
- completed = result.then((resolved) =>
- completeValue(exeContext, returnType, fieldNodes, info, path, resolved),
- );
- } else {
- completed = completeValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
- if (isPromise(completed)) {
-
-
- return completed.then(undefined, (rawError) => {
- const error = locatedError(rawError, fieldNodes, pathToArray(path));
- return handleFieldError(error, returnType, exeContext);
- });
- }
- return completed;
- } catch (rawError) {
- const error = locatedError(rawError, fieldNodes, pathToArray(path));
- return handleFieldError(error, returnType, exeContext);
- }
- }
- export function buildResolveInfo(
- exeContext,
- fieldDef,
- fieldNodes,
- parentType,
- path,
- ) {
-
-
- return {
- fieldName: fieldDef.name,
- fieldNodes,
- returnType: fieldDef.type,
- parentType,
- path,
- schema: exeContext.schema,
- fragments: exeContext.fragments,
- rootValue: exeContext.rootValue,
- operation: exeContext.operation,
- variableValues: exeContext.variableValues,
- };
- }
- function handleFieldError(error, returnType, exeContext) {
-
-
- if (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 (isNonNullType(returnType)) {
- const completed = completeValue(
- exeContext,
- returnType.ofType,
- fieldNodes,
- info,
- path,
- result,
- );
- if (completed === null) {
- throw new Error(
- `Cannot return null for non-nullable field ${info.parentType.name}.${info.fieldName}.`,
- );
- }
- return completed;
- }
- if (result == null) {
- return null;
- }
- if (isListType(returnType)) {
- return completeListValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
-
- if (isLeafType(returnType)) {
- return completeLeafValue(returnType, result);
- }
-
- if (isAbstractType(returnType)) {
- return completeAbstractValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
- if (isObjectType(returnType)) {
- return completeObjectValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
-
-
- false ||
- invariant(
- false,
- 'Cannot complete value of unexpected output type: ' + inspect(returnType),
- );
- }
- function completeListValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- ) {
- if (!isIterableObject(result)) {
- throw new GraphQLError(
- `Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`,
- );
- }
-
- const itemType = returnType.ofType;
- let containsPromise = false;
- const completedResults = Array.from(result, (item, index) => {
-
-
- const itemPath = addPath(path, index, undefined);
- try {
- let completedItem;
- if (isPromise(item)) {
- completedItem = item.then((resolved) =>
- completeValue(
- exeContext,
- itemType,
- fieldNodes,
- info,
- itemPath,
- resolved,
- ),
- );
- } else {
- completedItem = completeValue(
- exeContext,
- itemType,
- fieldNodes,
- info,
- itemPath,
- item,
- );
- }
- if (isPromise(completedItem)) {
- containsPromise = true;
-
- return completedItem.then(undefined, (rawError) => {
- const error = locatedError(
- rawError,
- fieldNodes,
- pathToArray(itemPath),
- );
- return handleFieldError(error, itemType, exeContext);
- });
- }
- return completedItem;
- } catch (rawError) {
- const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
- return handleFieldError(error, itemType, exeContext);
- }
- });
- return containsPromise ? Promise.all(completedResults) : completedResults;
- }
- function completeLeafValue(returnType, result) {
- const serializedResult = returnType.serialize(result);
- if (serializedResult == null) {
- throw new Error(
- `Expected \`${inspect(returnType)}.serialize(${inspect(result)})\` to ` +
- `return non-nullable value, returned: ${inspect(serializedResult)}`,
- );
- }
- return serializedResult;
- }
- function completeAbstractValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- ) {
- var _returnType$resolveTy;
- const resolveTypeFn =
- (_returnType$resolveTy = returnType.resolveType) !== null &&
- _returnType$resolveTy !== void 0
- ? _returnType$resolveTy
- : exeContext.typeResolver;
- const contextValue = exeContext.contextValue;
- const runtimeType = resolveTypeFn(result, contextValue, info, returnType);
- if (isPromise(runtimeType)) {
- return runtimeType.then((resolvedRuntimeType) =>
- 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(
- runtimeTypeName,
- exeContext,
- returnType,
- fieldNodes,
- info,
- result,
- ) {
- if (runtimeTypeName == null) {
- throw new 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.`,
- fieldNodes,
- );
- }
-
- if (isObjectType(runtimeTypeName)) {
- throw new GraphQLError(
- 'Support for returning GraphQLObjectType from resolveType was removed in graphql-js@16.0.0 please return type name instead.',
- );
- }
- if (typeof runtimeTypeName !== 'string') {
- throw new GraphQLError(
- `Abstract type "${returnType.name}" must resolve to an Object type at runtime for field "${info.parentType.name}.${info.fieldName}" with ` +
- `value ${inspect(result)}, received "${inspect(runtimeTypeName)}".`,
- );
- }
- const runtimeType = exeContext.schema.getType(runtimeTypeName);
- if (runtimeType == null) {
- throw new GraphQLError(
- `Abstract type "${returnType.name}" was resolved to a type "${runtimeTypeName}" that does not exist inside the schema.`,
- {
- nodes: fieldNodes,
- },
- );
- }
- if (!isObjectType(runtimeType)) {
- throw new GraphQLError(
- `Abstract type "${returnType.name}" was resolved to a non-object type "${runtimeTypeName}".`,
- {
- nodes: fieldNodes,
- },
- );
- }
- if (!exeContext.schema.isSubType(returnType, runtimeType)) {
- throw new GraphQLError(
- `Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`,
- {
- nodes: fieldNodes,
- },
- );
- }
- return runtimeType;
- }
- function completeObjectValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- ) {
-
- const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes);
-
-
- if (returnType.isTypeOf) {
- const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
- if (isPromise(isTypeOf)) {
- return isTypeOf.then((resolvedIsTypeOf) => {
- if (!resolvedIsTypeOf) {
- throw invalidReturnTypeError(returnType, result, fieldNodes);
- }
- return executeFields(
- exeContext,
- returnType,
- result,
- path,
- subFieldNodes,
- );
- });
- }
- if (!isTypeOf) {
- throw invalidReturnTypeError(returnType, result, fieldNodes);
- }
- }
- return executeFields(exeContext, returnType, result, path, subFieldNodes);
- }
- function invalidReturnTypeError(returnType, result, fieldNodes) {
- return new GraphQLError(
- `Expected value of type "${returnType.name}" but got: ${inspect(result)}.`,
- {
- nodes: fieldNodes,
- },
- );
- }
- export const defaultTypeResolver = function (
- value,
- contextValue,
- info,
- abstractType,
- ) {
-
- if (isObjectLike(value) && typeof value.__typename === 'string') {
- return value.__typename;
- }
- const possibleTypes = info.schema.getPossibleTypes(abstractType);
- const promisedIsTypeOfResults = [];
- for (let i = 0; i < possibleTypes.length; i++) {
- const type = possibleTypes[i];
- if (type.isTypeOf) {
- const isTypeOfResult = type.isTypeOf(value, contextValue, info);
- if (isPromise(isTypeOfResult)) {
- promisedIsTypeOfResults[i] = isTypeOfResult;
- } else if (isTypeOfResult) {
- return type.name;
- }
- }
- }
- if (promisedIsTypeOfResults.length) {
- return Promise.all(promisedIsTypeOfResults).then((isTypeOfResults) => {
- for (let i = 0; i < isTypeOfResults.length; i++) {
- if (isTypeOfResults[i]) {
- return possibleTypes[i].name;
- }
- }
- });
- }
- };
- export const defaultFieldResolver = function (
- source,
- args,
- contextValue,
- info,
- ) {
-
- if (isObjectLike(source) || typeof source === 'function') {
- const property = source[info.fieldName];
- if (typeof property === 'function') {
- return source[info.fieldName](args, contextValue, info);
- }
- return property;
- }
- };
- export function getFieldDef(schema, parentType, fieldNode) {
- const fieldName = fieldNode.name.value;
- if (
- fieldName === SchemaMetaFieldDef.name &&
- schema.getQueryType() === parentType
- ) {
- return SchemaMetaFieldDef;
- } else if (
- fieldName === TypeMetaFieldDef.name &&
- schema.getQueryType() === parentType
- ) {
- return TypeMetaFieldDef;
- } else if (fieldName === TypeNameMetaFieldDef.name) {
- return TypeNameMetaFieldDef;
- }
- return parentType.getFields()[fieldName];
- }
|