Non-Null
By default, all types in GraphQL are nullable; thenullvalue is a valid response for all of the above types. To declare a type that disallows null, the GraphQL Non‐Null type can be used. This type wraps an underlying type, and this type acts identically to that wrapped type, with the exception thatnullis not a valid response for the wrapping type. A trailing exclamation mark is used to denote a field that uses a Non‐Null type like this:name: String!
.
Nullable vs. Optional
Fields are_always_optional within the context of a query, a field may be omitted and the query is still valid. However fields that return Non‐Null types will never return the valuenullif queried.
Inputs (such as field arguments), are always optional by default. However a non‐null input type is required. In addition to not accepting the valuenull, it also does not accept omission. For the sake of simplicity nullable types are always optional and non‐null types are always required.
Result Coercion
In all of the above result coercions,nullwas considered a valid value. To coerce the result of a Non‐Null type, the coercion of the wrapped type should be performed. If that result was notnull, then the result of coercing the Non‐Null type is that result. If that result wasnull, then a field error must be raised.
Input Coercion
If an argument or input‐object field of a Non‐Null type is not provided, is provided with the literal valuenull, or is provided with a variable that was either not provided a value at runtime, or was provided the valuenull, then a query error must be raised.
If the value provided to the Non‐Null type is provided with a literal value other thannull, or a Non‐Null variable value, it is coerced using the input coercion for the wrapped type.
A non-null argument cannot be omitted.
{
fieldWithNonNullArg
}
The value {null} cannot be provided to a non-null argument.
{
fieldWithNonNullArg(nonNullArg: null)
}
A variable of a nullable type cannot be provided to a non-null argument.
query withNullableVariable($var: String) {
fieldWithNonNullArg(nonNullArg: $var)
}
The Validation section defines providing a nullable variable type to a non‐null input type as invalid.
Non‐Null type validation
- A Non‐Null type must not wrap another Non‐Null type.