Skip to content

Commit

Permalink
Refactor executeOperation (#883)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leebyron authored May 26, 2017
1 parent 1c4477c commit 508aa3a
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 255 deletions.
74 changes: 48 additions & 26 deletions src/execution/__tests__/executor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ describe('Execute: Handles basic execution tasks', () => {
expect(result).to.deep.equal({ data: { second: 'b' } });
});

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

expect(() => execute(schema, ast, data)).to.throw(
'Must provide an operation.'
);
const result = await execute(schema, ast, data);
expect(result).to.deep.equal({
errors: [
{
message: 'Must provide an operation.',
locations: undefined,
path: undefined,
}
]
});
});

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

expect(() => execute(schema, ast, data)).to.throw(
'Must provide operation name if query contains multiple operations.'
);
const result = await execute(schema, ast, data);
expect(result).to.deep.equal({
errors: [
{
message: 'Must provide operation name if query contains ' +
'multiple operations.',
locations: undefined,
path: undefined,
}
]
});
});

it('throws if unknown operation name is provided', () => {
it('throws if unknown operation name is provided', async () => {
const doc = 'query Example { a } query OtherExample { a }';
const data = { a: 'b' };
const ast = parse(doc);
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
Expand All @@ -661,11 +675,20 @@ describe('Execute: Handles basic execution tasks', () => {
})
});

expect(() =>
execute(schema, ast, data, null, null, 'UnknownExample')
).to.throw(
'Unknown operation named "UnknownExample".'
);
const result = await execute({
schema,
document: ast,
operationName: 'UnknownExample'
});
expect(result).to.deep.equal({
errors: [
{
message: 'Unknown operation named "UnknownExample".',
locations: undefined,
path: undefined,
}
]
});
});

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

let caughtError;
try {
await execute(schema, query);
} catch (error) {
caughtError = error;
}

expect(caughtError).to.jsonEqual({
message:
'GraphQL cannot execute a request containing a ObjectTypeDefinition.',
locations: [ { line: 4, column: 7 } ]
const result = await execute(schema, query);
expect(result).to.deep.equal({
errors: [
{
message: 'GraphQL cannot execute a request containing a ' +
'ObjectTypeDefinition.',
locations: [ { line: 4, column: 7 } ],
path: undefined,
}
]
});
});

Expand Down
Loading

0 comments on commit 508aa3a

Please sign in to comment.