Skip to content

Commit 62619df

Browse files
committed
Refactor executeOperation
This ensures failures related to buildExecutionContext() yield a GraphQL Result with errors rather than a thrown error from execute(), and handles top level error catching and nulling within executeOperation() instead of execute() for better alignment to spec text. This helps align to the idea that internal errors throw and GraphQL user errors are returned within the GraphQL Result.
1 parent 1c4477c commit 62619df

File tree

6 files changed

+332
-255
lines changed

6 files changed

+332
-255
lines changed

src/execution/__tests__/executor-test.js

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ describe('Execute: Handles basic execution tasks', () => {
612612
expect(result).to.deep.equal({ data: { second: 'b' } });
613613
});
614614

615-
it('throws if no operation is provided', () => {
615+
it('provides error if no operation is provided', async () => {
616616
const doc = 'fragment Example on Type { a }';
617617
const data = { a: 'b' };
618618
const ast = parse(doc);
@@ -625,12 +625,19 @@ describe('Execute: Handles basic execution tasks', () => {
625625
})
626626
});
627627

628-
expect(() => execute(schema, ast, data)).to.throw(
629-
'Must provide an operation.'
630-
);
628+
const result = await execute(schema, ast, data);
629+
expect(result).to.deep.equal({
630+
errors: [
631+
{
632+
message: 'Must provide an operation.',
633+
locations: undefined,
634+
path: undefined,
635+
}
636+
]
637+
});
631638
});
632639

633-
it('throws if no operation name is provided with multiple operations', () => {
640+
it('throws if no op name is provided with multiple operations', async () => {
634641
const doc = 'query Example { a } query OtherExample { a }';
635642
const data = { a: 'b' };
636643
const ast = parse(doc);
@@ -643,14 +650,21 @@ describe('Execute: Handles basic execution tasks', () => {
643650
})
644651
});
645652

646-
expect(() => execute(schema, ast, data)).to.throw(
647-
'Must provide operation name if query contains multiple operations.'
648-
);
653+
const result = await execute(schema, ast, data);
654+
expect(result).to.deep.equal({
655+
errors: [
656+
{
657+
message: 'Must provide operation name if query contains ' +
658+
'multiple operations.',
659+
locations: undefined,
660+
path: undefined,
661+
}
662+
]
663+
});
649664
});
650665

651-
it('throws if unknown operation name is provided', () => {
666+
it('throws if unknown operation name is provided', async () => {
652667
const doc = 'query Example { a } query OtherExample { a }';
653-
const data = { a: 'b' };
654668
const ast = parse(doc);
655669
const schema = new GraphQLSchema({
656670
query: new GraphQLObjectType({
@@ -661,11 +675,20 @@ describe('Execute: Handles basic execution tasks', () => {
661675
})
662676
});
663677

664-
expect(() =>
665-
execute(schema, ast, data, null, null, 'UnknownExample')
666-
).to.throw(
667-
'Unknown operation named "UnknownExample".'
668-
);
678+
const result = await execute({
679+
schema,
680+
document: ast,
681+
operationName: 'UnknownExample'
682+
});
683+
expect(result).to.deep.equal({
684+
errors: [
685+
{
686+
message: 'Unknown operation named "UnknownExample".',
687+
locations: undefined,
688+
path: undefined,
689+
}
690+
]
691+
});
669692
});
670693

671694
it('uses the query schema for queries', async () => {
@@ -960,17 +983,16 @@ describe('Execute: Handles basic execution tasks', () => {
960983
})
961984
});
962985

963-
let caughtError;
964-
try {
965-
await execute(schema, query);
966-
} catch (error) {
967-
caughtError = error;
968-
}
969-
970-
expect(caughtError).to.jsonEqual({
971-
message:
972-
'GraphQL cannot execute a request containing a ObjectTypeDefinition.',
973-
locations: [ { line: 4, column: 7 } ]
986+
const result = await execute(schema, query);
987+
expect(result).to.deep.equal({
988+
errors: [
989+
{
990+
message: 'GraphQL cannot execute a request containing a ' +
991+
'ObjectTypeDefinition.',
992+
locations: [ { line: 4, column: 7 } ],
993+
path: undefined,
994+
}
995+
]
974996
});
975997
});
976998

0 commit comments

Comments
 (0)