Description
Let's say you have a uniqueness constraint on a field in a mutation. For example the email
field must not match any existing email in the database.
In graphql/graphql-js#179 it's been suggested that we shouldn't use the server to validate our forms. But in the case of our uniqueness constraint the server is the only place with enough information to determine if our form is valid.
The format of the errors response appears to be aimed at debugging. Specifically a line number is given as the only context for the error. Presumably this is so that a programmer can then use that line number to find the issue in their query/mutation. So to me the current errors array looks more analogous to a stack trace then an error object like we'd traditionally expect in a RESTful API (eg. {"errors": {"email": ["this email has been taken"]}}
).
The advantage of the error object is that we can easily match the keys in the error to the names of the fields in our form to add an error message for our users next to the offending field.
I propose that changing the errors array to an errors object would give developers access to field names and error messages in a format that is easy to map on to their forms and display to users. Eg:
{
"errors": {
"email": {
"messages": ["The email address zuckerberg@facebook.com has already been taken"],
"locations": []
}
}
}
Relevant Spec Section: https://facebook.github.io/graphql/#sec-Errors