6

I'm wondering how to express input types validation in the GraphQL Schema, for example:

input DateFormat {
  format: String!
}
  • How can I express that format should be 10 chars max for example u other validations.
  • Is this possible to implement using GraphQL-core 3 for python?

Thanks in advance

1 Answer 1

5

I think there are two possibilities:

  1. 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.

  2. 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

2
  • Seems good. I was wondering there was something already part of the language specification. For example, for strings I'd like to automatically define in schema the maximum length allowed (String![max:10]). Or sth like that.
    – Cheche
    Commented Jul 2, 2020 at 21:00
  • No, there is no such thing. The thing that might come closest to this are directives. But I would be careful with overusing directives.
    – Herku
    Commented Jul 3, 2020 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.