@@ -5,19 +5,20 @@ import { describe, it } from 'mocha';
5
5
6
6
import { graphql } from '../graphql' ;
7
7
8
- import { StarWarsSchema } from './starWarsSchema' ;
8
+ import { StarWarsSchema as schema } from './starWarsSchema' ;
9
9
10
10
describe ( 'Star Wars Query Tests' , ( ) => {
11
11
describe ( 'Basic Queries' , ( ) => {
12
12
it ( 'Correctly identifies R2-D2 as the hero of the Star Wars Saga' , async ( ) => {
13
- const query = `
13
+ const source = `
14
14
query HeroNameQuery {
15
15
hero {
16
16
name
17
17
}
18
18
}
19
19
` ;
20
- const result = await graphql ( StarWarsSchema , query ) ;
20
+
21
+ const result = await graphql ( { schema, source } ) ;
21
22
expect ( result ) . to . deep . equal ( {
22
23
data : {
23
24
hero : {
@@ -27,18 +28,16 @@ describe('Star Wars Query Tests', () => {
27
28
} ) ;
28
29
} ) ;
29
30
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 = `
32
33
query HeroNameQuery {
33
34
hero {
34
35
name
35
36
}
36
37
}
37
38
` ;
38
- const result = await graphql ( {
39
- schema : StarWarsSchema ,
40
- source : query ,
41
- } ) ;
39
+
40
+ const result = await graphql ( schema , source ) ;
42
41
expect ( result ) . to . deep . equal ( {
43
42
data : {
44
43
hero : {
@@ -49,7 +48,7 @@ describe('Star Wars Query Tests', () => {
49
48
} ) ;
50
49
51
50
it ( 'Allows us to query for the ID and friends of R2-D2' , async ( ) => {
52
- const query = `
51
+ const source = `
53
52
query HeroNameAndFriendsQuery {
54
53
hero {
55
54
id
@@ -60,7 +59,8 @@ describe('Star Wars Query Tests', () => {
60
59
}
61
60
}
62
61
` ;
63
- const result = await graphql ( StarWarsSchema , query ) ;
62
+
63
+ const result = await graphql ( { schema, source } ) ;
64
64
expect ( result ) . to . deep . equal ( {
65
65
data : {
66
66
hero : {
@@ -85,7 +85,7 @@ describe('Star Wars Query Tests', () => {
85
85
86
86
describe ( 'Nested Queries' , ( ) => {
87
87
it ( 'Allows us to query for the friends of friends of R2-D2' , async ( ) => {
88
- const query = `
88
+ const source = `
89
89
query NestedQuery {
90
90
hero {
91
91
name
@@ -99,7 +99,8 @@ describe('Star Wars Query Tests', () => {
99
99
}
100
100
}
101
101
` ;
102
- const result = await graphql ( StarWarsSchema , query ) ;
102
+
103
+ const result = await graphql ( { schema, source } ) ;
103
104
expect ( result ) . to . deep . equal ( {
104
105
data : {
105
106
hero : {
@@ -165,14 +166,15 @@ describe('Star Wars Query Tests', () => {
165
166
166
167
describe ( 'Using IDs and query parameters to refetch objects' , ( ) => {
167
168
it ( 'Allows us to query for Luke Skywalker directly, using his ID' , async ( ) => {
168
- const query = `
169
+ const source = `
169
170
query FetchLukeQuery {
170
171
human(id: "1000") {
171
172
name
172
173
}
173
174
}
174
175
` ;
175
- const result = await graphql ( StarWarsSchema , query ) ;
176
+
177
+ const result = await graphql ( { schema, source } ) ;
176
178
expect ( result ) . to . deep . equal ( {
177
179
data : {
178
180
human : {
@@ -183,15 +185,16 @@ describe('Star Wars Query Tests', () => {
183
185
} ) ;
184
186
185
187
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 = `
187
189
query FetchSomeIDQuery($someId: String!) {
188
190
human(id: $someId) {
189
191
name
190
192
}
191
193
}
192
194
` ;
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 } ) ;
195
198
expect ( result ) . to . deep . equal ( {
196
199
data : {
197
200
human : {
@@ -202,15 +205,16 @@ describe('Star Wars Query Tests', () => {
202
205
} ) ;
203
206
204
207
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 = `
206
209
query FetchSomeIDQuery($someId: String!) {
207
210
human(id: $someId) {
208
211
name
209
212
}
210
213
}
211
214
` ;
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 } ) ;
214
218
expect ( result ) . to . deep . equal ( {
215
219
data : {
216
220
human : {
@@ -221,15 +225,16 @@ describe('Star Wars Query Tests', () => {
221
225
} ) ;
222
226
223
227
it ( 'Allows us to create a generic query, then pass an invalid ID to get null back' , async ( ) => {
224
- const query = `
228
+ const source = `
225
229
query humanQuery($id: String!) {
226
230
human(id: $id) {
227
231
name
228
232
}
229
233
}
230
234
` ;
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 } ) ;
233
238
expect ( result ) . to . deep . equal ( {
234
239
data : {
235
240
human : null ,
@@ -240,14 +245,15 @@ describe('Star Wars Query Tests', () => {
240
245
241
246
describe ( 'Using aliases to change the key in the response' , ( ) => {
242
247
it ( 'Allows us to query for Luke, changing his key with an alias' , async ( ) => {
243
- const query = `
248
+ const source = `
244
249
query FetchLukeAliased {
245
250
luke: human(id: "1000") {
246
251
name
247
252
}
248
253
}
249
254
` ;
250
- const result = await graphql ( StarWarsSchema , query ) ;
255
+
256
+ const result = await graphql ( { schema, source } ) ;
251
257
expect ( result ) . to . deep . equal ( {
252
258
data : {
253
259
luke : {
@@ -258,7 +264,7 @@ describe('Star Wars Query Tests', () => {
258
264
} ) ;
259
265
260
266
it ( 'Allows us to query for both Luke and Leia, using two root fields and an alias' , async ( ) => {
261
- const query = `
267
+ const source = `
262
268
query FetchLukeAndLeiaAliased {
263
269
luke: human(id: "1000") {
264
270
name
@@ -268,7 +274,8 @@ describe('Star Wars Query Tests', () => {
268
274
}
269
275
}
270
276
` ;
271
- const result = await graphql ( StarWarsSchema , query ) ;
277
+
278
+ const result = await graphql ( { schema, source } ) ;
272
279
expect ( result ) . to . deep . equal ( {
273
280
data : {
274
281
luke : {
@@ -284,7 +291,7 @@ describe('Star Wars Query Tests', () => {
284
291
285
292
describe ( 'Uses fragments to express more complex queries' , ( ) => {
286
293
it ( 'Allows us to query using duplicated content' , async ( ) => {
287
- const query = `
294
+ const source = `
288
295
query DuplicateFields {
289
296
luke: human(id: "1000") {
290
297
name
@@ -296,7 +303,8 @@ describe('Star Wars Query Tests', () => {
296
303
}
297
304
}
298
305
` ;
299
- const result = await graphql ( StarWarsSchema , query ) ;
306
+
307
+ const result = await graphql ( { schema, source } ) ;
300
308
expect ( result ) . to . deep . equal ( {
301
309
data : {
302
310
luke : {
@@ -312,7 +320,7 @@ describe('Star Wars Query Tests', () => {
312
320
} ) ;
313
321
314
322
it ( 'Allows us to use a fragment to avoid duplicating content' , async ( ) => {
315
- const query = `
323
+ const source = `
316
324
query UseFragment {
317
325
luke: human(id: "1000") {
318
326
...HumanFragment
@@ -327,7 +335,8 @@ describe('Star Wars Query Tests', () => {
327
335
homePlanet
328
336
}
329
337
` ;
330
- const result = await graphql ( StarWarsSchema , query ) ;
338
+
339
+ const result = await graphql ( { schema, source } ) ;
331
340
expect ( result ) . to . deep . equal ( {
332
341
data : {
333
342
luke : {
@@ -345,15 +354,16 @@ describe('Star Wars Query Tests', () => {
345
354
346
355
describe ( 'Using __typename to find the type of an object' , ( ) => {
347
356
it ( 'Allows us to verify that R2-D2 is a droid' , async ( ) => {
348
- const query = `
357
+ const source = `
349
358
query CheckTypeOfR2 {
350
359
hero {
351
360
__typename
352
361
name
353
362
}
354
363
}
355
364
` ;
356
- const result = await graphql ( StarWarsSchema , query ) ;
365
+
366
+ const result = await graphql ( { schema, source } ) ;
357
367
expect ( result ) . to . deep . equal ( {
358
368
data : {
359
369
hero : {
@@ -365,15 +375,16 @@ describe('Star Wars Query Tests', () => {
365
375
} ) ;
366
376
367
377
it ( 'Allows us to verify that Luke is a human' , async ( ) => {
368
- const query = `
378
+ const source = `
369
379
query CheckTypeOfLuke {
370
380
hero(episode: EMPIRE) {
371
381
__typename
372
382
name
373
383
}
374
384
}
375
385
` ;
376
- const result = await graphql ( StarWarsSchema , query ) ;
386
+
387
+ const result = await graphql ( { schema, source } ) ;
377
388
expect ( result ) . to . deep . equal ( {
378
389
data : {
379
390
hero : {
@@ -387,15 +398,16 @@ describe('Star Wars Query Tests', () => {
387
398
388
399
describe ( 'Reporting errors raised in resolvers' , ( ) => {
389
400
it ( 'Correctly reports error on accessing secretBackstory' , async ( ) => {
390
- const query = `
401
+ const source = `
391
402
query HeroNameQuery {
392
403
hero {
393
404
name
394
405
secretBackstory
395
406
}
396
407
}
397
408
` ;
398
- const result = await graphql ( StarWarsSchema , query ) ;
409
+
410
+ const result = await graphql ( { schema, source } ) ;
399
411
expect ( result ) . to . deep . equal ( {
400
412
data : {
401
413
hero : {
@@ -414,7 +426,7 @@ describe('Star Wars Query Tests', () => {
414
426
} ) ;
415
427
416
428
it ( 'Correctly reports error on accessing secretBackstory in a list' , async ( ) => {
417
- const query = `
429
+ const source = `
418
430
query HeroNameQuery {
419
431
hero {
420
432
name
@@ -425,7 +437,8 @@ describe('Star Wars Query Tests', () => {
425
437
}
426
438
}
427
439
` ;
428
- const result = await graphql ( StarWarsSchema , query ) ;
440
+
441
+ const result = await graphql ( { schema, source } ) ;
429
442
expect ( result ) . to . deep . equal ( {
430
443
data : {
431
444
hero : {
@@ -467,15 +480,16 @@ describe('Star Wars Query Tests', () => {
467
480
} ) ;
468
481
469
482
it ( 'Correctly reports error on accessing through an alias' , async ( ) => {
470
- const query = `
483
+ const source = `
471
484
query HeroNameQuery {
472
485
mainHero: hero {
473
486
name
474
487
story: secretBackstory
475
488
}
476
489
}
477
490
` ;
478
- const result = await graphql ( StarWarsSchema , query ) ;
491
+
492
+ const result = await graphql ( { schema, source } ) ;
479
493
expect ( result ) . to . deep . equal ( {
480
494
data : {
481
495
mainHero : {
0 commit comments