I’m spending a lot (read: “waaay to many”) time learning GraphQL. One of the things I had to figure out was how to use nullable types inside my GraphQL schema.
By default when you use a nullable type inside your GraphQL schema, you get the following error message:
ArgumentOutOfRangeException: Explicitly nullable type: Nullable<DateTime> cannot be coerced to a non nullable GraphQL type.
To fix this, you have to expliclity specify the field as nullable in you GraphQL type definition:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductType:ObjectGraphType<Product> | |
{ | |
public ProductType() | |
{ | |
Field(m => m.Id, type : typeof(IdGraphType)); | |
Field(m => m.Description); | |
Field(m => m.NullableDate, nullable:true); | |
} | |
} |