Nullability in practice

Intermediate · 23 min

Decide where the exclamation mark goes: why fields are nullable by default, how one failing non-null field blanks a response, what to promise.

Before thisNon-null and listsErrors and partial responsesSchema design principles

GraphQL Working Groups · 23 min

Nullability in practice

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by GraphQL Working GroupsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Every field is nullable unless you add `!`, because any field can fail and a null lets the rest of the response survive.
  • `!` is a promise to every client for the life of the API. Removing it later is a breaking change, so loosening an output from non-null to nullable breaks clients, while tightening nullable to non-null does not.
  • On input the direction flips: making an optional argument or input field required breaks callers.
  • A failing non-null field nulls its nearest nullable parent. In a test, one failing `score: Int!` inside `[Item!]!` turned the whole response into `data: null`.
  • Mark a field non-null only when it comes from the same source as its parent and cannot fail independently; keep fields backed by another service, a computation or a permission check nullable.

The problem: a promise you cannot take back

In TypeScript, a type without ? means “always present”, and the compiler checks it. GraphQL flips the default. Every field may be null unless the schema says !. That looks like friction at first, but it comes from the way GraphQL executes: any resolver can fail, and a nullable field gives the failure somewhere to land.

Adding ! is a promise to every client: this field will never be null. Clients then write code that depends on it, with generated types that have no null case. If you break the promise, there is nothing to catch it.

Why default to nullable

Recall how errors work. A failing field becomes null and the error is recorded. If the field is non-null, null is not allowed, so the failure moves up to the parent, and continues up until a nullable one absorbs it.

I measured how far it goes. A list of three items; the score of item 2 throws. Three variants, each queried with { items { id score } }:

score: Int!,  items: [Item!]!   data: null
score: Int!,  items: [Item]!    data: { items: [ {id:1,score:10}, null, {id:3,score:10} ] }
score: Int,   items: [Item!]!   data: { items: [ {…score:10}, {id:2,score:null}, {…score:10} ] }

The first row is the nightmare: one flaky field on one item deleted the whole response, because everything above it was non-null. The third row is the sensible one. Only the failing value is lost, and the client can render “score unavailable”.

The talk in this lesson describes this exact situation: marking every field non-null looks like it improves readability, and the downsides show up during service disruptions.

A rule of thumb for !

Ask: can this field fail independently of its parent?

Safe to make non-null:

  • id, always set, from the same row as the parent.
  • Columns that are NOT NULL in the database and come back with the parent row.
  • A field whose value is a pure function of the parent, such as fullName built from two non-null fields.
  • The list itself, when empty is a legitimate answer: posts: [Post!]! returns [] rather than null.

Keep nullable:

  • Fields fed by another service or an external call: recommendations, prices from a vendor, geolocation.
  • Fields behind a permission check. If a user may not see salary, null is the natural answer, and a non-null salary would turn a denied field into a failed parent.
  • Optional data: middleName, publishedAt on a draft.
  • Fields that are expensive or experimental and might be switched off.
  • Single-object lookups: post(id: ID!) returns Post, not Post!, because “not found” is null.

Think of non-null like a load-bearing wall: put it only where you are certain the structure will always stand.

Lists have two exclamation marks

A list has two nullability decisions: the list, and the items. [Post!]! is the usual pick: always a list, never null entries. [Post]! allows null entries, which is useful when items come from a source that can fail one by one, because a null entry limits damage to that item, as the second row above shows. [Post!] allows a null list, which is rarely what you want; prefer an empty list.

Inputs run the other way

For arguments and input fields, ! means required. Here the safe direction flips:

Change Output field Input field or argument
nullable → non-null safe breaks callers who omit it
non-null → nullable breaks clients safe

The reasoning is the same promise seen from either side. If the server promises more on output, clients cannot be hurt. If the server demands more on input, old callers are. So make new input fields optional or give them a default; the body: String = "" in an earlier lesson’s input type is that move. This is also why starting with the looser type is cheap and starting with the stricter one is not.

What may change

The ecosystem has been working on this. Proposals include client-controlled nullability, where a query itself says field! or field? to override the schema, and approaches that separate “can be null because it failed” from “can be null because the value is absent.” The talk describes client-controlled nullability and existing tools that let clients decide. These are not part of the settled spec in this lesson, so check your server and client libraries before relying on them. The principles above, nullable by default and ! as a promise, hold either way.

What the video covers

Ernie Turner of Coinbase gives this 22-minute talk, “Improving User Experiences with a Nullable Schema,” at a GraphQL Working Groups session in September 2023. Its description says it is tempting to mark every field non-null with !, that downsides appear during service disruptions, that the talk will challenge the server-centric approach and advocate shifting nullability responsibility to the client, and introduce existing and upcoming tools, including the Client Controlled Nullability proposal. It does argue a particular position, so the practical rules and the measurements above are this lesson’s; I could not read a transcript. Jamie Barton’s five-minute video in the links covers the basics of the ! syntax.

Where this fits

You can now choose ! deliberately. The next topic is lists that are too large to return whole, which makes both nullability and shape decisions permanent. Next: pagination: offset and cursor.

Check yourself

  1. 01Why are GraphQL fields nullable by default, unlike most typed languages?

    A resolver can fail for reasons the type cannot predict: another service, a timeout, a permission check. A null lets the response keep its other fields instead of failing as a whole.

  2. 02You change `score: Int` to `score: Int!` on an output type. Do existing clients break?

    No. They already handled null and now never receive it. Going the other way, from `Int!` to `Int`, breaks clients that assumed a value.

  3. 03In my test, items were `[Item!]!` with `score: Int!` and one score failed. What came back, and what would have limited the damage?

    `data: null`. Making `score` nullable, or the list `[Item]!`, would have kept the other items; with a nullable `score` the item survived with `score: null`.

  4. 04Which of these fields should be non-null: the `id` of a post, or its `recommendations` list from a separate service?

    The `id`, because it comes from the same row as the post. `recommendations` should stay nullable because the other service can be down, and the page can still render without it.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark