Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we return null for a boolean value in GraphQL response

Tags:

graphql

I have a use case where the value can be "yes", "no" or "null" where "null" means "not present". Can I use the boolean type in GraphQL to denote this?

like image 669
rishabhjainps Avatar asked Sep 02 '25 15:09

rishabhjainps


1 Answers

From the spec:

By default, all types in GraphQL are nullable; the null value is a valid response for all [types]. To declare a type that disallows null, the GraphQL Non‐Null type can be used.

In other words, unless you explicitly specify your field as non-null, null will always be a valid value for that field, regardless of whether the field is a boolean, another scalar type or an object type.

+----------+-------------------+
|   type   |  allowed values   |
+----------+-------------------+
| Boolean  | true, false, null |
| Boolean! | true, false       |
+----------+-------------------+
like image 162
Daniel Rearden Avatar answered Sep 04 '25 08:09

Daniel Rearden