Non-Null

By default, every GraphQL field can return either its declared type or null. The non-null modifier (!) tells clients that a field will never be null. If a resolver returns null for a non-null field, the execution engine raises an error rather than sending unexpected null values to clients.

GraphQL
type User {
  name: String!
  bio: String
}

In this schema, name always has a value. The bio field may be null.

Implicit Nullability from C# Types#

Hot Chocolate infers nullability from your C# types. When nullable reference types (NRT) are enabled in your project, the mapping is straightforward.

Value Types#

Value types are non-null by default. Use ? to make them nullable.

C# typeGraphQL type
intInt!
int?Int
boolBoolean!
bool?Boolean

Reference Types (NRT Enabled)#

With NRT enabled (recommended), non-nullable references map to non-null GraphQL types.

C# typeGraphQL type
stringString!
string?String
UserUser!
User?User

Reference Types (NRT Disabled)#

Without NRT, all reference types are nullable by default. Hot Chocolate cannot distinguish string from string? because the compiler treats them identically.

C# typeGraphQL type
stringString
UserUser

We strongly recommend enabling NRT. It provides accurate schema nullability without extra attributes and catches null-related bugs at compile time.

Enabling Nullable Reference Types#

Add the following to your .csproj file to enable NRT across the project:

XML
<PropertyGroup>
    <Nullable>enable</Nullable>
</PropertyGroup>

You can also enable it per file with #nullable enable at the top of the file.

Explicit Nullability#

When you need to override the inferred nullability, use attributes or the descriptor API.

Non-Null List Items#

Lists have two layers of nullability: the list itself and its items. With NRT enabled:

C# typeGraphQL type
List<string>[String!]!
List<string>?[String!]
List<string?>[String]!
List<string?>?[String]

To override nullability on list items explicitly:

Both produce genres: [String!] in the schema.

Next Steps#

Edit this page on GitHub
Last updated on by Tobias Tengler