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
Stricter types: semantic non null may only wrap output types
  • Loading branch information
benjie committed Mar 26, 2025
commit 62d1b7520cc00ba20a84cf176e2105a47f96b220
29 changes: 12 additions & 17 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ export type GraphQLType =
| GraphQLInterfaceType
| GraphQLUnionType
| GraphQLEnumType
| GraphQLInputObjectType
| GraphQLList<GraphQLType>
| GraphQLList<GraphQLOutputType>
>;

export function isType(type: unknown): type is GraphQLType {
Expand Down Expand Up @@ -213,24 +212,15 @@ export function assertNonNullType(type: unknown): GraphQLNonNull<GraphQLType> {
return type;
}

export function isSemanticNonNullType(
type: GraphQLInputType,
): type is GraphQLSemanticNonNull<GraphQLInputType>;
export function isSemanticNonNullType(
type: GraphQLOutputType,
): type is GraphQLSemanticNonNull<GraphQLOutputType>;
export function isSemanticNonNullType(
type: unknown,
): type is GraphQLSemanticNonNull<GraphQLType>;
export function isSemanticNonNullType(
type: unknown,
): type is GraphQLSemanticNonNull<GraphQLType> {
): type is GraphQLSemanticNonNull<GraphQLNullableType & GraphQLOutputType> {
return instanceOf(type, GraphQLSemanticNonNull);
}

export function assertSemanticNonNullType(
type: unknown,
): GraphQLSemanticNonNull<GraphQLType> {
): GraphQLSemanticNonNull<GraphQLNullableType & GraphQLOutputType> {
if (!isSemanticNonNullType(type)) {
throw new Error(
`Expected ${inspect(type)} to be a GraphQL Semantic-Non-Null type.`,
Expand Down Expand Up @@ -485,7 +475,9 @@ export class GraphQLNonNull<T extends GraphQLNullableType> {
*
* @experimental
*/
export class GraphQLSemanticNonNull<T extends GraphQLNullableType> {
export class GraphQLSemanticNonNull<
T extends GraphQLNullableType & GraphQLOutputType,
> {
readonly ofType: T;

constructor(ofType: T) {
Expand Down Expand Up @@ -516,8 +508,8 @@ export class GraphQLSemanticNonNull<T extends GraphQLNullableType> {

export type GraphQLWrappingType =
| GraphQLList<GraphQLType>
| GraphQLNonNull<GraphQLType>
| GraphQLSemanticNonNull<GraphQLType>;
| GraphQLNonNull<GraphQLNullableType>
| GraphQLSemanticNonNull<GraphQLNullableType & GraphQLOutputType>;

export function isWrappingType(type: unknown): type is GraphQLWrappingType {
return isListType(type) || isNonNullType(type) || isSemanticNonNullType(type);
Expand Down Expand Up @@ -555,7 +547,10 @@ export function assertNullableType(type: unknown): GraphQLNullableType {

export function getNullableType(type: undefined | null): void;
export function getNullableType<T extends GraphQLNullableType>(
type: T | GraphQLNonNull<T> | GraphQLSemanticNonNull<T>,
type:
| T
| GraphQLNonNull<T>
| GraphQLSemanticNonNull<T extends GraphQLOutputType ? T : never>,
): T;
export function getNullableType(
type: Maybe<GraphQLType>,
Expand Down
9 changes: 7 additions & 2 deletions src/utilities/buildClientSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,18 @@
return new GraphQLNonNull(assertNullableType(nullableType));
}
if (typeRef.kind === TypeKind.SEMANTIC_NON_NULL) {
const nullableRef = typeRef.ofType;
if (!nullableRef) {
throw new Error('Decorated type deeper than introspection query.');
}
const nullableType = getType(nullableRef);
return new GraphQLSemanticNonNull(assertNullableType(nullableType));
const nullableType = assertNullableType(getType(nullableRef));
if (!isOutputType(nullableType)) {
throw new Error(
'A semantic non-null wrapper must wrap an output type.',
);
}
return new GraphQLSemanticNonNull(nullableType);
}

Check warning on line 153 in src/utilities/buildClientSchema.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/buildClientSchema.ts#L142-L153

Added lines #L142 - L153 were not covered by tests
return getNamedType(typeRef);
}

Expand Down
7 changes: 6 additions & 1 deletion src/utilities/extendSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
isListType,
isNonNullType,
isObjectType,
isOutputType,
isScalarType,
isSemanticNonNullType,
isUnionType,
Expand Down Expand Up @@ -228,9 +229,9 @@
return new GraphQLNonNull(replaceType(type.ofType));
}
if (isSemanticNonNullType(type)) {
// @ts-expect-error
return new GraphQLSemanticNonNull(replaceType(type.ofType));
}

Check warning on line 234 in src/utilities/extendSchema.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/extendSchema.ts#L232-L234

Added lines #L232 - L234 were not covered by tests
// @ts-expect-error FIXME
return replaceNamedType(type);
}
Expand Down Expand Up @@ -439,8 +440,12 @@
return new GraphQLNonNull(getWrappedType(node.type));
}
if (node.kind === Kind.SEMANTIC_NON_NULL_TYPE) {
return new GraphQLSemanticNonNull(getWrappedType(node.type));
const wrapped = getWrappedType(node.type);
if (!isOutputType(wrapped)) {
throw new Error('A semantic non-null type cannot wrap an input type.');
}
return new GraphQLSemanticNonNull(wrapped);
}

Check warning on line 448 in src/utilities/extendSchema.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/extendSchema.ts#L443-L448

Added lines #L443 - L448 were not covered by tests
return getNamedType(node);
}

Expand Down
4 changes: 4 additions & 0 deletions src/utilities/typeFromAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
GraphQLList,
GraphQLNonNull,
GraphQLSemanticNonNull,
isOutputType,
} from '../type/definition';
import type { GraphQLSchema } from '../type/schema';

Expand Down Expand Up @@ -51,9 +52,12 @@
return innerType && new GraphQLNonNull(innerType);
}
case Kind.SEMANTIC_NON_NULL_TYPE: {
const innerType = typeFromAST(schema, typeNode.type);
if (!isOutputType(innerType)) {
throw new Error('A semantic non-null type must wrap an output type.');
}
return innerType && new GraphQLSemanticNonNull(innerType);
}

Check warning on line 60 in src/utilities/typeFromAST.ts

View check run for this annotation

Codecov / codecov/patch

src/utilities/typeFromAST.ts#L55-L60

Added lines #L55 - L60 were not covered by tests
case Kind.NAMED_TYPE:
return schema.getType(typeNode.name.value);
}
Expand Down
Loading