Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validations that cannot be ran on the client side and the errors object #117

Closed
D1plo1d opened this issue Nov 6, 2015 · 6 comments
Closed

Comments

@D1plo1d
Copy link

D1plo1d commented Nov 6, 2015

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 [email protected] has already been taken"],
      "locations": []
    }
  }
}

Relevant Spec Section: https://facebook.github.io/graphql/#sec-Errors

@satahippy
Copy link

+1

1 similar comment
@sulliwane
Copy link

+1

@CatTail
Copy link

CatTail commented Dec 28, 2015

I was wondering how facebook handle error and display related error information for user with graphql?

@leebyron

@leebyron
Copy link
Collaborator

leebyron commented Jan 9, 2016

The data in the errors object is definitely typical for debugging. But of course the existence of an error could be used to render something in a UI about data not being available.

My suggestion for the example you bring up here is to allow for data for a user-facing report in the payload of your mutation. It's often the case that mutation payloads include a "didSucceed" field and a "userError" field. If your UI requires rich information about potential errors, then you should include this information in your payload as well. For example, maybe you want to highlight the email field red, you might include something like a boolean "wasBadEmail".

The general philosophy at play is that Errors are considered exceptional. Your user data should never be represented as an Error. If your users can do something that needs to provide negative guidance, then you should represent that kind of information in GraphQL as Data not as an Error. Errors should always represent either developer errors or exceptional circumstances (e.g. the database was offline).

@D1plo1d
Copy link
Author

D1plo1d commented Jan 9, 2016

Thanks @leebyron. didSucceed and userError should work for my usage.

It might be useful to spec out userErrors somewhere similar to how Relay specifies connections and edges without necessarily including them in the GraphQL language spec. I feel like user input error handling is as common a concern as pagination in my code. Would it make any sense to consider this for a future version of Relay?

@koistya
Copy link

koistya commented Mar 7, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants