Skip to content

Commit 1782672

Browse files
committed
remove extra ticks from executeStreamField
by using custom completePromise helpers
1 parent 1bf71ee commit 1782672

File tree

2 files changed

+88
-48
lines changed

2 files changed

+88
-48
lines changed

src/execution/__tests__/stream-test.ts

-10
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,6 @@ describe('Execute: stream directive', () => {
531531
},
532532
],
533533
},
534-
],
535-
hasNext: true,
536-
},
537-
{
538-
incremental: [
539534
{
540535
items: [{ name: 'Leia', id: '3' }],
541536
path: ['friendList', 2],
@@ -984,11 +979,6 @@ describe('Execute: stream directive', () => {
984979
},
985980
],
986981
},
987-
],
988-
hasNext: true,
989-
},
990-
{
991-
incremental: [
992982
{
993983
items: [{ nonNullName: 'Han' }],
994984
path: ['friendList', 2],

src/execution/execute.ts

+88-38
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,72 @@ function executeDeferredFragment(
17981798
asyncPayloadRecord.addData(promiseOrData);
17991799
}
18001800

1801+
async function completedItemsFromPromisedItem(
1802+
exeContext: ExecutionContext,
1803+
itemType: GraphQLOutputType,
1804+
fieldNodes: ReadonlyArray<FieldNode>,
1805+
info: GraphQLResolveInfo,
1806+
path: Path,
1807+
itemPath: Path,
1808+
item: Promise<unknown>,
1809+
asyncPayloadRecord: AsyncPayloadRecord,
1810+
): Promise<[unknown] | null> {
1811+
try {
1812+
try {
1813+
const resolved = await item;
1814+
let completed = completeValue(
1815+
exeContext,
1816+
itemType,
1817+
fieldNodes,
1818+
info,
1819+
itemPath,
1820+
resolved,
1821+
asyncPayloadRecord,
1822+
);
1823+
if (isPromise(completed)) {
1824+
completed = await completed;
1825+
}
1826+
return [completed];
1827+
} catch (rawError) {
1828+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
1829+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1830+
const handledError = handleFieldError(error, itemType, errors);
1831+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1832+
return [handledError];
1833+
}
1834+
} catch (error) {
1835+
asyncPayloadRecord.errors.push(error);
1836+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
1837+
return null;
1838+
}
1839+
}
1840+
1841+
async function completedItemsFromPromisedCompletedItem(
1842+
exeContext: ExecutionContext,
1843+
itemType: GraphQLOutputType,
1844+
fieldNodes: ReadonlyArray<FieldNode>,
1845+
path: Path,
1846+
itemPath: Path,
1847+
completedItem: Promise<unknown>,
1848+
asyncPayloadRecord: AsyncPayloadRecord,
1849+
): Promise<[unknown] | null> {
1850+
try {
1851+
try {
1852+
return [await completedItem];
1853+
} catch (rawError) {
1854+
const errors = asyncPayloadRecord?.errors ?? exeContext.errors;
1855+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1856+
const handledError = handleFieldError(error, itemType, errors);
1857+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1858+
return [handledError];
1859+
}
1860+
} catch (error) {
1861+
asyncPayloadRecord.errors.push(error);
1862+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
1863+
return null;
1864+
}
1865+
}
1866+
18011867
function executeStreamField(
18021868
path: Path,
18031869
itemPath: Path,
@@ -1816,24 +1882,18 @@ function executeStreamField(
18161882
exeContext,
18171883
});
18181884
if (isPromise(item)) {
1819-
const completedItems = completePromisedValue(
1820-
exeContext,
1821-
itemType,
1822-
fieldNodes,
1823-
info,
1824-
itemPath,
1825-
item,
1826-
asyncPayloadRecord,
1827-
).then(
1828-
(value) => [value],
1829-
(error) => {
1830-
asyncPayloadRecord.errors.push(error);
1831-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
1832-
return null;
1833-
},
1885+
asyncPayloadRecord.addItems(
1886+
completedItemsFromPromisedItem(
1887+
exeContext,
1888+
itemType,
1889+
fieldNodes,
1890+
info,
1891+
path,
1892+
itemPath,
1893+
item,
1894+
asyncPayloadRecord,
1895+
),
18341896
);
1835-
1836-
asyncPayloadRecord.addItems(completedItems);
18371897
return asyncPayloadRecord;
18381898
}
18391899

@@ -1866,27 +1926,17 @@ function executeStreamField(
18661926
}
18671927

18681928
if (isPromise(completedItem)) {
1869-
const completedItems = completedItem
1870-
.then(undefined, (rawError) => {
1871-
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1872-
const handledError = handleFieldError(
1873-
error,
1874-
itemType,
1875-
asyncPayloadRecord.errors,
1876-
);
1877-
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1878-
return handledError;
1879-
})
1880-
.then(
1881-
(value) => [value],
1882-
(error) => {
1883-
asyncPayloadRecord.errors.push(error);
1884-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
1885-
return null;
1886-
},
1887-
);
1888-
1889-
asyncPayloadRecord.addItems(completedItems);
1929+
asyncPayloadRecord.addItems(
1930+
completedItemsFromPromisedCompletedItem(
1931+
exeContext,
1932+
itemType,
1933+
fieldNodes,
1934+
path,
1935+
itemPath,
1936+
completedItem,
1937+
asyncPayloadRecord,
1938+
),
1939+
);
18901940
return asyncPayloadRecord;
18911941
}
18921942

0 commit comments

Comments
 (0)