Non-null and lists
How the ! and [] modifiers combine into four list types, how null propagates up to the nearest nullable parent, and how to choose between nullable and required.
Non-null and lists
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- In GraphQL every type is nullable by default; adding ! makes it non-null, and wrapping in [ ] makes it a list.
- [Int], [Int!], [Int]! and [Int!]! are four different types, depending on whether the list or its items may be null.
- If a non-null field resolves to null, the error is raised and null replaces the nearest nullable parent, so one bad field can erase a whole object.
- A non-null list argument or input rejects null items and missing values at validation time; a single value passed to a list argument is wrapped into a list.
- Choose non-null only for output fields you can always provide: adding a ! later is safe, but removing one breaks clients that stopped checking for null.
Null is the default
In many languages a value must opt in to being nullable. GraphQL does the opposite: every field may return null unless you say it cannot. That default comes from the fact that servers fetch data from places that fail, and one broken source should not take down the whole response.
The ! modifier marks a type as non-null:
type User {
name: String! # always present
nick: String # may be null
}
The brackets mean “a list of”:
type Query {
a: [Int] # nullable list of nullable ints
b: [Int!] # nullable list of non-null ints
c: [Int]! # non-null list of nullable ints
d: [Int!]! # non-null list of non-null ints
}
Those are four different types. The ! after the brackets governs the list itself, and the ! inside governs each item. I ran each against graphql-js 17.1.0 with a resolver that returns [1, null]:
| Field | Returned | What the client sees |
|---|---|---|
a: [Int] |
[1, null] |
[1, null] |
b: [Int!] |
[1, null] |
b: null plus an error at ["b", 1] |
c: [Int]! |
[1, null] |
[1, null] |
d: [Int!]! |
[1, null] |
data: null plus an error at ["d", 1] |
For d, the bad item cannot be null, the list cannot be null, and Query has no nullable parent above it, so the whole data becomes null. That brings us to propagation.
Null propagation
When a non-null field would be null, GraphQL cannot return it. So it records an error and replaces the nearest nullable parent with null. The rule is “bubble up until something is allowed to be null”.
With User.name: String! and a resolver returning null (user is declared as User, nullable):
{ user { name nick } }
{"errors":[{"message":"Cannot return null for non-nullable field User.name.","path":["user","name"]}],"data":{"user":null}}
user is nullable, so it takes the hit: you lose nick as well, even though nick was fine. Ask only for nick and you get {"user":{"nick":null}} with no error. And with e: Int! directly on Query, data itself becomes null.
This is the heart of how you should think about !: it is a promise. Every ! is a statement that this field can always be produced. If the promise breaks, the damage spreads upward. Using ! everywhere makes the happy path pleasant for clients (no null checks) and makes the failure path brutal.
Whole list versus items
null and an empty list are different answers. b: [] returns {"b":[]} for every list type above; a: null returns {"a":null} for the nullable one. For a field like books: [Book!]!, the empty list is the right “nothing found”, and null is not allowed. A client written against that schema never has to write a fallback for a missing list.
A common default for collections is [Item!]!: the list is always there, and no item is ever null. Items inside a list rarely have a meaningful “missing” state, and a null item usually signals a bug. That is why Query.books in this roadmap is [Book!]!.
On the input side
The same modifiers apply to arguments, and the validator is stricter because values come from clients. With x(ids: [Int!]!, flag: Boolean! = true, opt: [Int]):
| Query | Result |
|---|---|
{ x(ids: 1) } |
accepted: a single value is wrapped into [1] |
{ x(ids: [1, null]) } |
Expected value of non-null type “Int!” not to be null. |
{ x } |
Argument “Query.x(ids:)” of type “[Int!]!” is required, but it was not provided. |
{ x(ids: []) } |
accepted: empty list is still a list |
The first row surprises people: GraphQL coerces a single value to a one-element list on input. Arguments and input objects get their own lesson later (input types).
Choosing
- Output fields: be conservative with
!. Mark it only when the value is guaranteed. A field backed by a flaky service should be nullable, so its failure is contained. - Collections:
[T!]!, unless “unknown” and “empty” are different things in your domain. - Arguments: required arguments get
!. Optional ones stay nullable, often with a default. - Changing later: on an output field, going from
TtoT!is safe and going fromT!toTbreaks clients that stopped checking for null. On an argument it is the reverse: making an optional argument required breaks callers. Nullability in practice goes through the trade-offs in depth.
What the video covers
Jamie Barton’s “GraphQL Nullability” is about six minutes, published in May 2022. Its description says it shows how to define nullable and non-nullable fields in a schema-first setup, tagged JavaScript. I could not read a transcript, so I do not know whether it covers propagation or the list variants; the tables above are from my own runs. No second candidate was a better fit for this lesson. The 2018 Net Ninja episode on NonNull is older and tied to a different stack.
Where this fits
Fields and lists can now say what they hold and when they can be empty. Next: a type for a small fixed set of values. Next: enums.
Check yourself
What is the difference between [Int!] and [Int]!?
[Int!] is a list that may be null but cannot contain null items, while [Int]! is a list that is never null but may contain null items.
A field User.name is String! and its resolver returns null. What does the client receive for { user { name nick } }?
An error at the path user.name and user as null, because the null propagates to the nearest nullable parent and the sibling nick disappears with it.
What happens if you pass the single value 1 for an argument of type [Int!]!?
It is accepted, because GraphQL input coercion wraps a single value into a one-element list.
Why is it risky to mark a field non-null when its data source is unreliable?
Any failure that returns null on it nulls out the parent object and can take down large parts of the response.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.