Skip to content

[WIP] Experimental support for semantic-non-null #4192

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

Draft
wants to merge 39 commits into
base: on-error
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7ca49b2
New GraphQLSemanticNonNull type
benjie Sep 14, 2024
16a2114
Handle isNonNullType
benjie Sep 14, 2024
2b13389
More fixes
benjie Sep 14, 2024
04a8e91
More fixes
benjie Sep 14, 2024
076a735
Yet more updates
benjie Sep 14, 2024
c2196a0
Recognize in introspection, enable disabling null bubbling
benjie Sep 14, 2024
f588046
Lint fixes
benjie Sep 14, 2024
fa3f177
More missing pieces
benjie Sep 14, 2024
b5e81bd
More fixes
benjie Sep 14, 2024
1f6a019
Fix schema
benjie Sep 14, 2024
491f49b
Fix another test
benjie Sep 14, 2024
3a91590
More minor test fixes
benjie Sep 14, 2024
56db880
Fix introspection test
benjie Sep 14, 2024
593ce44
Add support for * to lexer
benjie Sep 14, 2024
1311906
Allow specifying errorPropagation at top level
benjie Sep 14, 2024
9d706d2
Factor into getIntrospectionQuery
benjie Sep 14, 2024
e9f9628
Lint
benjie Sep 14, 2024
eb9b6c8
Prettier
benjie Sep 14, 2024
6ef4bec
Merge branch '16.x.x' into semantic-non-null
benjie Mar 26, 2025
8fcacc8
Switch to errorBehavior, replace contextual introspection to simple i…
benjie Mar 26, 2025
88c5d93
Simplify
benjie Mar 26, 2025
62d1b75
Stricter types: semantic non null may only wrap output types
benjie Mar 26, 2025
96e8b53
Use GraphQLNullableOutputType instead of intersection
benjie Mar 26, 2025
a2169ac
Simpler type
benjie Mar 26, 2025
1ce6880
Only allow GraphQLSemanticNonNull of output type
benjie Mar 26, 2025
97256f0
Tidy
benjie Mar 26, 2025
f464644
Memoize
benjie Mar 26, 2025
2113676
Rename errorBehavior to onError and NULL to NO_PROPAGATE
benjie Mar 27, 2025
95da88d
Centralise the definition of GraphQLErrorBehavior
benjie Mar 27, 2025
a1d2dbe
Lint
benjie Mar 27, 2025
70dc6f8
Prettier
benjie Mar 27, 2025
f3109c3
Implement onError proposal
benjie Mar 27, 2025
a4cec5c
Add tests
benjie Mar 27, 2025
947b040
Test invalid onError is handled
benjie Mar 27, 2025
1bcc31d
Ignore invariant from code coverage
benjie Mar 27, 2025
8338656
Finickity
benjie Mar 27, 2025
c8fdfba
Urghhhhhh
benjie Mar 27, 2025
1cff421
Remove unnecessary resolver causing coverage issue
benjie Mar 27, 2025
c0d54cf
Merge branch 'on-error' into semantic-non-null
benjie Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement onError proposal
  • Loading branch information
benjie committed Mar 27, 2025
commit f3109c3952828cb9e8c2be9cfea93f000069cbd6
9 changes: 9 additions & 0 deletions src/error/ErrorBehavior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type GraphQLErrorBehavior = 'PROPAGATE' | 'NO_PROPAGATE' | 'ABORT';

