I'm wondering how to express input types validation in the GraphQL Schema, for example:
input DateFormat {
format: String!
}
- How can I express that
formatshould be 10 chars max for example u other validations. - Is this possible to implement using GraphQL-core 3 for python?
Thanks in advance
I think there are two possibilities:
Create a custom scalar. Scalars can do input validation in the parseLiteral and parseValue functions. Since GraphQL-core 3 seems to be a port of the JS library, it should be possible to do it with the library. But creating a new Scalar is quite a lot of work and does not necessarily offer a greater developer experience. The scalar has to be documented in the same was a simple field would have to be documented. In practice, this means that seldomly a new scalar type is used for specific formats. Here are some examples in JavaScript.
Simply document the validation in the description of the field. If the strings are only constructed by developers it might be okay to throw an error in the resolver. If the validation concerns the end user I would encourage a mutation response payload, that contains errors as results. See also this article