Handing Typecasting Errors

In some cases, runtime data can cause GraphQL execution to reach an invalid state:

You can specify behavior in these cases by defining a Schema#type_error hook:

MySchema = GraphQL::Schema.define do
  type_error ->(type_error, query_ctx) {
    # Handle a failed runtime type coercion
  }
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:

An object that fails type resolution is treated as nil.