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:

generated/schema.graphql
directive @semanticNonNull(levels: [Int] = [0]) on FIELD_DEFINITION

Clients 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:

User.ts
/** @gqlType */
type User = {
  name: string;
};

/** @gqlField */
export function name(user: User): string {
  return user.name;
}

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:

tsconfig.json
{
  "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:

aString.ts
/** @gqlQueryField */
export function aString(): string {
  const someArr: string[] = [];
  return someArr[0]; // Oops! Out of bounds access!
}

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