Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export execution algorithm implementation as Executor class #3193

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d8e3254
feat: introduce GraphQLAggregateError
yaacovCR Jun 21, 2021
b7a0645
refactor: move subscriptions into execution folder
yaacovCR Jun 22, 2021
9b88831
refactor: introduce executeQueryOrMutation
yaacovCR Jun 22, 2021
f54a818
remove buildExecutionContext from try block
yaacovCR Jun 22, 2021
b6a446d
refactor: use handleRawError with subscriptions
yaacovCR Jun 22, 2021
2a0a03e
refactor: rename executeSubscription
yaacovCR Jun 22, 2021
04a93cf
refactor: move underlying subscription methods
yaacovCR Jun 22, 2021
dab7997
refactor: executeSubscriptionRootField
yaacovCR Jun 22, 2021
f7c1ae5
introduce executeSubscription
yaacovCR Jun 22, 2021
dc88bcf
refactor: streamline exeContext access
yaacovCR Jun 22, 2021
b98d329
introduce isAggregateOfGraphQLErrors helper
yaacovCR Jun 22, 2021
2b54f3c
refactor: narrow buildExecutionContext returnType
yaacovCR Jun 22, 2021
3b5983f
refactor: move assertValidExecutionArguments
yaacovCR Jun 22, 2021
92912b4
use named arguments for buildExecutionContext
yaacovCR Jun 22, 2021
b31b67c
refactor: move non-Executor functions to eof
yaacovCR Jun 22, 2021
6ba6cdd
refactor: executor methods into Executor class
yaacovCR Jun 22, 2021
95171f9
refactor: store ExecutionContext in Executor
yaacovCR Jun 22, 2021
4994041
refactor: spread ExecutionContext properties
yaacovCR Jun 22, 2021
ea5051e
refactor: move Executor into separate file
yaacovCR Jun 22, 2021
2423b77
introduce ExecutorArgs interface
yaacovCR Jun 22, 2021
c124435
export supporting Executor interfaces
yaacovCR Jun 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/APIReference-Errors.md
Expand Up @@ -3,7 +3,7 @@ title: graphql/error
layout: ../_core/GraphQLJSLayout
category: API Reference
permalink: /graphql-js/error/
sublinks: formatError,GraphQLError,locatedError,syntaxError
sublinks: formatError,GraphQLError,locatedError,syntaxError,GraphQLAggregateError
next: /graphql-js/execution/
---

Expand Down Expand Up @@ -107,3 +107,20 @@ type GraphQLErrorLocation = {

Given a GraphQLError, format it according to the rules described by the
Response Format, Errors section of the GraphQL Specification.

### GraphQLAggregateError

```js
class GraphQLAggregateError extends Error {
constructor(
errors: Array<Error>,
message?: string
)
}
```

A helper class for bundling multiple distinct errors. When a
GraphQLAggregateError is thrown during execution of a GraphQL operation,
a GraphQLError will be produced from each individual errors and will be
reported separately, according to the rules described by the Response
Format, Errors section of the GraphQL Specification.
1 change: 0 additions & 1 deletion src/README.md
Expand Up @@ -20,4 +20,3 @@ Each sub directory within is a sub-module of graphql-js:
- [`graphql/error`](error/README.md): Creating and formatting GraphQL errors.
- [`graphql/utilities`](utilities/README.md): Common useful computations upon
the GraphQL language and type objects.
- [`graphql/subscription`](subscription/README.md): Subscribe to data updates.
47 changes: 47 additions & 0 deletions src/error/GraphQLAggregateError.ts
@@ -0,0 +1,47 @@
import { GraphQLError } from './GraphQLError';

/**
* A GraphQLAggregateError is a container for multiple errors.
*
* This helper can be used to report multiple distinct errors simultaneously.
* Note that error handlers must be aware aggregated errors may be reported so as to
* properly handle the contained errors.
*
* See also:
* https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-aggregate-error-objects
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
* https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.aggregate-error.js
* https://github.com/sindresorhus/aggregate-error
*
*/
export class GraphQLAggregateError<T = Error> extends Error {
readonly errors!: ReadonlyArray<T>;

constructor(errors: ReadonlyArray<T>, message?: string) {
super(message);

Object.defineProperties(this, {
name: { value: 'GraphQLAggregateError' },
message: {
value: message,
writable: true,
},
errors: {
value: errors,
},
});
}

get [Symbol.toStringTag](): string {
return 'GraphQLAggregateError';
}
}

export function isAggregateOfGraphQLErrors(
error: unknown,
): error is GraphQLAggregateError<GraphQLError> {
return (
error instanceof GraphQLAggregateError &&
error.errors.every((subError) => subError instanceof GraphQLError)
);
}
25 changes: 25 additions & 0 deletions src/error/__tests__/GraphQLAggregateError-test.ts
@@ -0,0 +1,25 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { GraphQLAggregateError } from '../GraphQLAggregateError';

describe('GraphQLAggregateError', () => {
it('is a class and is a subclass of Error', () => {
const errors = [new Error('Error1'), new Error('Error2')];
expect(new GraphQLAggregateError(errors)).to.be.instanceof(Error);
expect(new GraphQLAggregateError(errors)).to.be.instanceof(
GraphQLAggregateError,
);
});

it('has a name, errors, and a message (if provided)', () => {
const errors = [new Error('Error1'), new Error('Error2')];
const e = new GraphQLAggregateError(errors, 'msg');

expect(e).to.include({
name: 'GraphQLAggregateError',
errors,
message: 'msg',
});
});
});
5 changes: 5 additions & 0 deletions src/error/index.ts
@@ -1,3 +1,8 @@
export {
GraphQLAggregateError,
isAggregateOfGraphQLErrors,
} from './GraphQLAggregateError';

export { GraphQLError, printError, formatError } from './GraphQLError';
export type { GraphQLFormattedError } from './GraphQLError';

Expand Down
5 changes: 5 additions & 0 deletions src/execution/README.md
Expand Up @@ -7,3 +7,8 @@ fulfilling a GraphQL request.
import { execute } from 'graphql/execution'; // ES6
var GraphQLExecution = require('graphql/execution'); // CommonJS
```

```js
import { subscribe, createSourceEventStream } from 'graphql/execution'; // ES6
var GraphQLSubscription = require('graphql/execution'); // CommonJS
```