Skip to content

Commit 9063ade

Browse files
test: Switch to using named arguments to graphql/execute (#2288)
1 parent 7895fba commit 9063ade

15 files changed

+206
-184
lines changed

src/__tests__/starWarsQuery-test.js

+56-42
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ import { describe, it } from 'mocha';
55

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

8-
import { StarWarsSchema } from './starWarsSchema';
8+
import { StarWarsSchema as schema } from './starWarsSchema';
99

1010
describe('Star Wars Query Tests', () => {
1111
describe('Basic Queries', () => {
1212
it('Correctly identifies R2-D2 as the hero of the Star Wars Saga', async () => {
13-
const query = `
13+
const source = `
1414
query HeroNameQuery {
1515
hero {
1616
name
1717
}
1818
}
1919
`;
20-
const result = await graphql(StarWarsSchema, query);
20+
21+
const result = await graphql({ schema, source });
2122
expect(result).to.deep.equal({
2223
data: {
2324
hero: {
@@ -27,18 +28,16 @@ describe('Star Wars Query Tests', () => {
2728
});
2829
});
2930

30-
it('Accepts an object with named properties to graphql()', async () => {
31-
const query = `
31+
it('Accepts positional arguments to graphql()', async () => {
32+
const source = `
3233
query HeroNameQuery {
3334
hero {
3435
name
3536
}
3637
}
3738
`;
38-
const result = await graphql({
39-
schema: StarWarsSchema,
40-
source: query,
41-
});
39+
40+
const result = await graphql(schema, source);
4241
expect(result).to.deep.equal({
4342
data: {
4443
hero: {
@@ -49,7 +48,7 @@ describe('Star Wars Query Tests', () => {
4948
});
5049

5150
it('Allows us to query for the ID and friends of R2-D2', async () => {
52-
const query = `
51+
const source = `
5352
query HeroNameAndFriendsQuery {
5453
hero {
5554
id
@@ -60,7 +59,8 @@ describe('Star Wars Query Tests', () => {
6059
}
6160
}
6261
`;
63-
const result = await graphql(StarWarsSchema, query);
62+
63+
const result = await graphql({ schema, source });
6464
expect(result).to.deep.equal({
6565
data: {
6666
hero: {
@@ -85,7 +85,7 @@ describe('Star Wars Query Tests', () => {
8585

8686
describe('Nested Queries', () => {
8787
it('Allows us to query for the friends of friends of R2-D2', async () => {
88-
const query = `
88+
const source = `
8989
query NestedQuery {
9090
hero {
9191
name
@@ -99,7 +99,8 @@ describe('Star Wars Query Tests', () => {
9999
}
100100
}
101101
`;
102-
const result = await graphql(StarWarsSchema, query);
102+
103+
const result = await graphql({ schema, source });
103104
expect(result).to.deep.equal({
104105
data: {
105106
hero: {
@@ -165,14 +166,15 @@ describe('Star Wars Query Tests', () => {
165166

166167
describe('Using IDs and query parameters to refetch objects', () => {
167168
it('Allows us to query for Luke Skywalker directly, using his ID', async () => {
168-
const query = `
169+
const source = `
169170
query FetchLukeQuery {
170171
human(id: "1000") {
171172
name
172173
}
173174
}
174175
`;
175-
const result = await graphql(StarWarsSchema, query);
176+
177+
const result = await graphql({ schema, source });
176178
expect(result).to.deep.equal({
177179
data: {
178180
human: {
@@ -183,15 +185,16 @@ describe('Star Wars Query Tests', () => {
183185
});
184186

185187
it('Allows us to create a generic query, then use it to fetch Luke Skywalker using his ID', async () => {
186-
const query = `
188+
const source = `
187189
query FetchSomeIDQuery($someId: String!) {
188190
human(id: $someId) {
189191
name
190192
}
191193
}
192194
`;
193-
const params = { someId: '1000' };
194-
const result = await graphql(StarWarsSchema, query, null, null, params);
195+
const variableValues = { someId: '1000' };
196+
197+
const result = await graphql({ schema, source, variableValues });
195198
expect(result).to.deep.equal({
196199
data: {
197200
human: {
@@ -202,15 +205,16 @@ describe('Star Wars Query Tests', () => {
202205
});
203206

204207
it('Allows us to create a generic query, then use it to fetch Han Solo using his ID', async () => {
205-
const query = `
208+
const source = `
206209
query FetchSomeIDQuery($someId: String!) {
207210
human(id: $someId) {
208211
name
209212
}
210213
}
211214
`;
212-
const params = { someId: '1002' };
213-
const result = await graphql(StarWarsSchema, query, null, null, params);
215+
const variableValues = { someId: '1002' };
216+
217+
const result = await graphql({ schema, source, variableValues });
214218
expect(result).to.deep.equal({
215219
data: {
216220
human: {
@@ -221,15 +225,16 @@ describe('Star Wars Query Tests', () => {
221225
});
222226

223227
it('Allows us to create a generic query, then pass an invalid ID to get null back', async () => {
224-
const query = `
228+
const source = `
225229
query humanQuery($id: String!) {
226230
human(id: $id) {
227231
name
228232
}
229233
}
230234
`;
231-
const params = { id: 'not a valid id' };
232-
const result = await graphql(StarWarsSchema, query, null, null, params);
235+
const variableValues = { id: 'not a valid id' };
236+
237+
const result = await graphql({ schema, source, variableValues });
233238
expect(result).to.deep.equal({
234239
data: {
235240
human: null,
@@ -240,14 +245,15 @@ describe('Star Wars Query Tests', () => {
240245

241246
describe('Using aliases to change the key in the response', () => {
242247
it('Allows us to query for Luke, changing his key with an alias', async () => {
243-
const query = `
248+
const source = `
244249
query FetchLukeAliased {
245250
luke: human(id: "1000") {
246251
name
247252
}
248253
}
249254
`;
250-
const result = await graphql(StarWarsSchema, query);
255+
256+
const result = await graphql({ schema, source });
251257
expect(result).to.deep.equal({
252258
data: {
253259
luke: {
@@ -258,7 +264,7 @@ describe('Star Wars Query Tests', () => {
258264
});
259265

260266
it('Allows us to query for both Luke and Leia, using two root fields and an alias', async () => {
261-
const query = `
267+
const source = `
262268
query FetchLukeAndLeiaAliased {
263269
luke: human(id: "1000") {
264270
name
@@ -268,7 +274,8 @@ describe('Star Wars Query Tests', () => {
268274
}
269275
}
270276
`;
271-
const result = await graphql(StarWarsSchema, query);
277+
278+
const result = await graphql({ schema, source });
272279
expect(result).to.deep.equal({
273280
data: {
274281
luke: {
@@ -284,7 +291,7 @@ describe('Star Wars Query Tests', () => {
284291

285292
describe('Uses fragments to express more complex queries', () => {
286293
it('Allows us to query using duplicated content', async () => {
287-
const query = `
294+
const source = `
288295
query DuplicateFields {
289296
luke: human(id: "1000") {
290297
name
@@ -296,7 +303,8 @@ describe('Star Wars Query Tests', () => {
296303
}
297304
}
298305
`;
299-
const result = await graphql(StarWarsSchema, query);
306+
307+
const result = await graphql({ schema, source });
300308
expect(result).to.deep.equal({
301309
data: {
302310
luke: {
@@ -312,7 +320,7 @@ describe('Star Wars Query Tests', () => {
312320
});
313321

314322
it('Allows us to use a fragment to avoid duplicating content', async () => {
315-
const query = `
323+
const source = `
316324
query UseFragment {
317325
luke: human(id: "1000") {
318326
...HumanFragment
@@ -327,7 +335,8 @@ describe('Star Wars Query Tests', () => {
327335
homePlanet
328336
}
329337
`;
330-
const result = await graphql(StarWarsSchema, query);
338+
339+
const result = await graphql({ schema, source });
331340
expect(result).to.deep.equal({
332341
data: {
333342
luke: {
@@ -345,15 +354,16 @@ describe('Star Wars Query Tests', () => {
345354

346355
describe('Using __typename to find the type of an object', () => {
347356
it('Allows us to verify that R2-D2 is a droid', async () => {
348-
const query = `
357+
const source = `
349358
query CheckTypeOfR2 {
350359
hero {
351360
__typename
352361
name
353362
}
354363
}
355364
`;
356-
const result = await graphql(StarWarsSchema, query);
365+
366+
const result = await graphql({ schema, source });
357367
expect(result).to.deep.equal({
358368
data: {
359369
hero: {
@@ -365,15 +375,16 @@ describe('Star Wars Query Tests', () => {
365375
});
366376

367377
it('Allows us to verify that Luke is a human', async () => {
368-
const query = `
378+
const source = `
369379
query CheckTypeOfLuke {
370380
hero(episode: EMPIRE) {
371381
__typename
372382
name
373383
}
374384
}
375385
`;
376-
const result = await graphql(StarWarsSchema, query);
386+
387+
const result = await graphql({ schema, source });
377388
expect(result).to.deep.equal({
378389
data: {
379390
hero: {
@@ -387,15 +398,16 @@ describe('Star Wars Query Tests', () => {
387398

388399
describe('Reporting errors raised in resolvers', () => {
389400
it('Correctly reports error on accessing secretBackstory', async () => {
390-
const query = `
401+
const source = `
391402
query HeroNameQuery {
392403
hero {
393404
name
394405
secretBackstory
395406
}
396407
}
397408
`;
398-
const result = await graphql(StarWarsSchema, query);
409+
410+
const result = await graphql({ schema, source });
399411
expect(result).to.deep.equal({
400412
data: {
401413
hero: {
@@ -414,7 +426,7 @@ describe('Star Wars Query Tests', () => {
414426
});
415427

416428
it('Correctly reports error on accessing secretBackstory in a list', async () => {
417-
const query = `
429+
const source = `
418430
query HeroNameQuery {
419431
hero {
420432
name
@@ -425,7 +437,8 @@ describe('Star Wars Query Tests', () => {
425437
}
426438
}
427439
`;
428-
const result = await graphql(StarWarsSchema, query);
440+
441+
const result = await graphql({ schema, source });
429442
expect(result).to.deep.equal({
430443
data: {
431444
hero: {
@@ -467,15 +480,16 @@ describe('Star Wars Query Tests', () => {
467480
});
468481

469482
it('Correctly reports error on accessing through an alias', async () => {
470-
const query = `
483+
const source = `
471484
query HeroNameQuery {
472485
mainHero: hero {
473486
name
474487
story: secretBackstory
475488
}
476489
}
477490
`;
478-
const result = await graphql(StarWarsSchema, query);
491+
492+
const result = await graphql({ schema, source });
479493
expect(result).to.deep.equal({
480494
data: {
481495
mainHero: {

0 commit comments

Comments
 (0)