From 38f678c9c7433191932ad7d42a16c8ee57783a86 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Mon, 22 Mar 2021 12:30:18 -0400 Subject: [PATCH 1/2] fix(stream): allow initialValue of zero --- src/execution/execute.js | 45 ++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/execution/execute.js b/src/execution/execute.js index a33353806b..cb9cd517df 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -1032,7 +1032,26 @@ function completeAsyncIteratorValue( let containsPromise = false; const stream = getStreamValues(exeContext, fieldNodes); return new Promise((resolve) => { - function next(index, completedResults) { + function advance(index, completedResults) { + if ( + stream && + typeof stream.initialCount === 'number' && + index >= stream.initialCount + ) { + exeContext.dispatcher.addAsyncIteratorValue( + stream.label, + index, + path, + iterator, + exeContext, + fieldNodes, + info, + itemType, + ); + resolve(completedResults); + return; + } + const fieldPath = addPath(path, index, undefined); iterator.next().then( ({ value, done }) => { @@ -1067,26 +1086,7 @@ function completeAsyncIteratorValue( return; } - const newIndex = index + 1; - if ( - stream && - typeof stream.initialCount === 'number' && - newIndex >= stream.initialCount - ) { - exeContext.dispatcher.addAsyncIteratorValue( - stream.label, - newIndex, - path, - iterator, - exeContext, - fieldNodes, - info, - itemType, - ); - resolve(completedResults); - return; - } - next(newIndex, completedResults); + advance(index + 1, completedResults); }, (rawError) => { completedResults.push(null); @@ -1100,7 +1100,8 @@ function completeAsyncIteratorValue( }, ); } - next(0, []); + + advance(0, []); }).then((completedResults) => containsPromise ? Promise.all(completedResults) : completedResults, ); From 41065ae968695de5f548784706e7251899503b40 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Wed, 24 Mar 2021 18:13:54 -0400 Subject: [PATCH 2/2] add test --- src/execution/__tests__/stream-test.js | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/execution/__tests__/stream-test.js b/src/execution/__tests__/stream-test.js index 9e4796edaa..5d980e504c 100644 --- a/src/execution/__tests__/stream-test.js +++ b/src/execution/__tests__/stream-test.js @@ -351,6 +351,52 @@ describe('Execute: stream directive', () => { ]); }); it('Can stream a field that returns an async iterable', async () => { + const document = parse(` + query { + asyncIterableList @stream { + name + id + } + } + `); + const result = await complete(document); + expect(result).to.deep.equal([ + { + data: { + asyncIterableList: [], + }, + hasNext: true, + }, + { + data: { + name: 'Luke', + id: '1', + }, + path: ['asyncIterableList', 0], + hasNext: true, + }, + { + data: { + name: 'Han', + id: '2', + }, + path: ['asyncIterableList', 1], + hasNext: true, + }, + { + data: { + name: 'Leia', + id: '3', + }, + path: ['asyncIterableList', 2], + hasNext: true, + }, + { + hasNext: false, + }, + ]); + }); + it('Can stream a field that returns an async iterable, using a non-zero initialCount', async () => { const document = parse(` query { asyncIterableList @stream(initialCount: 2) {