@@ -378,7 +378,7 @@ describe('Execute: Handles basic execution tasks', () => {
378
378
] ) ;
379
379
} ) ;
380
380
381
- it ( 'uses the inline operation if no operation is provided' , async ( ) => {
381
+ it ( 'uses the inline operation if no operation name is provided' , async ( ) => {
382
382
const doc = '{ a }' ;
383
383
const data = { a : 'b' } ;
384
384
const ast = parse ( doc ) ;
@@ -396,7 +396,7 @@ describe('Execute: Handles basic execution tasks', () => {
396
396
expect ( result ) . to . deep . equal ( { data : { a : 'b' } } ) ;
397
397
} ) ;
398
398
399
- it ( 'uses the only operation if no operation is provided' , async ( ) => {
399
+ it ( 'uses the only operation if no operation name is provided' , async ( ) => {
400
400
const doc = 'query Example { a }' ;
401
401
const data = { a : 'b' } ;
402
402
const ast = parse ( doc ) ;
@@ -414,7 +414,43 @@ describe('Execute: Handles basic execution tasks', () => {
414
414
expect ( result ) . to . deep . equal ( { data : { a : 'b' } } ) ;
415
415
} ) ;
416
416
417
- it ( 'throws if no operation is provided with multiple operations' , ( ) => {
417
+ it ( 'uses the named operation if operation name is provided' , async ( ) => {
418
+ const doc = 'query Example { first: a } query OtherExample { second: a }' ;
419
+ const data = { a : 'b' } ;
420
+ const ast = parse ( doc ) ;
421
+ const schema = new GraphQLSchema ( {
422
+ query : new GraphQLObjectType ( {
423
+ name : 'Type' ,
424
+ fields : {
425
+ a : { type : GraphQLString } ,
426
+ }
427
+ } )
428
+ } ) ;
429
+
430
+ const result = await execute ( schema , ast , data , null , 'OtherExample' ) ;
431
+
432
+ expect ( result ) . to . deep . equal ( { data : { second : 'b' } } ) ;
433
+ } ) ;
434
+
435
+ it ( 'throws if no operation is provided' , ( ) => {
436
+ const doc = 'fragment Example on Type { a }' ;
437
+ const data = { a : 'b' } ;
438
+ const ast = parse ( doc ) ;
439
+ const schema = new GraphQLSchema ( {
440
+ query : new GraphQLObjectType ( {
441
+ name : 'Type' ,
442
+ fields : {
443
+ a : { type : GraphQLString } ,
444
+ }
445
+ } )
446
+ } ) ;
447
+
448
+ expect ( ( ) => execute ( schema , ast , data ) ) . to . throw (
449
+ 'Must provide an operation.'
450
+ ) ;
451
+ } ) ;
452
+
453
+ it ( 'throws if no operation name is provided with multiple operations' , ( ) => {
418
454
const doc = 'query Example { a } query OtherExample { a }' ;
419
455
const data = { a : 'b' } ;
420
456
const ast = parse ( doc ) ;
@@ -432,6 +468,24 @@ describe('Execute: Handles basic execution tasks', () => {
432
468
) ;
433
469
} ) ;
434
470
471
+ it ( 'throws if unknown operation name is provided' , ( ) => {
472
+ const doc = 'query Example { a } query OtherExample { a }' ;
473
+ const data = { a : 'b' } ;
474
+ const ast = parse ( doc ) ;
475
+ const schema = new GraphQLSchema ( {
476
+ query : new GraphQLObjectType ( {
477
+ name : 'Type' ,
478
+ fields : {
479
+ a : { type : GraphQLString } ,
480
+ }
481
+ } )
482
+ } ) ;
483
+
484
+ expect ( ( ) => execute ( schema , ast , data , null , 'UnknownExample' ) ) . to . throw (
485
+ 'Unknown operation named "UnknownExample".'
486
+ ) ;
487
+ } ) ;
488
+
435
489
it ( 'uses the query schema for queries' , async ( ) => {
436
490
const doc = 'query Q { a } mutation M { c } subscription S { a }' ;
437
491
const data = { a : 'b' , c : 'd' } ;
0 commit comments