Return Value
Return Value
Schema of primitive type query should be returning the entire (full integrity) of a node. e.g. Book
should return all fields relevant to Book
node.
Schema can define a field as required
in the query, but it is possible the server side may have defined it as any arbitrary return value.
- For example, when a field is required, but a server side hasn't completed implementation, the server side could have defined it to return
null
orundefined
despite the interface definition
This way, the type is spoofed
null
is an explicit return that the field exists and the record is empty (nullable type).
undefined
GraphQL typically treats undefined
in resolvers as a signal that the field does not exist, and it may result in an error
or unexpected behavior depending on the GraphQL server you are using (e.g., Apollo Server, GraphQL.js).
Every Query is its own type
Every Query is its own type
- Use the type that is delivered
Caveat with required fields
When you require
a field that is a primitive type, be aware that primitive type on the field may have its required fields that are another primitive type (effectively, creating a complex and deep chain of required fields to satisfy the type expectation from codegen)
Required/Non-nullable Fields (!
)
Non-nullable field means that our server
always expects to return a non-null
value for the defined field.
type Author { name: String! # Can't return null books: [Book] }
If the field ends up returning a null
value from resolver function, GraphQL will trigger a GraphQL execution error
, letting the client
know that something has gone wrong.
Non-nullable field also applies to a list.
type Author { books: [Book!]! # This list can't be null AND its list *items* can't be null }
If the !
is inside the array, the list can't include items that are null
.
If the !
is outside the array, the list itself can't be null
.
[Node] # [], null, [null], and [{field: "someVal"}] [Node!] # [], null, [{field: "someVal"}] [Node]! # [], [null], [{field: "someVal"}] [Node!]! # [], [{field: "someVal"}]