Skip to content

Commit 1012e3e

Browse files
authored
fix(stream): allow initialValue of zero (#2979)
* fix(stream): allow initialValue of zero * add test
1 parent 24330c0 commit 1012e3e

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

src/execution/__tests__/stream-test.js

+46
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,52 @@ describe('Execute: stream directive', () => {
351351
]);
352352
});
353353
it('Can stream a field that returns an async iterable', async () => {
354+
const document = parse(`
355+
query {
356+
asyncIterableList @stream {
357+
name
358+
id
359+
}
360+
}
361+
`);
362+
const result = await complete(document);
363+
expect(result).to.deep.equal([
364+
{
365+
data: {
366+
asyncIterableList: [],
367+
},
368+
hasNext: true,
369+
},
370+
{
371+
data: {
372+
name: 'Luke',
373+
id: '1',
374+
},
375+
path: ['asyncIterableList', 0],
376+
hasNext: true,
377+
},
378+
{
379+
data: {
380+
name: 'Han',
381+
id: '2',
382+
},
383+
path: ['asyncIterableList', 1],
384+
hasNext: true,
385+
},
386+
{
387+
data: {
388+
name: 'Leia',
389+
id: '3',
390+
},
391+
path: ['asyncIterableList', 2],
392+
hasNext: true,
393+
},
394+
{
395+
hasNext: false,
396+
},
397+
]);
398+
});
399+
it('Can stream a field that returns an async iterable, using a non-zero initialCount', async () => {
354400
const document = parse(`
355401
query {
356402
asyncIterableList @stream(initialCount: 2) {

src/execution/execute.js

+23-22
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,26 @@ function completeAsyncIteratorValue(
10321032
let containsPromise = false;
10331033
const stream = getStreamValues(exeContext, fieldNodes);
10341034
return new Promise((resolve) => {
1035-
function next(index, completedResults) {
1035+
function advance(index, completedResults) {
1036+
if (
1037+
stream &&
1038+
typeof stream.initialCount === 'number' &&
1039+
index >= stream.initialCount
1040+
) {
1041+
exeContext.dispatcher.addAsyncIteratorValue(
1042+
stream.label,
1043+
index,
1044+
path,
1045+
iterator,
1046+
exeContext,
1047+
fieldNodes,
1048+
info,
1049+
itemType,
1050+
);
1051+
resolve(completedResults);
1052+
return;
1053+
}
1054+
10361055
const fieldPath = addPath(path, index, undefined);
10371056
iterator.next().then(
10381057
({ value, done }) => {
@@ -1067,26 +1086,7 @@ function completeAsyncIteratorValue(
10671086
return;
10681087
}
10691088

1070-
const newIndex = index + 1;
1071-
if (
1072-
stream &&
1073-
typeof stream.initialCount === 'number' &&
1074-
newIndex >= stream.initialCount
1075-
) {
1076-
exeContext.dispatcher.addAsyncIteratorValue(
1077-
stream.label,
1078-
newIndex,
1079-
path,
1080-
iterator,
1081-
exeContext,
1082-
fieldNodes,
1083-
info,
1084-
itemType,
1085-
);
1086-
resolve(completedResults);
1087-
return;
1088-
}
1089-
next(newIndex, completedResults);
1089+
advance(index + 1, completedResults);
10901090
},
10911091
(rawError) => {
10921092
completedResults.push(null);
@@ -1100,7 +1100,8 @@ function completeAsyncIteratorValue(
11001100
},
11011101
);
11021102
}
1103-
next(0, []);
1103+
1104+
advance(0, []);
11041105
}).then((completedResults) =>
11051106
containsPromise ? Promise.all(completedResults) : completedResults,
11061107
);

0 commit comments

Comments
 (0)