Resolvers
Field nullability
On this page
By default, Gratos makes every field nullable, following GraphQL best practices. Because fields are nullable by default, an error thrown by a resolver function or method is caught by the GraphQL executor and returned as a null value, with the error in the response.
That keeps requests as resilient as they can be: a single error does not take down the whole query, only the field that threw.
If you find this pervasive nullability makes building client applications frustrating, you may want to enable Strict semantic nullability.
@killsParentOnException
Some fields need to be non-nullable, though, an id for example. Add the docblock tag @killsParentOnException to such a field. The field is then typed as non-nullable, but it comes at a price: if the resolver throws, the error bubbles up to the first nullable parent.
Caution
If @killsParentOnException is used too liberally, small errors can take down huge portions of your query.
/** @gqlType */
class MyType {
/**
* @gqlField
* @killsParentOnException
*/
myField(): string {
if (Math.random() > 0.5) {
throw new Error("Bang");
}
return "Whew!";
}
}type MyType {
myField: String!
}Changing the default
To change this behavior, set the config option "nullableByDefault": false.
Danger
Disabling nullableByDefault is equivalent to marking all non-nullable fields as @killsParentOnException.