export function isErrorBehavior(
onError: unknown,
): onError is GraphQLErrorBehavior {
return (
onError === 'PROPAGATE' || onError === 'NO_PROPAGATE' || onError === 'ABORT'
);
}
1 change: 1 addition & 0 deletions src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export type {
export { syntaxError } from './syntaxError';

export { locatedError } from './locatedError';
export type { GraphQLErrorBehavior } from './ErrorBehavior';
2 changes: 2 additions & 0 deletions src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ describe('Execute: Handles basic execution tasks', () => {
'rootValue',
'operation',
'variableValues',
'errorBehavior',
);

const operation = document.definitions[0];
Expand All @@ -275,6 +276,7 @@ describe('Execute: Handles basic execution tasks', () => {
schema,
rootValue,
operation,
errorBehavior: 'PROPAGATE',
});

const field = operation.selectionSet.selections[0];
Expand Down
45 changes: 42 additions & 3 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { promiseForObject } from '../jsutils/promiseForObject';
import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
import { promiseReduce } from '../jsutils/promiseReduce';

import type { GraphQLErrorBehavior } from '../error/ErrorBehavior';
import { isErrorBehavior } from '../error/ErrorBehavior';
import type { GraphQLFormattedError } from '../error/GraphQLError';
import { GraphQLError } from '../error/GraphQLError';
import { locatedError } from '../error/locatedError';
Expand Down Expand Up @@ -115,6 +117,7 @@ export interface ExecutionContext {
typeResolver: GraphQLTypeResolver<any, any>;
subscribeFieldResolver: GraphQLFieldResolver<any, any>;
errors: Array<GraphQLError>;
errorBehavior: GraphQLErrorBehavior;
}

/**
Expand All @@ -130,6 +133,7 @@ export interface ExecutionResult<
> {
errors?: ReadonlyArray<GraphQLError>;
data?: TData | null;
onError?: GraphQLErrorBehavior;
extensions?: TExtensions;
}

Expand All @@ -152,6 +156,15 @@ export interface ExecutionArgs {
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
subscribeFieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
/**
* Experimental. Set to NO_PROPAGATE to prevent error propagation. Set to ABORT to
* abort a request when any error occurs.
*
* Default: PROPAGATE
*
* @experimental
*/
onError?: GraphQLErrorBehavior;
}

/**
Expand Down Expand Up @@ -286,8 +299,17 @@ export function buildExecutionContext(
fieldResolver,
typeResolver,
subscribeFieldResolver,
onError,
} = args;

if (onError != null && !isErrorBehavior(onError)) {
return [
new GraphQLError(
'Unsupported `onError` value; supported values are `PROPAGATE`, `NO_PROPAGATE` and `ABORT`.',
),
];
}

let operation: OperationDefinitionNode | undefined;
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
for (const definition of document.definitions) {
Expand Down Expand Up @@ -347,6 +369,7 @@ export function buildExecutionContext(
typeResolver: typeResolver ?? defaultTypeResolver,
subscribeFieldResolver: subscribeFieldResolver ?? defaultFieldResolver,
errors: [],
errorBehavior: onError ?? 'PROPAGATE',
};
}

Expand Down Expand Up @@ -585,6 +608,7 @@ export function buildResolveInfo(
rootValue: exeContext.rootValue,
operation: exeContext.operation,
variableValues: exeContext.variableValues,
errorBehavior: exeContext.errorBehavior,
};
}

Expand All @@ -593,10 +617,25 @@ function handleFieldError(
returnType: GraphQLOutputType,
exeContext: ExecutionContext,
): null {
// If the field type is non-nullable, then it is resolved without any
// protection from errors, however it still properly locates the error.
if (isNonNullType(returnType)) {
if (exeContext.errorBehavior === 'PROPAGATE') {
// If the field type is non-nullable, then it is resolved without any
// protection from errors, however it still properly locates the error.
// Note: semantic non-null types are treated as nullable for the purposes
// of error handling.
if (isNonNullType(returnType)) {
throw error;
}
} else if (exeContext.errorBehavior === 'ABORT') {
// In this mode, any error aborts the request
throw error;
} else if (exeContext.errorBehavior === 'NO_PROPAGATE') {
// In this mode, the client takes responsibility for error handling, so we
// treat the field as if it were nullable.
} else {
invariant(
false,
'Unexpected errorBehavior setting: ' + inspect(exeContext.errorBehavior),
);
}

// Otherwise, error protection is applied, logging the error and resolving
Expand Down
13 changes: 13 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { isPromise } from './jsutils/isPromise';
import type { Maybe } from './jsutils/Maybe';
import type { PromiseOrValue } from './jsutils/PromiseOrValue';

import type { GraphQLErrorBehavior } from './error/ErrorBehavior';

import { parse } from './language/parser';
import type { Source } from './language/source';

Expand Down Expand Up @@ -66,6 +68,15 @@ export interface GraphQLArgs {
operationName?: Maybe<string>;
fieldResolver?: Maybe<GraphQLFieldResolver<any, any>>;
typeResolver?: Maybe<GraphQLTypeResolver<any, any>>;
/**
* Experimental. Set to NO_PROPAGATE to prevent error propagation. Set to ABORT to
* abort a request when any error occurs.
*
* Default: PROPAGATE
*
* @experimental
*/
onError?: GraphQLErrorBehavior;
}

export function graphql(args: GraphQLArgs): Promise<ExecutionResult> {
Expand Down Expand Up @@ -106,6 +117,7 @@ function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
operationName,
fieldResolver,
typeResolver,
onError,
} = args;

// Validate Schema
Expand Down Expand Up @@ -138,5 +150,6 @@ function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
operationName,
fieldResolver,
typeResolver,
onError,
});
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ export {
} from './error/index';

export type {
GraphQLErrorBehavior,
GraphQLErrorOptions,
GraphQLFormattedError,
GraphQLErrorExtensions,
Expand Down
3 changes: 3 additions & 0 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue';
import { suggestionList } from '../jsutils/suggestionList';
import { toObjMap } from '../jsutils/toObjMap';

import type { GraphQLErrorBehavior } from '../error/ErrorBehavior';
import { GraphQLError } from '../error/GraphQLError';

import type {
Expand Down Expand Up @@ -988,6 +989,8 @@ export interface GraphQLResolveInfo {
readonly rootValue: unknown;
readonly operation: OperationDefinitionNode;
readonly variableValues: { [variable: string]: unknown };
/** @experimental */
readonly errorBehavior: GraphQLErrorBehavior;
}

/**
Expand Down
Loading