Skip to content

Commit

Permalink
inline completePromisedValue using new helper
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Dec 18, 2022
1 parent b7c6551 commit dd5c269
Showing 1 changed file with 60 additions and 56 deletions.
116 changes: 60 additions & 56 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,14 +722,24 @@ function executeField(
const result = resolveFn(source, args, contextValue, info);

if (isPromise(result)) {
return completePromisedValue(
exeContext,
returnType,
fieldNodes,
info,
path,
return after(
result,
asyncPayloadRecord,
(resolved) =>
completeValue(
exeContext,
returnType,
fieldNodes,
info,
path,
resolved,
asyncPayloadRecord,
),
(rawError) => {
const error = locatedError(rawError, fieldNodes, pathToArray(path));
const handledError = handleFieldError(error, returnType, errors);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return handledError;
},
);
}

Expand Down Expand Up @@ -917,41 +927,6 @@ function completeValue(
);
}

async function completePromisedValue(
exeContext: ExecutionContext,
returnType: GraphQLOutputType,
fieldNodes: ReadonlyArray<FieldNode>,
info: GraphQLResolveInfo,
path: Path,
result: Promise<unknown>,
asyncPayloadRecord?: AsyncPayloadRecord,
): Promise<unknown> {
try {
const resolved = await result;
let completed = completeValue(
exeContext,
returnType,
fieldNodes,
info,
path,
resolved,
asyncPayloadRecord,
);
if (isPromise(completed)) {
// see: https://github.com/tc39/proposal-faster-promise-adoption
// it is faster to await a promise prior to returning it from an async function
completed = await completed;
}
return completed;
} catch (rawError) {
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
const error = locatedError(rawError, fieldNodes, pathToArray(path));
const handledError = handleFieldError(error, returnType, errors);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return handledError;
}
}

/**
* Returns an object containing the `@stream` arguments if a field should be
* streamed based on the experimental flag, stream directive present and
Expand Down Expand Up @@ -1189,14 +1164,28 @@ function completeListItemValue(
): boolean {
if (isPromise(item)) {
completedResults.push(
completePromisedValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
after(
item,
asyncPayloadRecord,
(resolved) =>
completeValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
resolved,
asyncPayloadRecord,
),
(rawError) => {
const error = locatedError(
rawError,
fieldNodes,
pathToArray(itemPath),
);
const handledError = handleFieldError(error, itemType, errors);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return handledError;
},
),
);

Expand Down Expand Up @@ -1917,15 +1906,30 @@ function executeStreamField(
exeContext,
});
if (isPromise(item)) {
const completedItem = completePromisedValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
const completedItem = after(
item,
asyncPayloadRecord,
(resolved) =>
completeValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
resolved,
asyncPayloadRecord,
),
(rawError) => {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return handledError;
},
);

const completedItems = after(
completedItem,
(resolved) => [resolved],
Expand Down

0 comments on commit dd5c269

Please sign in to comment.