Lists and Non-Null Types
GraphQL type modifiers let you express collection and nullability constraints.
The [] syntax represents a list and ! represents Non-Null.
Consider these types:
users: [User]
users: [User]!
users: [User!]
users: [User!]!
They are not equivalent.
[User]
The list may be null, and individual elements may also be null.
[User]!
The list itself cannot be null, but individual elements may be null.
[User!]
The list may be null, but every element that exists must be a User rather than null.
[User!]!
Neither the list nor its elements can be null.
This distinction is important when designing reliable APIs. Nullability is part of the schema contract and affects validation, execution, generated types, and client behavior.
For example:
type Query {
users: [User!]!
}
communicates that a successful resolver result should always be a list and that every element should be a valid non-null User.
Non-null does not mean that the underlying database can never contain missing data. It means the GraphQL field contract promises that a null value cannot be returned at that location during successful execution. If a resolver violates a non-null contract, GraphQL can propagate null upward according to its execution rules and report an error.
Nullable list, nullable elementsNon-null list, nullable elementsNullable list, non-null elementsNon-null list, non-null elements- Square brackets represent lists.
- The exclamation mark represents Non-Null.
- List nullability and element nullability are independent.
- Nullability is an important part of API design.
What does `[User!]!` mean?