How can I transform a simple GraphQL schema, which I can access server-side by importing and client-side by querying, into validation rules?
I can kind of see how I could do this for enums, lets say I have a title:
enum Title {
Mr, Ms, Mrs, Dr
}
I could access the possible values by querying
__type (name: "Title") {
name
enumValues {
name
}
}
And now I can create a drop-down on the client or a validation on the server, but how do I encode and access for example the min/max length of a string field? Or min/max value for a number?
Do I need to add a custom type for each field? Or is, there a better way?
[edit] in response to Daniel's answer:
I'm using yup for validation, which is a great library, but for example if a certain field was required on a gql InputType, how do I get that into the yup schema?
And how do I use that yup schema on the front-end? Ideally I would like to serialise it, send it to the client, and reconstruct the validation there.
The only other way I can think of is to share the code, but then I need to re-build and re-deploy the client each time the schema changes, which is of course out of the question :/