module Err = struct
type t = | String of string | Extension of string * string
let message_of_error t = match t with
| String s -> s
| Extension _ -> ""
let extensions_of_error t = match t with
| String _ -> []
| Extension (k, v) -> [(k, `String v)]
end
module CustomErrorsSchema = Graphql_schema.Make (struct
type +'a t = 'a
let bind t f = f t
let return t = t
module Stream = struct
type 'a t = 'a Seq.t
let map t f = Seq.map f t
let iter t f = Seq.iter f t
let close _t = ()
end
end) (Err)
Adds support for building a schema with a custom error type.
This allows users of ocaml-graphql-server to add extra error information in the extensions field of the error response.
OneGraph is using this in production to provide more context in the errors that we display to the user. For example, we provide hints if the user is not logged in (try https://www.onegraph.com/graphiql?shortenedId=41WMZX) and nicely format json errors from upstream APIs (try https://www.onegraph.com/graphiql?shortenedId=VJSMV7).
There is a test with an example schema module:
Example resolver: