Skip to content

Commit

Permalink
use complete helpers within completeListItemValue
Browse files Browse the repository at this point in the history
The optimized helpers change the order of promise resolution and affect
the value of hasNext.
  • Loading branch information
yaacovCR committed Oct 5, 2022
1 parent 52ff552 commit ed82aea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 48 deletions.
5 changes: 5 additions & 0 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,11 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ name: 'Leia', id: '3' }],
path: ['friendList', 2],
Expand Down
66 changes: 18 additions & 48 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,6 @@ async function completeAsyncIteratorValue(
completeListItemValue(
iteration.value,
completedResults,
errors,
exeContext,
itemType,
fieldNodes,
Expand Down Expand Up @@ -1121,7 +1120,6 @@ function completeListValue(
asyncPayloadRecord?: AsyncPayloadRecord,
): PromiseOrValue<ReadonlyArray<unknown>> {
const itemType = returnType.ofType;
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;

if (isAsyncIterable(result)) {
const iterator = result[Symbol.asyncIterator]();
Expand Down Expand Up @@ -1180,7 +1178,6 @@ function completeListValue(
completeListItemValue(
item,
completedResults,
errors,
exeContext,
itemType,
fieldNodes,
Expand All @@ -1206,68 +1203,41 @@ function completeListValue(
function completeListItemValue(
item: unknown,
completedResults: Array<unknown>,
errors: Array<GraphQLError>,
exeContext: ExecutionContext,
itemType: GraphQLOutputType,
fieldNodes: ReadonlyArray<FieldNode>,
info: GraphQLResolveInfo,
itemPath: Path,
asyncPayloadRecord?: AsyncPayloadRecord,
): boolean {
try {
let completedItem;
if (isPromise(item)) {
completedItem = item.then((resolved) =>
completeValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
resolved,
asyncPayloadRecord,
),
);
} else {
completedItem = completeValue(
if (isPromise(item)) {
completedResults.push(
completePromiseCatchingErrors(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
item,
asyncPayloadRecord,
);
}

if (isPromise(completedItem)) {
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
completedResults.push(
completedItem.then(undefined, (rawError) => {
const error = locatedError(
rawError,
fieldNodes,
pathToArray(itemPath),
);
const handledError = handleFieldError(error, itemType, errors);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return handledError;
}),
);
),
);
return true;
}

return true;
}
const completed = completeValueCatchingErrors(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
item,
asyncPayloadRecord,
);

completedResults.push(completedItem);
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(error, itemType, errors);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
completedResults.push(handledError);
}
completedResults.push(completed);

return false;
return isPromise(completed);
}

/**
Expand Down

0 comments on commit ed82aea

Please sign in to comment.