Skip to the workspace
Pebellslearn by building
The Schema  ›  Lesson

Objects, Lists and Non-Null

Objects 14 minObject types group typed fields that may take their own arguments, list and non-null wrappers combine to say whether a list and its items can be null, and a null in a non-null position moves up to the nearest nullable parent.

graphql / Objects, Lists and Non-Null

Explore each scene at your own pace.

01 · The rule

The rule

Objects, Lists and Non-Null

A book has one author and many reviews. Some books have no reviews yet; a book without an author should never exist. A schema captures all of that in a few characters, and reading those characters correctly tells you exactly what null the client might ever see.

The rule

! means never null; read list types from the outside in.

An object type is a named set of fields, each with a type and optionally its own arguments:

type Book {
  id: ID!
  title: String!
  genre: Genre!
  author: Author!
  reviews(first: Int = 5): [Review!]!
  subtitle: String
}
  • subtitle: String may be null (many books have none).
  • author: Author! is never null.
  • reviews(first: Int = 5) is a field argument with a default; reviews { rating } returns up to five.

For lists, the two positions of ! mean different things. Read from the outside in: first "can the list be null?", then "can an item be null?"

Type List null? Item null? Allowed values
[Review] yes yes null, [], [r1, null]
[Review]! no yes [], [r1, null]
[Review!] yes no null, [], [r1]
[Review!]! no no [], [r1, r2]

[Review!]! is the usual choice for "zero or more reviews": the client can loop without null checks. An empty list is still allowed.

A ! is a promise, and if a resolver breaks it, GraphQL does not send the forbidden null. Instead, the null moves up to the nearest parent that is allowed to be null, and an error explains why. If Book.author fails for b1, then book itself becomes null, because Query.book returns a nullable Book. Mark fields non-null when they truly always exist; overusing ! lets one failure blank out large parts of a response.

One convenience on input: if an argument expects a list, such as ids: [ID!]!, and you pass a single value "b1", it is treated as ["b1"].

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.