Guides
Strict semantic nullability
On this page
Strict semantic nullability is an early-stage idea that the GraphQL Working Group is exploring. To help the community understand the idea and its implications, Gratos includes experimental support for it.
Caution
Because strict semantic nullability is still in flux, its implementation and behavior in Gratos may change as we learn more about the idea and its implications.
What is strict semantic nullability?
Today a GraphQL field is either nullable or non-nullable. But this distinction is overloaded: null represents both a "true" null value and a value in an error state. Strict semantic nullability teases these two apart: the schema can mark the fields that are null only in case of error. A client that handles field errors out of band, for example by discarding any response with errors, can then treat these fields as non-nullable.
Client code can then decide about the expected nullability of a field with full information: it avoids tedious null checks of values that are never null in practice, and still handles the nulls it should expect.
How does Gratos support strict semantic nullability?
The current RFC for strict semantic nullability proposes new schema syntax for output types that are null only in case of error. But new syntax needs support from many tools of the GraphQL ecosystem: servers, clients and IDEs. So that you can experiment with it today, Gratos uses a directive instead of new syntax. It is noisier, but needs no new syntax support:
directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITIONClients may assume that a field annotated with this directive is null only when the response's errors include an error for this field or value. Apollo Kotlin is one client with experimental support for this directive.
Strict semantic nullability builds on the nullableByDefault option. With nullableByDefault on, Gratos types every field of the schema as nullable, even when its TypeScript type is non-nullable. With strict semantic nullability on as well, the fields stay nullable, but a field whose TypeScript type is non-nullable also gets the @semanticNonNull directive. That tells error-handling clients the field is null only in case of error.
Example
With strict semantic nullability on, a field that returns a non-nullable string is typed as nullable, with the @semanticNonNull directive. The schema also defines the directive itself:
/** @gqlType */
type User = {
name: string;
};
/** @gqlField */
export function name(user: User): string {
return user.name;
}"""
Indicates that a position is semantically non null: it is only null if there is a matching error in the `errors` array.
In all other cases, the position is non-null.
Tools doing code generation may use this information to generate the position as non-null if field errors are handled out of band:
```graphql
type User {
# email is semantically non-null and can be generated as non-null by error-handling clients.
email: String @semanticNonNull
}
```
The `levels` argument indicates what levels are semantically non null in case of lists:
```graphql
type User {
# friends is semantically non null
friends: [User] @semanticNonNull # same as @semanticNonNull(levels: [0])
# every friends[k] is semantically non null
friends: [User] @semanticNonNull(levels: [1])
# friends as well as every friends[k] is semantically non null
friends: [User] @semanticNonNull(levels: [0, 1])
}
```
`levels` are zero indexed.
Passing a negative level or a level greater than the list dimension is an error.
"""
directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITION
type User {
name: String @semanticNonNull
}Enabling strict semantic nullability
Turn strict semantic nullability on in the Gratos config in your tsconfig.json. nullableByDefault must be on too, as it is by default. The sample above's config:
{
"extends": "../tsconfig.base.json",
"include": ["*.ts"],
"grats": {
"graphqlSchema": "./generated/schema.graphql",
"tsSchema": "./generated/schema.ts",
"importModuleSpecifierEnding": ".ts",
"schemaHeader": null,
"strictSemanticNullability": true
}
}This site's own schema is strict too.
Limitations
Gratos lets the community experiment with strict semantic nullability by supporting part of the proposed RFC, not all of it. In particular:
- Gratos does not support determining with introspection whether a field is semantically non-null.
- Gratos does not support declaring the items of a list as semantically non-null.
Info
Gratos does not support marking list items as semantically non-null because most resolvers cannot fail on an individual item of a list without failing the list resolver itself. Generally that only happens when you return an Iterable that throws when next() is called.
Runtime validation
With strict semantic nullability on, Gratos inserts runtime validation to ensure that fields marked as semantically non-null never return null. If such a field returns null, the validation throws an error, so the field is in an error state. This is needed because TypeScript is not fully sound, and a function typed as non-nullable can return null:
/** @gqlQueryField */
export function aString(): string {
const someArr: string[] = [];
return someArr[0]; // Oops! Out of bounds access!
}"""
Indicates that a position is semantically non null: it is only null if there is a matching error in the `errors` array.
In all other cases, the position is non-null.
Tools doing code generation may use this information to generate the position as non-null if field errors are handled out of band:
```graphql
type User {
# email is semantically non-null and can be generated as non-null by error-handling clients.
email: String @semanticNonNull
}
```
The `levels` argument indicates what levels are semantically non null in case of lists:
```graphql
type User {
# friends is semantically non null
friends: [User] @semanticNonNull # same as @semanticNonNull(levels: [0])
# every friends[k] is semantically non null
friends: [User] @semanticNonNull(levels: [1])
# friends as well as every friends[k] is semantically non null
friends: [User] @semanticNonNull(levels: [0, 1])
}
```
`levels` are zero indexed.
Passing a negative level or a level greater than the list dimension is an error.
"""
directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITION
type Query {
aString: String @semanticNonNull
}For a field that returns an Effect.Effect, the generated check cannot see inside the Effect, so Gratos marks the field semanticNonNull instead, and the server's Effect layer checks the value the Effect succeeds with (see What the compiler generates).
Further reading
- True Nullability Schema
- Strict Semantic Nullability
- RFC: SemanticNonNull type (null only on error)
- Apollo's specification for this directive
- Support for
@SemanticNonNullin Apollo Kotlin, added in 4.0.0-beta.3 - Example project showing this feature with experimental support in Relay
- Semantic Nullability - A Path Toward Safe NonNull Fields, Jordan Eldredge's talk at GraphQL Conf 2024
- Awesome Semantic Nullability, a list of projects and tools that support or implement strict semantic nullability