|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { describe, it } from 'mocha';
|
3 | 3 |
|
| 4 | +import invariant from '../../jsutils/invariant'; |
4 | 5 | import isAsyncIterable from '../../jsutils/isAsyncIterable';
|
5 | 6 | import { parse } from '../../language/parser';
|
6 | 7 |
|
@@ -72,6 +73,36 @@ const query = new GraphQLObjectType({
|
72 | 73 | yield {};
|
73 | 74 | },
|
74 | 75 | },
|
| 76 | + asyncIterableListDelayed: { |
| 77 | + type: new GraphQLList(friendType), |
| 78 | + async *resolve() { |
| 79 | + for (const friend of friends) { |
| 80 | + // pause an additional ms before yielding to allow time |
| 81 | + // for tests to return or throw before next value is processed. |
| 82 | + // eslint-disable-next-line no-await-in-loop |
| 83 | + await new Promise((r) => setTimeout(r, 1)); |
| 84 | + yield friend; |
| 85 | + } |
| 86 | + }, |
| 87 | + }, |
| 88 | + asyncIterableListNoReturn: { |
| 89 | + type: new GraphQLList(friendType), |
| 90 | + resolve() { |
| 91 | + let i = 0; |
| 92 | + return { |
| 93 | + [Symbol.asyncIterator]: () => ({ |
| 94 | + async next() { |
| 95 | + const friend = friends[i++]; |
| 96 | + if (friend) { |
| 97 | + await new Promise((r) => setTimeout(r, 1)); |
| 98 | + return { value: friend, done: false }; |
| 99 | + } |
| 100 | + return { value: undefined, done: true }; |
| 101 | + }, |
| 102 | + }), |
| 103 | + }; |
| 104 | + }, |
| 105 | + }, |
75 | 106 | asyncIterableListDelayedClose: {
|
76 | 107 | type: new GraphQLList(friendType),
|
77 | 108 | async *resolve() {
|
@@ -626,4 +657,114 @@ describe('Execute: stream directive', () => {
|
626 | 657 | },
|
627 | 658 | ]);
|
628 | 659 | });
|
| 660 | + it('Returns underlying async iterables when dispatcher is returned', async () => { |
| 661 | + const document = parse(` |
| 662 | + query { |
| 663 | + asyncIterableListDelayed @stream(initialCount: 1) { |
| 664 | + name |
| 665 | + id |
| 666 | + } |
| 667 | + } |
| 668 | + `); |
| 669 | + const schema = new GraphQLSchema({ query }); |
| 670 | + |
| 671 | + const executeResult = await execute(schema, document, {}); |
| 672 | + invariant(isAsyncIterable(executeResult)); |
| 673 | + |
| 674 | + const result1 = await executeResult.next(); |
| 675 | + expect(result1).to.deep.equal({ |
| 676 | + done: false, |
| 677 | + value: { |
| 678 | + data: { |
| 679 | + asyncIterableListDelayed: [ |
| 680 | + { |
| 681 | + id: '1', |
| 682 | + name: 'Luke', |
| 683 | + }, |
| 684 | + ], |
| 685 | + }, |
| 686 | + hasNext: true, |
| 687 | + }, |
| 688 | + }); |
| 689 | + |
| 690 | + executeResult.return(); |
| 691 | + |
| 692 | + // this result had started processing before return was called |
| 693 | + const result2 = await executeResult.next(); |
| 694 | + expect(result2).to.deep.equal({ |
| 695 | + done: false, |
| 696 | + value: { |
| 697 | + data: { |
| 698 | + id: '2', |
| 699 | + name: 'Han', |
| 700 | + }, |
| 701 | + hasNext: true, |
| 702 | + path: ['asyncIterableListDelayed', 1], |
| 703 | + }, |
| 704 | + }); |
| 705 | + |
| 706 | + // third result is not returned because async iterator has returned |
| 707 | + const result3 = await executeResult.next(); |
| 708 | + expect(result3).to.deep.equal({ |
| 709 | + done: false, |
| 710 | + value: { |
| 711 | + hasNext: false, |
| 712 | + }, |
| 713 | + }); |
| 714 | + }); |
| 715 | + it('Can return async iterable when underlying iterable does not have a return method', async () => { |
| 716 | + const document = parse(` |
| 717 | + query { |
| 718 | + asyncIterableListNoReturn @stream(initialCount: 1) { |
| 719 | + name |
| 720 | + id |
| 721 | + } |
| 722 | + } |
| 723 | + `); |
| 724 | + const schema = new GraphQLSchema({ query }); |
| 725 | + |
| 726 | + const executeResult = await execute(schema, document, {}); |
| 727 | + invariant(isAsyncIterable(executeResult)); |
| 728 | + |
| 729 | + const result1 = await executeResult.next(); |
| 730 | + expect(result1).to.deep.equal({ |
| 731 | + done: false, |
| 732 | + value: { |
| 733 | + data: { |
| 734 | + asyncIterableListNoReturn: [ |
| 735 | + { |
| 736 | + id: '1', |
| 737 | + name: 'Luke', |
| 738 | + }, |
| 739 | + ], |
| 740 | + }, |
| 741 | + hasNext: true, |
| 742 | + }, |
| 743 | + }); |
| 744 | + |
| 745 | + executeResult.return(); |
| 746 | + |
| 747 | + // this result had started processing before return was called |
| 748 | + const result2 = await executeResult.next(); |
| 749 | + expect(result2).to.deep.equal({ |
| 750 | + done: false, |
| 751 | + value: { |
| 752 | + data: { |
| 753 | + id: '2', |
| 754 | + name: 'Han', |
| 755 | + }, |
| 756 | + hasNext: true, |
| 757 | + path: ['asyncIterableListNoReturn', 1], |
| 758 | + }, |
| 759 | + }); |
| 760 | + |
| 761 | + // third result is not returned because async iterator has returned |
| 762 | + const result3 = await executeResult.next(); |
| 763 | + expect(result3).to.deep.equal({ |
| 764 | + done: false, |
| 765 | + value: { |
| 766 | + hasNext: false, |
| 767 | + }, |
| 768 | + }); |
| 769 | + }); |
629 | 770 | });
|
0 commit comments