|
| 1 | +// @flow strict |
| 2 | + |
| 3 | +import { forAwaitEach } from 'iterall'; |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { describe, it } from 'mocha'; |
| 6 | + |
| 7 | +import { graphql } from '../graphql'; |
| 8 | + |
| 9 | +import { |
| 10 | + StarWarsSchema, |
| 11 | + StarWarsSchemaDeferStreamEnabled, |
| 12 | +} from './starWarsSchema'; |
| 13 | + |
| 14 | +describe('Star Wars Query Stream Tests', () => { |
| 15 | + describe('Compatibility', () => { |
| 16 | + it('Can disable @stream and return would-be streamed data as part of initial result', async () => { |
| 17 | + const query = ` |
| 18 | + query HeroFriendsQuery { |
| 19 | + hero { |
| 20 | + friends @stream(initial_count: 0, label: "HeroFriends") { |
| 21 | + id |
| 22 | + name |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + `; |
| 27 | + const result = await graphql(StarWarsSchema, query); |
| 28 | + expect(result).to.deep.equal({ |
| 29 | + data: { |
| 30 | + hero: { |
| 31 | + friends: [ |
| 32 | + { |
| 33 | + id: '1000', |
| 34 | + name: 'Luke Skywalker', |
| 35 | + }, |
| 36 | + { |
| 37 | + id: '1002', |
| 38 | + name: 'Han Solo', |
| 39 | + }, |
| 40 | + { |
| 41 | + id: '1003', |
| 42 | + name: 'Leia Organa', |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + }, |
| 47 | + }); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Basic Queries', () => { |
| 52 | + it('Can @stream an array field', async () => { |
| 53 | + const query = ` |
| 54 | + query HeroFriendsQuery { |
| 55 | + hero { |
| 56 | + friends @stream(initial_count: 2, label: "HeroFriends") { |
| 57 | + id |
| 58 | + name |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + `; |
| 63 | + const result = await graphql(StarWarsSchemaDeferStreamEnabled, query); |
| 64 | + const { patches: patchesIterable, ...initial } = result; |
| 65 | + expect(initial).to.deep.equal({ |
| 66 | + data: { |
| 67 | + hero: { |
| 68 | + friends: [ |
| 69 | + { |
| 70 | + id: '1000', |
| 71 | + name: 'Luke Skywalker', |
| 72 | + }, |
| 73 | + { |
| 74 | + id: '1002', |
| 75 | + name: 'Han Solo', |
| 76 | + }, |
| 77 | + ], |
| 78 | + }, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const patches = []; |
| 83 | + |
| 84 | + if (patchesIterable) { |
| 85 | + await forAwaitEach(patchesIterable, patch => { |
| 86 | + patches.push(patch); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + expect(patches).to.have.lengthOf(1); |
| 91 | + expect(patches[0]).to.deep.equal({ |
| 92 | + label: 'HeroFriends', |
| 93 | + path: ['hero', 'friends', 2], |
| 94 | + data: { |
| 95 | + id: '1003', |
| 96 | + name: 'Leia Organa', |
| 97 | + }, |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('Can @stream multiple selections on the same field', async () => { |
| 103 | + const query = ` |
| 104 | + query HeroFriendsQuery { |
| 105 | + hero { |
| 106 | + friends { |
| 107 | + id |
| 108 | + } |
| 109 | + ...FriendsName |
| 110 | + ...FriendsAppearsIn |
| 111 | + } |
| 112 | + } |
| 113 | + fragment FriendsName on Character { |
| 114 | + friends @stream(label: "nameLabel", initial_count: 1) { |
| 115 | + name |
| 116 | + } |
| 117 | + } |
| 118 | + fragment FriendsAppearsIn on Character { |
| 119 | + friends @stream(label: "appearsInLabel", initial_count: 2) { |
| 120 | + appearsIn |
| 121 | + } |
| 122 | + } |
| 123 | + `; |
| 124 | + const result = await graphql(StarWarsSchemaDeferStreamEnabled, query); |
| 125 | + const { patches: patchesIterable, ...initial } = result; |
| 126 | + expect(initial).to.deep.equal({ |
| 127 | + data: { |
| 128 | + hero: { |
| 129 | + friends: [ |
| 130 | + { |
| 131 | + id: '1000', |
| 132 | + appearsIn: ['NEWHOPE', 'EMPIRE', 'JEDI'], |
| 133 | + name: 'Luke Skywalker', |
| 134 | + }, |
| 135 | + { |
| 136 | + id: '1002', |
| 137 | + appearsIn: ['NEWHOPE', 'EMPIRE', 'JEDI'], |
| 138 | + }, |
| 139 | + { |
| 140 | + id: '1003', |
| 141 | + }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + }, |
| 145 | + }); |
| 146 | + |
| 147 | + const patches = []; |
| 148 | + |
| 149 | + if (patchesIterable) { |
| 150 | + await forAwaitEach(patchesIterable, patch => { |
| 151 | + patches.push(patch); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + expect(patches).to.have.lengthOf(3); |
| 156 | + expect(patches[0]).to.deep.equal({ |
| 157 | + data: { |
| 158 | + name: 'Han Solo', |
| 159 | + }, |
| 160 | + path: ['hero', 'friends', 1], |
| 161 | + label: 'nameLabel', |
| 162 | + }); |
| 163 | + |
| 164 | + expect(patches[1]).to.deep.equal({ |
| 165 | + data: { |
| 166 | + name: 'Leia Organa', |
| 167 | + }, |
| 168 | + path: ['hero', 'friends', 2], |
| 169 | + label: 'nameLabel', |
| 170 | + }); |
| 171 | + |
| 172 | + expect(patches[2]).to.deep.equal({ |
| 173 | + data: { |
| 174 | + appearsIn: ['NEWHOPE', 'EMPIRE', 'JEDI'], |
| 175 | + }, |
| 176 | + path: ['hero', 'friends', 2], |
| 177 | + label: 'appearsInLabel', |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments