1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 |
- // @flow strict
- import type { Path } from '../jsutils/Path';
- import type { ObjMap } from '../jsutils/ObjMap';
- import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
- import inspect from '../jsutils/inspect';
- import memoize3 from '../jsutils/memoize3';
- import invariant from '../jsutils/invariant';
- import devAssert from '../jsutils/devAssert';
- import isPromise from '../jsutils/isPromise';
- import isObjectLike from '../jsutils/isObjectLike';
- import safeArrayFrom from '../jsutils/safeArrayFrom';
- import promiseReduce from '../jsutils/promiseReduce';
- import promiseForObject from '../jsutils/promiseForObject';
- import { addPath, pathToArray } from '../jsutils/Path';
- import type { GraphQLFormattedError } from '../error/formatError';
- import { GraphQLError } from '../error/GraphQLError';
- import { locatedError } from '../error/locatedError';
- import type {
- DocumentNode,
- OperationDefinitionNode,
- SelectionSetNode,
- FieldNode,
- FragmentSpreadNode,
- InlineFragmentNode,
- FragmentDefinitionNode,
- } from '../language/ast';
- import { Kind } from '../language/kinds';
- import type { GraphQLSchema } from '../type/schema';
- import type {
- GraphQLObjectType,
- GraphQLOutputType,
- GraphQLLeafType,
- GraphQLAbstractType,
- GraphQLField,
- GraphQLFieldResolver,
- GraphQLResolveInfo,
- GraphQLTypeResolver,
- GraphQLList,
- } from '../type/definition';
- import { assertValidSchema } from '../type/validate';
- import {
- SchemaMetaFieldDef,
- TypeMetaFieldDef,
- TypeNameMetaFieldDef,
- } from '../type/introspection';
- import {
- GraphQLIncludeDirective,
- GraphQLSkipDirective,
- } from '../type/directives';
- import {
- isNamedType,
- isObjectType,
- isAbstractType,
- isLeafType,
- isListType,
- isNonNullType,
- } from '../type/definition';
- import { typeFromAST } from '../utilities/typeFromAST';
- import { getOperationRootType } from '../utilities/getOperationRootType';
- import {
- getVariableValues,
- getArgumentValues,
- getDirectiveValues,
- } from './values';
- /**
- * Terminology
- *
- * "Definitions" are the generic name for top-level statements in the document.
- * Examples of this include:
- * 1) Operations (such as a query)
- * 2) Fragments
- *
- * "Operations" are a generic name for requests in the document.
- * Examples of this include:
- * 1) query,
- * 2) mutation
- *
- * "Selections" are the definitions that can appear legally and at
- * single level of the query. These include:
- * 1) field references e.g "a"
- * 2) fragment "spreads" e.g. "...c"
- * 3) inline fragment "spreads" e.g. "...on Type { a }"
- */
- /**
- * Data that must be available at all points during query execution.
- *
- * Namely, schema of the type system that is currently executing,
- * and the fragments defined in the query document
- */
- export type ExecutionContext = {|
- schema: GraphQLSchema,
- fragments: ObjMap<FragmentDefinitionNode>,
- rootValue: mixed,
- contextValue: mixed,
- operation: OperationDefinitionNode,
- variableValues: { [variable: string]: mixed, ... },
- fieldResolver: GraphQLFieldResolver<any, any>,
- typeResolver: GraphQLTypeResolver<any, any>,
- errors: Array<GraphQLError>,
- |};
- /**
- * The result of GraphQL execution.
- *
- * - `errors` is included when any errors occurred as a non-empty array.
- * - `data` is the result of a successful execution of the query.
- * - `extensions` is reserved for adding non-standard properties.
- */
- export type ExecutionResult = {|
- errors?: $ReadOnlyArray<GraphQLError>,
- data?: ObjMap<mixed> | null,
- extensions?: ObjMap<mixed>,
- |};
- export type FormattedExecutionResult = {|
- errors?: $ReadOnlyArray<GraphQLFormattedError>,
- data?: ObjMap<mixed> | null,
- extensions?: ObjMap<mixed>,
- |};
- export type ExecutionArgs = {|
- schema: GraphQLSchema,
- document: DocumentNode,
- rootValue?: mixed,
- contextValue?: mixed,
- variableValues?: ?{ +[variable: string]: mixed, ... },
- operationName?: ?string,
- fieldResolver?: ?GraphQLFieldResolver<any, any>,
- typeResolver?: ?GraphQLTypeResolver<any, any>,
- |};
- /**
- * Implements the "Evaluating requests" section of the GraphQL specification.
- *
- * Returns either a synchronous ExecutionResult (if all encountered resolvers
- * are synchronous), or a Promise of an ExecutionResult that will eventually be
- * resolved and never rejected.
- *
- * If the arguments to this function do not result in a legal execution context,
- * a GraphQLError will be thrown immediately explaining the invalid input.
- *
- * Accepts either an object with named arguments, or individual arguments.
- */
- declare function execute(
- ExecutionArgs,
- ..._: []
- ): PromiseOrValue<ExecutionResult>;
- /* eslint-disable no-redeclare */
- declare function execute(
- schema: GraphQLSchema,
- document: DocumentNode,
- rootValue?: mixed,
- contextValue?: mixed,
- variableValues?: ?{ +[variable: string]: mixed, ... },
- operationName?: ?string,
- fieldResolver?: ?GraphQLFieldResolver<any, any>,
- typeResolver?: ?GraphQLTypeResolver<any, any>,
- ): PromiseOrValue<ExecutionResult>;
- export function execute(
- argsOrSchema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- typeResolver,
- ) {
- /* eslint-enable no-redeclare */
- // Extract arguments from object args if provided.
- return arguments.length === 1
- ? executeImpl(argsOrSchema)
- : executeImpl({
- schema: argsOrSchema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- typeResolver,
- });
- }
- /**
- * Also implements the "Evaluating requests" section of the GraphQL specification.
- * However, it guarantees to complete synchronously (or throw an error) assuming
- * that all field resolvers are also synchronous.
- */
- export function executeSync(args: ExecutionArgs): ExecutionResult {
- const result = executeImpl(args);
- // Assert that the execution was synchronous.
- if (isPromise(result)) {
- throw new Error('GraphQL execution failed to complete synchronously.');
- }
- return result;
- }
- function executeImpl(args: ExecutionArgs): PromiseOrValue<ExecutionResult> {
- const {
- schema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- typeResolver,
- } = args;
- // If arguments are missing or incorrect, throw an error.
- assertValidExecutionArguments(schema, document, variableValues);
- // If a valid execution context cannot be created due to incorrect arguments,
- // a "Response" with only errors is returned.
- const exeContext = buildExecutionContext(
- schema,
- document,
- rootValue,
- contextValue,
- variableValues,
- operationName,
- fieldResolver,
- typeResolver,
- );
- // Return early errors if execution context failed.
- if (Array.isArray(exeContext)) {
- return { errors: exeContext };
- }
- // Return a Promise that will eventually resolve to the data described by
- // The "Response" section of the GraphQL specification.
- //
- // If errors are encountered while executing a GraphQL field, only that
- // field and its descendants will be omitted, and sibling fields will still
- // be executed. An execution which encounters errors will still result in a
- // resolved Promise.
- const data = executeOperation(exeContext, exeContext.operation, rootValue);
- return buildResponse(exeContext, data);
- }
- /**
- * Given a completed execution context and data, build the { errors, data }
- * response defined by the "Response" section of the GraphQL specification.
- */
- function buildResponse(
- exeContext: ExecutionContext,
- data: PromiseOrValue<ObjMap<mixed> | null>,
- ): PromiseOrValue<ExecutionResult> {
- if (isPromise(data)) {
- return data.then((resolved) => buildResponse(exeContext, resolved));
- }
- return exeContext.errors.length === 0
- ? { data }
- : { errors: exeContext.errors, data };
- }
- /**
- * Essential assertions before executing to provide developer feedback for
- * improper use of the GraphQL library.
- *
- * @internal
- */
- export function assertValidExecutionArguments(
- schema: GraphQLSchema,
- document: DocumentNode,
- rawVariableValues: ?{ +[variable: string]: mixed, ... },
- ): void {
- devAssert(document, 'Must provide document.');
- // If the schema used for execution is invalid, throw an error.
- assertValidSchema(schema);
- // Variables, if provided, must be an object.
- devAssert(
- 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.',
- );
- }
- /**
- * Constructs a ExecutionContext object from the arguments passed to
- * execute, which we will pass throughout the other execution methods.
- *
- * Throws a GraphQLError if a valid execution context cannot be created.
- *
- * @internal
- */
- export function buildExecutionContext(
- schema: GraphQLSchema,
- document: DocumentNode,
- rootValue: mixed,
- contextValue: mixed,
- rawVariableValues: ?{ +[variable: string]: mixed, ... },
- operationName: ?string,
- fieldResolver: ?GraphQLFieldResolver<mixed, mixed>,
- typeResolver?: ?GraphQLTypeResolver<mixed, mixed>,
- ): $ReadOnlyArray<GraphQLError> | ExecutionContext {
- let operation: OperationDefinitionNode | void;
- const fragments: ObjMap<FragmentDefinitionNode> = 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?.value === operationName) {
- operation = definition;
- }
- break;
- case Kind.FRAGMENT_DEFINITION:
- fragments[definition.name.value] = definition;
- break;
- }
- }
- if (!operation) {
- if (operationName != null) {
- return [new GraphQLError(`Unknown operation named "${operationName}".`)];
- }
- return [new GraphQLError('Must provide an operation.')];
- }
- // istanbul ignore next (See: 'https://github.com/graphql/graphql-js/issues/2203')
- const variableDefinitions = operation.variableDefinitions ?? [];
- const coercedVariableValues = getVariableValues(
- schema,
- variableDefinitions,
- rawVariableValues ?? {},
- { maxErrors: 50 },
- );
- if (coercedVariableValues.errors) {
- return coercedVariableValues.errors;
- }
- return {
- schema,
- fragments,
- rootValue,
- contextValue,
- operation,
- variableValues: coercedVariableValues.coerced,
- fieldResolver: fieldResolver ?? defaultFieldResolver,
- typeResolver: typeResolver ?? defaultTypeResolver,
- errors: [],
- };
- }
- /**
- * Implements the "Evaluating operations" section of the spec.
- */
- function executeOperation(
- exeContext: ExecutionContext,
- operation: OperationDefinitionNode,
- rootValue: mixed,
- ): PromiseOrValue<ObjMap<mixed> | null> {
- const type = getOperationRootType(exeContext.schema, operation);
- const fields = collectFields(
- exeContext,
- type,
- operation.selectionSet,
- Object.create(null),
- Object.create(null),
- );
- const path = undefined;
- // Errors from sub-fields of a NonNull type may propagate to the top level,
- // at which point we still log the error and null the parent field, which
- // in this case is the entire response.
- try {
- const result =
- operation.operation === 'mutation'
- ? executeFieldsSerially(exeContext, type, rootValue, path, fields)
- : executeFields(exeContext, type, rootValue, path, fields);
- if (isPromise(result)) {
- return result.then(undefined, (error) => {
- exeContext.errors.push(error);
- return Promise.resolve(null);
- });
- }
- return result;
- } catch (error) {
- exeContext.errors.push(error);
- return null;
- }
- }
- /**
- * Implements the "Evaluating selection sets" section of the spec
- * for "write" mode.
- */
- function executeFieldsSerially(
- exeContext: ExecutionContext,
- parentType: GraphQLObjectType,
- sourceValue: mixed,
- path: Path | void,
- fields: ObjMap<Array<FieldNode>>,
- ): PromiseOrValue<ObjMap<mixed>> {
- return promiseReduce(
- Object.keys(fields),
- (results, responseName) => {
- const fieldNodes = fields[responseName];
- const fieldPath = addPath(path, responseName, parentType.name);
- const result = resolveField(
- 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),
- );
- }
- /**
- * Implements the "Evaluating selection sets" section of the spec
- * for "read" mode.
- */
- function executeFields(
- exeContext: ExecutionContext,
- parentType: GraphQLObjectType,
- sourceValue: mixed,
- path: Path | void,
- fields: ObjMap<Array<FieldNode>>,
- ): PromiseOrValue<ObjMap<mixed>> {
- const results = Object.create(null);
- let containsPromise = false;
- for (const responseName of Object.keys(fields)) {
- const fieldNodes = fields[responseName];
- const fieldPath = addPath(path, responseName, parentType.name);
- const result = resolveField(
- exeContext,
- parentType,
- sourceValue,
- fieldNodes,
- fieldPath,
- );
- if (result !== undefined) {
- results[responseName] = result;
- if (isPromise(result)) {
- containsPromise = true;
- }
- }
- }
- // If there are no promises, we can just return the object
- if (!containsPromise) {
- return results;
- }
- // Otherwise, results is a map from field name to the result of resolving that
- // field, which is possibly a promise. Return a promise that will return this
- // same map, but with any promises replaced with the values they resolved to.
- return promiseForObject(results);
- }
- /**
- * Given a selectionSet, adds all of the fields in that selection to
- * the passed in map of fields, and returns it at the end.
- *
- * CollectFields requires the "runtime type" of an object. For a field which
- * returns an Interface or Union type, the "runtime type" will be the actual
- * Object type returned by that field.
- *
- * @internal
- */
- export function collectFields(
- exeContext: ExecutionContext,
- runtimeType: GraphQLObjectType,
- selectionSet: SelectionSetNode,
- fields: ObjMap<Array<FieldNode>>,
- visitedFragmentNames: ObjMap<boolean>,
- ): ObjMap<Array<FieldNode>> {
- for (const selection of selectionSet.selections) {
- switch (selection.kind) {
- case Kind.FIELD: {
- if (!shouldIncludeNode(exeContext, selection)) {
- continue;
- }
- const name = getFieldEntryKey(selection);
- if (!fields[name]) {
- fields[name] = [];
- }
- fields[name].push(selection);
- break;
- }
- case Kind.INLINE_FRAGMENT: {
- if (
- !shouldIncludeNode(exeContext, selection) ||
- !doesFragmentConditionMatch(exeContext, selection, runtimeType)
- ) {
- continue;
- }
- collectFields(
- exeContext,
- runtimeType,
- selection.selectionSet,
- fields,
- visitedFragmentNames,
- );
- break;
- }
- case Kind.FRAGMENT_SPREAD: {
- const fragName = selection.name.value;
- if (
- visitedFragmentNames[fragName] ||
- !shouldIncludeNode(exeContext, selection)
- ) {
- continue;
- }
- visitedFragmentNames[fragName] = true;
- const fragment = exeContext.fragments[fragName];
- if (
- !fragment ||
- !doesFragmentConditionMatch(exeContext, fragment, runtimeType)
- ) {
- continue;
- }
- collectFields(
- exeContext,
- runtimeType,
- fragment.selectionSet,
- fields,
- visitedFragmentNames,
- );
- break;
- }
- }
- }
- return fields;
- }
- /**
- * Determines if a field should be included based on the @include and @skip
- * directives, where @skip has higher precedence than @include.
- */
- function shouldIncludeNode(
- exeContext: ExecutionContext,
- node: FragmentSpreadNode | FieldNode | InlineFragmentNode,
- ): boolean {
- const skip = getDirectiveValues(
- GraphQLSkipDirective,
- node,
- exeContext.variableValues,
- );
- if (skip?.if === true) {
- return false;
- }
- const include = getDirectiveValues(
- GraphQLIncludeDirective,
- node,
- exeContext.variableValues,
- );
- if (include?.if === false) {
- return false;
- }
- return true;
- }
- /**
- * Determines if a fragment is applicable to the given type.
- */
- function doesFragmentConditionMatch(
- exeContext: ExecutionContext,
- fragment: FragmentDefinitionNode | InlineFragmentNode,
- type: GraphQLObjectType,
- ): boolean {
- const typeConditionNode = fragment.typeCondition;
- if (!typeConditionNode) {
- return true;
- }
- const conditionalType = typeFromAST(exeContext.schema, typeConditionNode);
- if (conditionalType === type) {
- return true;
- }
- if (isAbstractType(conditionalType)) {
- return exeContext.schema.isSubType(conditionalType, type);
- }
- return false;
- }
- /**
- * Implements the logic to compute the key of a given field's entry
- */
- function getFieldEntryKey(node: FieldNode): string {
- return node.alias ? node.alias.value : node.name.value;
- }
- /**
- * Resolves the field on the given source object. In particular, this
- * figures out the value that the field returns by calling its resolve function,
- * then calls completeValue to complete promises, serialize scalars, or execute
- * the sub-selection-set for objects.
- */
- function resolveField(
- exeContext: ExecutionContext,
- parentType: GraphQLObjectType,
- source: mixed,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- path: Path,
- ): PromiseOrValue<mixed> {
- const fieldNode = fieldNodes[0];
- const fieldName = fieldNode.name.value;
- const fieldDef = getFieldDef(exeContext.schema, parentType, fieldName);
- if (!fieldDef) {
- return;
- }
- const returnType = fieldDef.type;
- const resolveFn = fieldDef.resolve ?? exeContext.fieldResolver;
- const info = buildResolveInfo(
- exeContext,
- fieldDef,
- fieldNodes,
- parentType,
- path,
- );
- // Get the resolve function, regardless of if its result is normal or abrupt (error).
- try {
- // Build a JS object of arguments from the field.arguments AST, using the
- // variables scope to fulfill any variable references.
- // TODO: find a way to memoize, in case this field is within a List type.
- const args = getArgumentValues(
- fieldDef,
- fieldNodes[0],
- exeContext.variableValues,
- );
- // The resolve function's optional third argument is a context value that
- // is provided to every resolve function within an execution. It is commonly
- // used to represent an authenticated user, or request-specific caches.
- 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)) {
- // 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 = 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);
- }
- }
- /**
- * @internal
- */
- export function buildResolveInfo(
- exeContext: ExecutionContext,
- fieldDef: GraphQLField<mixed, mixed>,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- parentType: GraphQLObjectType,
- path: Path,
- ): GraphQLResolveInfo {
- // The resolve function's optional fourth argument is a collection of
- // information about the current execution state.
- 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: GraphQLError,
- returnType: GraphQLOutputType,
- exeContext: ExecutionContext,
- ): null {
- // If the field type is non-nullable, then it is resolved without any
- // protection from errors, however it still properly locates the error.
- if (isNonNullType(returnType)) {
- throw error;
- }
- // Otherwise, error protection is applied, logging the error and resolving
- // a null value for this field if one is encountered.
- exeContext.errors.push(error);
- return null;
- }
- /**
- * Implements the instructions for completeValue as defined in the
- * "Field entries" section of the spec.
- *
- * If the field type is Non-Null, then this recursively completes the value
- * for the inner type. It throws a field error if that completion returns null,
- * as per the "Nullability" section of the spec.
- *
- * If the field type is a List, then this recursively completes the value
- * for the inner type on each item in the list.
- *
- * If the field type is a Scalar or Enum, ensures the completed value is a legal
- * value of the type by calling the `serialize` method of GraphQL type
- * definition.
- *
- * If the field is an abstract type, determine the runtime type of the value
- * and then complete based on that type
- *
- * Otherwise, the field type expects a sub-selection set, and will complete the
- * value by evaluating all sub-selections.
- */
- function completeValue(
- exeContext: ExecutionContext,
- returnType: GraphQLOutputType,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- info: GraphQLResolveInfo,
- path: Path,
- result: mixed,
- ): PromiseOrValue<mixed> {
- // If result is an Error, throw a located error.
- if (result instanceof Error) {
- throw result;
- }
- // If field type is NonNull, complete for inner type, and throw field error
- // if result is null.
- 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 value is null or undefined then return null.
- if (result == null) {
- return null;
- }
- // If field type is List, complete each item in the list with the inner type
- if (isListType(returnType)) {
- return completeListValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
- // If field type is a leaf type, Scalar or Enum, serialize to a valid value,
- // returning null if serialization is not possible.
- if (isLeafType(returnType)) {
- return completeLeafValue(returnType, result);
- }
- // If field type is an abstract type, Interface or Union, determine the
- // runtime Object type and complete for that type.
- if (isAbstractType(returnType)) {
- return completeAbstractValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
- // If field type is Object, execute and complete all sub-selections.
- // istanbul ignore else (See: 'https://github.com/graphql/graphql-js/issues/2618')
- if (isObjectType(returnType)) {
- return completeObjectValue(
- exeContext,
- returnType,
- fieldNodes,
- info,
- path,
- result,
- );
- }
- // istanbul ignore next (Not reachable. All possible output types have been considered)
- invariant(
- false,
- 'Cannot complete value of unexpected output type: ' +
- inspect((returnType: empty)),
- );
- }
- /**
- * Complete a list value by completing each item in the list with the
- * inner type
- */
- function completeListValue(
- exeContext: ExecutionContext,
- returnType: GraphQLList<GraphQLOutputType>,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- info: GraphQLResolveInfo,
- path: Path,
- result: mixed,
- ): PromiseOrValue<$ReadOnlyArray<mixed>> {
- // This is specified as a simple map, however we're optimizing the path
- // where the list contains no Promises by avoiding creating another Promise.
- const itemType = returnType.ofType;
- let containsPromise = false;
- const completedResults = safeArrayFrom(result, (item, index) => {
- // No need to modify the info object containing the path,
- // since from here on it is not ever accessed by resolver functions.
- 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;
- // Note: we don't rely on a `catch` method, but we do expect "thenable"
- // to take a second callback for the error case.
- 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);
- }
- });
- if (completedResults == null) {
- throw new GraphQLError(
- `Expected Iterable, but did not find one for field "${info.parentType.name}.${info.fieldName}".`,
- );
- }
- return containsPromise ? Promise.all(completedResults) : completedResults;
- }
- /**
- * Complete a Scalar or Enum by serializing to a valid value, returning
- * null if serialization is not possible.
- */
- function completeLeafValue(returnType: GraphQLLeafType, result: mixed): mixed {
- const serializedResult = returnType.serialize(result);
- if (serializedResult === undefined) {
- throw new Error(
- `Expected a value of type "${inspect(returnType)}" but ` +
- `received: ${inspect(result)}`,
- );
- }
- return serializedResult;
- }
- /**
- * Complete a value of an abstract type by determining the runtime object type
- * of that value, then complete the value for that type.
- */
- function completeAbstractValue(
- exeContext: ExecutionContext,
- returnType: GraphQLAbstractType,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- info: GraphQLResolveInfo,
- path: Path,
- result: mixed,
- ): PromiseOrValue<ObjMap<mixed>> {
- const resolveTypeFn = returnType.resolveType ?? 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(
- runtimeTypeOrName: mixed,
- exeContext: ExecutionContext,
- returnType: GraphQLAbstractType,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- info: GraphQLResolveInfo,
- result: mixed,
- ): GraphQLObjectType {
- if (runtimeTypeOrName == 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,
- );
- }
- // FIXME: temporary workaround until support for passing object types would be removed in v16.0.0
- const runtimeTypeName = isNamedType(runtimeTypeOrName)
- ? runtimeTypeOrName.name
- : runtimeTypeOrName;
- 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(runtimeTypeOrName)}".`,
- );
- }
- const runtimeType = exeContext.schema.getType(runtimeTypeName);
- if (runtimeType == null) {
- throw new GraphQLError(
- `Abstract type "${returnType.name}" was resolve to a type "${runtimeTypeName}" that does not exist inside schema.`,
- fieldNodes,
- );
- }
- if (!isObjectType(runtimeType)) {
- throw new GraphQLError(
- `Abstract type "${returnType.name}" was resolve to a non-object type "${runtimeTypeName}".`,
- fieldNodes,
- );
- }
- if (!exeContext.schema.isSubType(returnType, runtimeType)) {
- throw new GraphQLError(
- `Runtime Object type "${runtimeType.name}" is not a possible type for "${returnType.name}".`,
- fieldNodes,
- );
- }
- return runtimeType;
- }
- /**
- * Complete an Object value by executing all sub-selections.
- */
- function completeObjectValue(
- exeContext: ExecutionContext,
- returnType: GraphQLObjectType,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- info: GraphQLResolveInfo,
- path: Path,
- result: mixed,
- ): PromiseOrValue<ObjMap<mixed>> {
- // If there is an isTypeOf predicate function, call it with the
- // current result. If isTypeOf returns false, then raise an error rather
- // than continuing execution.
- 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 collectAndExecuteSubfields(
- exeContext,
- returnType,
- fieldNodes,
- path,
- result,
- );
- });
- }
- if (!isTypeOf) {
- throw invalidReturnTypeError(returnType, result, fieldNodes);
- }
- }
- return collectAndExecuteSubfields(
- exeContext,
- returnType,
- fieldNodes,
- path,
- result,
- );
- }
- function invalidReturnTypeError(
- returnType: GraphQLObjectType,
- result: mixed,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- ): GraphQLError {
- return new GraphQLError(
- `Expected value of type "${returnType.name}" but got: ${inspect(result)}.`,
- fieldNodes,
- );
- }
- function collectAndExecuteSubfields(
- exeContext: ExecutionContext,
- returnType: GraphQLObjectType,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- path: Path,
- result: mixed,
- ): PromiseOrValue<ObjMap<mixed>> {
- // Collect sub-fields to execute to complete this value.
- const subFieldNodes = collectSubfields(exeContext, returnType, fieldNodes);
- return executeFields(exeContext, returnType, result, path, subFieldNodes);
- }
- /**
- * A memoized collection of relevant subfields with regard to the return
- * type. Memoizing ensures the subfields are not repeatedly calculated, which
- * saves overhead when resolving lists of values.
- */
- const collectSubfields = memoize3(_collectSubfields);
- function _collectSubfields(
- exeContext: ExecutionContext,
- returnType: GraphQLObjectType,
- fieldNodes: $ReadOnlyArray<FieldNode>,
- ): ObjMap<Array<FieldNode>> {
- let subFieldNodes = Object.create(null);
- const visitedFragmentNames = Object.create(null);
- for (const node of fieldNodes) {
- if (node.selectionSet) {
- subFieldNodes = collectFields(
- exeContext,
- returnType,
- node.selectionSet,
- subFieldNodes,
- visitedFragmentNames,
- );
- }
- }
- return subFieldNodes;
- }
- /**
- * If a resolveType function is not given, then a default resolve behavior is
- * used which attempts two strategies:
- *
- * First, See if the provided value has a `__typename` field defined, if so, use
- * that value as name of the resolved type.
- *
- * Otherwise, test each possible type for the abstract type by calling
- * isTypeOf for the object being coerced, returning the first type that matches.
- */
- export const defaultTypeResolver: GraphQLTypeResolver<mixed, mixed> = function (
- value,
- contextValue,
- info,
- abstractType,
- ) {
- // First, look for `__typename`.
- if (isObjectLike(value) && typeof value.__typename === 'string') {
- return value.__typename;
- }
- // Otherwise, test each possible type.
- 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;
- }
- }
- });
- }
- };
- /**
- * If a resolve function is not given, then a default resolve behavior is used
- * which takes the property of the source object of the same name as the field
- * and returns it as the result, or if it's a function, returns the result
- * of calling that function while passing along args and context value.
- */
- export const defaultFieldResolver: GraphQLFieldResolver<
- mixed,
- mixed,
- > = function (source: any, args, contextValue, info) {
- // ensure source is a value for which property access is acceptable.
- if (isObjectLike(source) || typeof source === 'function') {
- const property = source[info.fieldName];
- if (typeof property === 'function') {
- return source[info.fieldName](args, contextValue, info);
- }
- return property;
- }
- };
- /**
- * This method looks up the field on the given type definition.
- * It has special casing for the three introspection fields,
- * __schema, __type and __typename. __typename is special because
- * it can always be queried as a field, even in situations where no
- * other fields are allowed, like on a Union. __schema and __type
- * could get automatically added to the query type, but that would
- * require mutating type definitions, which would cause issues.
- *
- * @internal
- */
- export function getFieldDef(
- schema: GraphQLSchema,
- parentType: GraphQLObjectType,
- fieldName: string,
- ): ?GraphQLField<mixed, mixed> {
- 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];
- }
|