Skip to content

Commit 2573d53

Browse files
committed
add @stream tests
1 parent 023300d commit 2573d53

File tree

3 files changed

+193
-9
lines changed

3 files changed

+193
-9
lines changed

src/__tests__/starWarsDeferredQuery-test.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { describe, it } from 'mocha';
66

77
import { graphql } from '../graphql';
88

9-
import { StarWarsSchema, StarWarsSchemaDeferEnabled } from './starWarsSchema';
9+
import {
10+
StarWarsSchema,
11+
StarWarsSchemaDeferStreamEnabled,
12+
} from './starWarsSchema';
1013

1114
describe('Star Wars Query Deferred Tests', () => {
1215
describe('Compatibility', () => {
@@ -49,7 +52,7 @@ describe('Star Wars Query Deferred Tests', () => {
4952
name
5053
}
5154
`;
52-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
55+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
5356
const { patches: patchesIterable, ...initial } = result;
5457
expect(initial).to.deep.equal({
5558
data: {
@@ -102,7 +105,7 @@ describe('Star Wars Query Deferred Tests', () => {
102105
}
103106
`;
104107

105-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
108+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
106109
const { patches: patchesIterable, ...rest } = result;
107110
expect(rest).to.deep.equal({
108111
data: {
@@ -179,7 +182,7 @@ describe('Star Wars Query Deferred Tests', () => {
179182
}
180183
}
181184
`;
182-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
185+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
183186
const { patches: patchesIterable, ...rest } = result;
184187

185188
expect(rest).to.deep.equal({
@@ -292,7 +295,7 @@ describe('Star Wars Query Deferred Tests', () => {
292295
secretBackstory
293296
}
294297
`;
295-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
298+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
296299
const { patches: patchesIterable, ...initial } = result;
297300

298301
expect(initial).to.deep.equal({
@@ -343,7 +346,7 @@ describe('Star Wars Query Deferred Tests', () => {
343346
}
344347
}
345348
`;
346-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
349+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
347350
const { patches: patchesIterable, ...initial } = result;
348351
expect(initial).to.deep.equal({
349352
data: {
@@ -426,7 +429,7 @@ describe('Star Wars Query Deferred Tests', () => {
426429
story: secretBackstory
427430
}
428431
`;
429-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
432+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
430433
const { patches: patchesIterable, ...initial } = result;
431434
expect(initial).to.deep.equal({
432435
data: {
@@ -470,7 +473,7 @@ describe('Star Wars Query Deferred Tests', () => {
470473
secretFriend
471474
}
472475
`;
473-
const result = await graphql(StarWarsSchemaDeferEnabled, query);
476+
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
474477
const { patches: patchesIterable, ...initial } = result;
475478
expect(initial).to.deep.equal({
476479
data: {

src/__tests__/starWarsSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,9 @@ export const StarWarsSchema = new GraphQLSchema({
306306
types: [humanType, droidType],
307307
});
308308

309-
export const StarWarsSchemaDeferEnabled = new GraphQLSchema({
309+
export const StarWarsSchemaDeferStreamEnabled = new GraphQLSchema({
310310
query: queryType,
311311
types: [humanType, droidType],
312312
experimentalDeferFragmentSpreads: true,
313+
experimentalStream: true,
313314
});
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
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

Comments
 (0)