Does graphql support non-nullable optional field?

Viewed 539

I am using graphql as the interface. When design the schema I can't find a way to support non-nullable optional fields. For example:

mutation {
  createUser (firstName: String!, lastName: String!, middleName: String): Void
}

for above mutation, I'd like to make the middlename as optional but also don't accept null value. Is it possible to do in graphql?

1 Answers

The graphql spec mentions that non-nullable input fields are always required.

https://github.com/graphql/graphql-spec/blob/main/spec/Section%203%20--%20Type%20System.md#input-objects

Quoting:

If no value is provided for a defined input object field and that field definition provides a default value, the default value should be used. If no default value is provided and the input object field's type is non-null, an error should be raised. Otherwise, if the field is not required, then no entry is added to the coerced unordered map.

Related