Type Errors
The GraphQL specification requires certain assumptions to hold true when executing a query. However, it’s possible that some code would violate that assumption, resulting in a type error.
Here are two type errors that you can customize in GraphQL-Ruby:
-
A field with
null: falsereturnednil -
A field returned a value as a union or interface, but that value couldn’t be resolved to a member of that union or interface.
You can specify behavior in these cases by defining a Schema.type_error hook:
class MySchema < GraphQL::Schema def self.type_error(err, query_ctx) # Handle a failed runtime type coercion end end
It is called with an instance of GraphQL::UnresolvedTypeError or GraphQL::InvalidNullError and the query context (a GraphQL::Query::Context).
If you don’t specify a hook, you get the default behavior:
-
Unexpected
nils add an error the response’s"errors"key -
Unresolved Union / Interface types raise
GraphQL::UnresolvedTypeError
An object that fails type resolution is treated as nil.