-
Notifications
You must be signed in to change notification settings - Fork 45
How can I return an error to the client with a mutation? #64
Comments
Did a bunch of tracking down and When using |
@aweiker I saw the same thing. I finally came up with a solution. defmodule GraphQL.InvalidChangesetError do
@moduledoc """
Exception raised when Ecto Changeset is invalid.
"""
defexception plug_status: 422, message: "invalid changeset", changeset: nil, errors: %{}
@doc false
def exception(opts) do
changeset = Keyword.fetch!(opts, :changeset)
errors = changeset.errors
%GraphQL.InvalidChangesetError{
message: "invalid changeset for (#{changeset.data.__struct__}): #{inspect errors}",
changeset: changeset,
errors: errors
}
end
end These are errors that have to do with uniqueness & querying the db. Then added the following to my def render("422.json", assigns) do
{_, _, _, stack} = hd(assigns[:stack])
{field, message} = hd(assigns[:reason].errors)
%{errors: [%{message: "#{field} #{message}",
file: to_string(stack[:file]),
line: stack[:line]}]}
end Now whenever I need to produce an error to the client I raise GraphQL.InvalidChangesetError, changeset: changeset This now returns an appropriate error object that Relay can understand. |
Related to #56 |
Any status on this? From reading the spec, if we can't bubble errors during query execution, then the library isn't spec compliant. |
Whenever I execute a mutation I can never seem to produces errors as stated here: https://facebook.github.io/graphql/#sec-Errors
How do I produce errors?
The text was updated successfully, but these errors were encountered: