Skip to content

Commit 3278e86

Browse files
committed
Add tests confirming #319
1 parent 3ce90fa commit 3278e86

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

src/execution/__tests__/executor.js

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ describe('Execute: Handles basic execution tasks', () => {
378378
]);
379379
});
380380

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 () => {
382382
const doc = '{ a }';
383383
const data = { a: 'b' };
384384
const ast = parse(doc);
@@ -396,7 +396,7 @@ describe('Execute: Handles basic execution tasks', () => {
396396
expect(result).to.deep.equal({ data: { a: 'b' } });
397397
});
398398

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 () => {
400400
const doc = 'query Example { a }';
401401
const data = { a: 'b' };
402402
const ast = parse(doc);
@@ -414,7 +414,43 @@ describe('Execute: Handles basic execution tasks', () => {
414414
expect(result).to.deep.equal({ data: { a: 'b' } });
415415
});
416416

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', () => {
418454
const doc = 'query Example { a } query OtherExample { a }';
419455
const data = { a: 'b' };
420456
const ast = parse(doc);
@@ -432,6 +468,24 @@ describe('Execute: Handles basic execution tasks', () => {
432468
);
433469
});
434470

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+
435489
it('uses the query schema for queries', async () => {
436490
const doc = 'query Q { a } mutation M { c } subscription S { a }';
437491
const data = { a: 'b', c: 'd' };

0 commit comments

Comments
 (0)