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 12, 2022
1 parent 91bc8c0 commit e5d982f
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 @@ -1092,7 +1092,6 @@ async function completeAsyncIteratorValue(
completeListItemValue(
iteration.value,
completedResults,
errors,
exeContext,
itemType,
fieldNodes,
Expand Down Expand Up @@ -1122,7 +1121,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 @@ -1181,7 +1179,6 @@ function completeListValue(
completeListItemValue(
item,
completedResults,
errors,
exeContext,
itemType,
fieldNodes,
Expand All @@ -1207,68 +1204,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 e5d982f

Please sign in to comment.