|
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() {
|
@@ -649,4 +680,114 @@ describe('Execute: stream directive', () => {
|
649 | 680 | },
|
650 | 681 | ]);
|
651 | 682 | });
|
| 683 | + it('Returns underlying async iterables when dispatcher is returned', async () => { |
| 684 | + const document = parse(` |
| 685 | + query { |
| 686 | + asyncIterableListDelayed @stream(initialCount: 1) { |
| 687 | + name |
| 688 | + id |
| 689 | + } |
| 690 | + } |
| 691 | + `); |
| 692 | + const schema = new GraphQLSchema({ query }); |
| 693 | + |
| 694 | + const executeResult = await execute(schema, document, {}); |
| 695 | + invariant(isAsyncIterable(executeResult)); |
| 696 | + |
| 697 | + const result1 = await executeResult.next(); |
| 698 | + expect(result1).to.deep.equal({ |
| 699 | + done: false, |
| 700 | + value: { |
| 701 | + data: { |
| 702 | + asyncIterableListDelayed: [ |
| 703 | + { |
| 704 | + id: '1', |
| 705 | + name: 'Luke', |
| 706 | + }, |
| 707 | + ], |
| 708 | + }, |
| 709 | + hasNext: true, |
| 710 | + }, |
| 711 | + }); |
| 712 | + |
| 713 | + executeResult.return(); |
| 714 | + |
| 715 | + // this result had started processing before return was called |
| 716 | + const result2 = await executeResult.next(); |
| 717 | + expect(result2).to.deep.equal({ |
| 718 | + done: false, |
| 719 | + value: { |
| 720 | + data: { |
| 721 | + id: '2', |
| 722 | + name: 'Han', |
| 723 | + }, |
| 724 | + hasNext: true, |
| 725 | + path: ['asyncIterableListDelayed', 1], |
| 726 | + }, |
| 727 | + }); |
| 728 | + |
| 729 | + // third result is not returned because async iterator has returned |
| 730 | + const result3 = await executeResult.next(); |
| 731 | + expect(result3).to.deep.equal({ |
| 732 | + done: false, |
| 733 | + value: { |
| 734 | + hasNext: false, |
| 735 | + }, |
| 736 | + }); |
| 737 | + }); |
| 738 | + it('Can return async iterable when underlying iterable does not have a return method', async () => { |
| 739 | + const document = parse(` |
| 740 | + query { |
| 741 | + asyncIterableListNoReturn @stream(initialCount: 1) { |
| 742 | + name |
| 743 | + id |
| 744 | + } |
| 745 | + } |
| 746 | + `); |
| 747 | + const schema = new GraphQLSchema({ query }); |
| 748 | + |
| 749 | + const executeResult = await execute(schema, document, {}); |
| 750 | + invariant(isAsyncIterable(executeResult)); |
| 751 | + |
| 752 | + const result1 = await executeResult.next(); |
| 753 | + expect(result1).to.deep.equal({ |
| 754 | + done: false, |
| 755 | + value: { |
| 756 | + data: { |
| 757 | + asyncIterableListNoReturn: [ |
| 758 | + { |
| 759 | + id: '1', |
| 760 | + name: 'Luke', |
| 761 | + }, |
| 762 | + ], |
| 763 | + }, |
| 764 | + hasNext: true, |
| 765 | + }, |
| 766 | + }); |
| 767 | + |
| 768 | + executeResult.return(); |
| 769 | + |
| 770 | + // this result had started processing before return was called |
| 771 | + const result2 = await executeResult.next(); |
| 772 | + expect(result2).to.deep.equal({ |
| 773 | + done: false, |
| 774 | + value: { |
| 775 | + data: { |
| 776 | + id: '2', |
| 777 | + name: 'Han', |
| 778 | + }, |
| 779 | + hasNext: true, |
| 780 | + path: ['asyncIterableListNoReturn', 1], |
| 781 | + }, |
| 782 | + }); |
| 783 | + |
| 784 | + // third result is not returned because async iterator has returned |
| 785 | + const result3 = await executeResult.next(); |
| 786 | + expect(result3).to.deep.equal({ |
| 787 | + done: false, |
| 788 | + value: { |
| 789 | + hasNext: false, |
| 790 | + }, |
| 791 | + }); |
| 792 | + }); |
652 | 793 | });
|
0 commit comments