Skip to content

Commit 95da88d

Browse files
committed
Centralise the definition of GraphQLErrorBehavior
1 parent 2113676 commit 95da88d

File tree

6 files changed

+20
-10
lines changed

6 files changed

+20
-10
lines changed

src/error/ErrorBehavior.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type GraphQLErrorBehavior = 'PROPAGATE' | 'NO_PROPAGATE' | 'ABORT';
2+
3+
export function isErrorBehavior(
4+
onError: unknown,
5+
): onError is GraphQLErrorBehavior {
6+
return (
7+
onError === 'PROPAGATE' || onError === 'NO_PROPAGATE' || onError === 'ABORT'
8+
);
9+
}

src/error/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export type {
99
export { syntaxError } from './syntaxError';
1010

1111
export { locatedError } from './locatedError';
12+
export type { GraphQLErrorBehavior } from './ErrorBehavior';

src/execution/execute.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import {
5858
collectSubfields as _collectSubfields,
5959
} from './collectFields';
6060
import { getArgumentValues, getVariableValues } from './values';
61+
import { GraphQLErrorBehavior, isErrorBehavior } from '../error/ErrorBehavior';
6162

6263
/**
6364
* A memoized collection of relevant subfields with regard to the return
@@ -116,7 +117,7 @@ export interface ExecutionContext {
116117
typeResolver: GraphQLTypeResolver<any, any>;
117118
subscribeFieldResolver: GraphQLFieldResolver<any, any>;
118119
errors: Array<GraphQLError>;
119-
errorBehavior: 'PROPAGATE' | 'NO_PROPAGATE' | 'ABORT';
120+
errorBehavior: GraphQLErrorBehavior;
120121
}
121122

122123
/**
@@ -132,6 +133,7 @@ export interface ExecutionResult<
132133
> {
133134
errors?: ReadonlyArray<GraphQLError>;
134135
data?: TData | null;
136+
onError?: GraphQLErrorBehavior;
135137
extensions?: TExtensions;
136138
}
137139

@@ -162,7 +164,7 @@ export interface ExecutionArgs {
162164
*
163165
* @experimental
164166
*/
165-
onError?: 'PROPAGATE' | 'NO_PROPAGATE' | 'ABORT';
167+
onError?: GraphQLErrorBehavior;
166168
}
167169

168170
/**
@@ -300,12 +302,7 @@ export function buildExecutionContext(
300302
onError,
301303
} = args;
302304

303-
if (
304-
onError != null &&
305-
onError !== 'PROPAGATE' &&
306-
onError !== 'NO_PROPAGATE' &&
307-
onError !== 'ABORT'
308-
) {
305+
if (onError != null && !isErrorBehavior(onError)) {
309306
return [
310307
new GraphQLError(
311308
'Unsupported `onError` value; supported values are `PROPAGATE`, `NO_PROPAGATE` and `ABORT`.',

src/graphql.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { validate } from './validation/validate';
1717

1818
import type { ExecutionResult } from './execution/execute';
1919
import { execute } from './execution/execute';
20+
import type { GraphQLErrorBehavior } from './error/ErrorBehavior';
2021

2122
/**
2223
* This is the primary entry point function for fulfilling GraphQL operations
@@ -74,7 +75,7 @@ export interface GraphQLArgs {
7475
*
7576
* @experimental
7677
*/
77-
onError?: 'PROPAGATE' | 'NO_PROPAGATE' | 'ABORT';
78+
onError?: GraphQLErrorBehavior;
7879
}
7980

8081
export function graphql(args: GraphQLArgs): Promise<ExecutionResult> {

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ export {
400400
} from './error/index';
401401

402402
export type {
403+
GraphQLErrorBehavior,
403404
GraphQLErrorOptions,
404405
GraphQLFormattedError,
405406
GraphQLErrorExtensions,

src/type/definition.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
1414
import { suggestionList } from '../jsutils/suggestionList';
1515
import { toObjMap } from '../jsutils/toObjMap';
1616

17+
import type { GraphQLErrorBehavior } from '../error/ErrorBehavior';
1718
import { GraphQLError } from '../error/GraphQLError';
1819

1920
import type {
@@ -1097,7 +1098,7 @@ export interface GraphQLResolveInfo {
10971098
readonly operation: OperationDefinitionNode;
10981099
readonly variableValues: { [variable: string]: unknown };
10991100
/** @experimental */
1100-
readonly errorBehavior: 'PROPAGATE' | 'NO_PROPAGATE' | 'ABORT';
1101+
readonly errorBehavior: GraphQLErrorBehavior;
11011102
}
11021103

11031104
/**

0 commit comments

Comments
 (0)