* Implements the "Executing fields" section of the spec * In particular, this function 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.
( exeContext: ExecutionContext, parentType: GraphQLObjectType, source: unknown, fieldNodes: Array<FieldNode>, path: Path, asyncPayloadRecord?: AsyncPayloadRecord, )
| 692 | * serialize scalars, or execute the sub-selection-set for objects. |
| 693 | */ |
| 694 | function executeField( |
| 695 | exeContext: ExecutionContext, |
| 696 | parentType: GraphQLObjectType, |
| 697 | source: unknown, |
| 698 | fieldNodes: Array<FieldNode>, |
| 699 | path: Path, |
| 700 | asyncPayloadRecord?: AsyncPayloadRecord, |
| 701 | ): MaybePromise<unknown> { |
| 702 | const errors = asyncPayloadRecord?.errors ?? exeContext.errors; |
| 703 | const fieldDef = getFieldDef(exeContext.schema, parentType, fieldNodes[0]); |
| 704 | if (!fieldDef) { |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | const returnType = fieldDef.type; |
| 709 | const resolveFn = fieldDef.resolve ?? exeContext.fieldResolver; |
| 710 | |
| 711 | const info = buildResolveInfo(exeContext, fieldDef, fieldNodes, parentType, path); |
| 712 | |
| 713 | // Get the resolve function, regardless of if its result is normal or abrupt (error). |
| 714 | try { |
| 715 | exeContext.signal?.throwIfAborted(); |
| 716 | // Build a JS object of arguments from the field.arguments AST, using the |
| 717 | // variables scope to fulfill any variable references. |
| 718 | // TODO: find a way to memoize, in case this field is within a List type. |
| 719 | const args = getArgumentValues(fieldDef, fieldNodes[0], exeContext.variableValues); |
| 720 | |
| 721 | // The resolve function's optional third argument is a context value that |
| 722 | // is provided to every resolve function within an execution. It is commonly |
| 723 | // used to represent an authenticated user, or request-specific caches. |
| 724 | const contextValue = exeContext.contextValue; |
| 725 | |
| 726 | const result = resolveFn(source, args, contextValue, info); |
| 727 | |
| 728 | let completed; |
| 729 | if (isPromise(result)) { |
| 730 | completed = result.then(resolved => |
| 731 | completeValue(exeContext, returnType, fieldNodes, info, path, resolved, asyncPayloadRecord), |
| 732 | ); |
| 733 | } else { |
| 734 | completed = completeValue( |
| 735 | exeContext, |
| 736 | returnType, |
| 737 | fieldNodes, |
| 738 | info, |
| 739 | path, |
| 740 | result, |
| 741 | asyncPayloadRecord, |
| 742 | ); |
| 743 | } |
| 744 | |
| 745 | if (isPromise(completed)) { |
| 746 | // Note: we don't rely on a `catch` method, but we do expect "thenable" |
| 747 | // to take a second callback for the error case. |
| 748 | return completed.then(undefined, rawError => { |
| 749 | if (rawError instanceof AggregateError) { |
| 750 | let result: unknown; |
| 751 | for (let rawErrorItem of rawError.errors) { |
no test coverage detected
searching dependent graphs…