Skip to content

Commit

Permalink
remove extra ticks from executeStreamField
Browse files Browse the repository at this point in the history
by using custom completePromise helpers
  • Loading branch information
yaacovCR committed Dec 27, 2022
1 parent 1bf71ee commit e758f46
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 48 deletions.
10 changes: 0 additions & 10 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,6 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ name: 'Leia', id: '3' }],
path: ['friendList', 2],
Expand Down Expand Up @@ -984,11 +979,6 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ nonNullName: 'Han' }],
path: ['friendList', 2],
Expand Down
132 changes: 94 additions & 38 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,78 @@ function executeDeferredFragment(
asyncPayloadRecord.addData(promiseOrData);
}

async function completedItemsFromPromisedItem(
exeContext: ExecutionContext,
itemType: GraphQLOutputType,
fieldNodes: ReadonlyArray<FieldNode>,
info: GraphQLResolveInfo,
path: Path,
itemPath: Path,
item: Promise<unknown>,
asyncPayloadRecord: AsyncPayloadRecord,
): Promise<[unknown] | null> {
try {
try {
const resolved = await item;
let completedItem = completeValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
resolved,
asyncPayloadRecord,
);
if (isPromise(completedItem)) {
completedItem = await completedItem;
}
return [completedItem];
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return [handledError];
}
} catch (error) {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
}
}

async function completedItemsFromPromisedCompletedItem(
exeContext: ExecutionContext,
itemType: GraphQLOutputType,
fieldNodes: ReadonlyArray<FieldNode>,
path: Path,
itemPath: Path,
completedItem: Promise<unknown>,
asyncPayloadRecord: AsyncPayloadRecord,
): Promise<[unknown] | null> {
try {
try {
return [await completedItem];
} catch (rawError) {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return [handledError];
}
} catch (error) {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
}
}

function executeStreamField(
path: Path,
itemPath: Path,
Expand All @@ -1816,24 +1888,18 @@ function executeStreamField(
exeContext,
});
if (isPromise(item)) {
const completedItems = completePromisedValue(
exeContext,
itemType,
fieldNodes,
info,
itemPath,
item,
asyncPayloadRecord,
).then(
(value) => [value],
(error) => {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
},
asyncPayloadRecord.addItems(
completedItemsFromPromisedItem(
exeContext,
itemType,
fieldNodes,
info,
path,
itemPath,
item,
asyncPayloadRecord,
),
);

asyncPayloadRecord.addItems(completedItems);
return asyncPayloadRecord;
}

Expand Down Expand Up @@ -1866,27 +1932,17 @@ function executeStreamField(
}

if (isPromise(completedItem)) {
const completedItems = completedItem
.then(undefined, (rawError) => {
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
const handledError = handleFieldError(
error,
itemType,
asyncPayloadRecord.errors,
);
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
return handledError;
})
.then(
(value) => [value],
(error) => {
asyncPayloadRecord.errors.push(error);
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
return null;
},
);

asyncPayloadRecord.addItems(completedItems);
asyncPayloadRecord.addItems(
completedItemsFromPromisedCompletedItem(
exeContext,
itemType,
fieldNodes,
path,
itemPath,
completedItem,
asyncPayloadRecord,
),
);
return asyncPayloadRecord;
}

Expand Down

0 comments on commit e758f46

Please sign in to comment.