This is documentation for v11, which is no longer actively maintained.
For up-to-date documentation, see the latest version.

Non-Null

Per default all fields on an object type can be either null or the specified type.

SDL
type User {
name: String
}

In the above example name can either be null or a String.

Being nullable does not make sense for every field though. Maybe we have some database constraint which enforces the name to never be null. GraphQL allows us to be specific about this, by marking a field as non-null.

SDL
type User {
name: String!
}

The exclamation mark (!) denotes that the field can never be null. This is also enforced by the execution engine. If we were to return a null value in the name resolver, the execution engine would throw an error. This prevents unexpected null values from causing issues in the consuming applications.

Implicit nullability

Hot Chocolate automatically infers the nullability of the schema type from the nullability of the used CLR type.

Value types are non-null per default, unless they have been marked as nullable.

CLR TypeSchema Type
intInt!
int?Int
Nullable<int>Int

Reference types are always nullable, unless we have enabled nullable reference types. With nullable reference types enabled all fields are non-null per default.

We strongly encourage the use of nullable reference types.

Explicit nullability

We can also be explicit about the nullability of our fields.

C#
public class Query
{
[GraphQLNonNullType]
public Book GetBook()
{
return new Book { Title = "C# in depth", Author = "Jon Skeet" };
}
}
public class Book
{
[GraphQLNonNullType]
public string Title { get; set; }
public string Author { get; set; }
}