-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Question: Form validation #179
Comments
My suggestion is to first perform form validation on the client. You'll be able to develop a much nicer user interface where invalidations are seen immediately upon typing and contextual advice for resolving the issue can be presented. Of course, it's also best to have form validation on the server as well to avoid performing actions on invalid data (though I would not recommend using a server error response as a way to update a UI). There are two ways of going about this:
|
@leebyron At FB, do you define specific types for say emails, a |
You mean as custom scalar types? We have a couple that we define, but actually for the most part we haven't needed them. I think one of the more interesting ones which we may end up defining in the spec is |
@leebyron Thanks, just as I thought! And yes, I also wonder the same as @grydstedt. How do you validate a field (e.g. an email)? |
Would be great if graphql would also serialize a thrown object that isn't an Error. An example might be a signup form that errors when the user exists. You'll want an overarching error |
I ran into this same issue. Coming from a legacy REST app, I used boom to generate errors that conformed to HTTP response codes, and used a universal schema (shared on the server and the client) to do form validation, so I wasn't repeating keystrokes. If validation failed, it would wind up being thrown like this...
... where
It was useful to share validation logic on the front and back, because:
In those cases, I wanted the server to respond with the same error format as used on the front-end, so it could be immediately compatible. To get that same concept back in GraphQL, I'm currently trying out graphql-errors (a guide for using it is available here) By default, it sends back a plain 'Internal Error' message, but there's a Hope that helps someone. |
Let's say you have a form which posts data to API server. The API server validates the input and returns JSON object. If the input is invalid an error objects
{errors: {field1: "is required"}}
is returned. How do we handle and serve these kind of errors when using GraphQL? How and where should data validation be implemented (should that be part of my GraphQL code, maybe inside each resolve function)?The text was updated successfully, but these errors were encountered: