Skip to content

Commit e758f46

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

File tree

2 files changed

+94
-48
lines changed

2 files changed

+94
-48
lines changed

src/execution/__tests__/stream-test.ts

Lines changed: 0 additions & 10 deletions
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

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,78 @@ 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 completedItem = completeValue(
1815+
exeContext,
1816+
itemType,
1817+
fieldNodes,
1818+
info,
1819+
itemPath,
1820+
resolved,
1821+
asyncPayloadRecord,
1822+
);
1823+
if (isPromise(completedItem)) {
1824+
completedItem = await completedItem;
1825+
}
1826+
return [completedItem];
1827+
} catch (rawError) {
1828+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1829+
const handledError = handleFieldError(
1830+
error,
1831+
itemType,
1832+
asyncPayloadRecord.errors,
1833+
);
1834+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1835+
return [handledError];
1836+
}
1837+
} catch (error) {
1838+
asyncPayloadRecord.errors.push(error);
1839+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
1840+
return null;
1841+
}
1842+
}
1843+
1844+
async function completedItemsFromPromisedCompletedItem(
1845+
exeContext: ExecutionContext,
1846+
itemType: GraphQLOutputType,
1847+
fieldNodes: ReadonlyArray<FieldNode>,
1848+
path: Path,
1849+
itemPath: Path,
1850+
completedItem: Promise<unknown>,
1851+
asyncPayloadRecord: AsyncPayloadRecord,
1852+
): Promise<[unknown] | null> {
1853+
try {
1854+
try {
1855+
return [await completedItem];
1856+
} catch (rawError) {
1857+
const error = locatedError(rawError, fieldNodes, pathToArray(itemPath));
1858+
const handledError = handleFieldError(
1859+
error,
1860+
itemType,
1861+
asyncPayloadRecord.errors,
1862+
);
1863+
filterSubsequentPayloads(exeContext, itemPath, asyncPayloadRecord);
1864+
return [handledError];
1865+
}
1866+
} catch (error) {
1867+
asyncPayloadRecord.errors.push(error);
1868+
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
1869+
return null;
1870+
}
1871+
}
1872+
18011873
function executeStreamField(
18021874
path: Path,
18031875
itemPath: Path,
@@ -1816,24 +1888,18 @@ function executeStreamField(
18161888
exeContext,
18171889
});
18181890
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-
},
1891+
asyncPayloadRecord.addItems(
1892+
completedItemsFromPromisedItem(
1893+
exeContext,
1894+
itemType,
1895+
fieldNodes,
1896+
info,
1897+
path,
1898+
itemPath,
1899+
item,
1900+
asyncPayloadRecord,
1901+
),
18341902
);
1835-
1836-
asyncPayloadRecord.addItems(completedItems);
18371903
return asyncPayloadRecord;
18381904
}
18391905

@@ -1866,27 +1932,17 @@ function executeStreamField(
18661932
}
18671933

18681934
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);
1935+
asyncPayloadRecord.addItems(
1936+
completedItemsFromPromisedCompletedItem(
1937+
exeContext,
1938+
itemType,
1939+
fieldNodes,
1940+
path,
1941+
itemPath,
1942+
completedItem,
1943+
asyncPayloadRecord,
1944+
),
1945+
);
18901946
return asyncPayloadRecord;
18911947
}
18921948

0 commit comments

Comments
 (0)