简体   繁体   English

如何在 GraphQL 中表达输入验证

[英]How to express input validations in GraphQL

I'm wondering how to express input types validation in the GraphQL Schema, for example:我想知道如何在 GraphQL Schema 中表达输入类型验证,例如:

input DateFormat {
  format: String!
}
  • How can I express that format should be 10 chars max for example u other validations.我如何表达该format最多应为 10 个字符,例如其他验证。
  • Is this possible to implement using GraphQL-core 3 for python?这是否可以使用python 的 GraphQL-core 3来实现?

Thanks in advance提前致谢

I think there are two possibilities:我认为有两种可能:

  1. Create a custom scalar.创建自定义标量。 Scalars can do input validation in the parseLiteral and parseValue functions.标量可以在parseLiteral 和 parseValue函数中进行输入验证。 Since GraphQL-core 3 seems to be a port of the JS library, it should be possible to do it with the library.由于 GraphQL-core 3 似乎是 JS 库的一个端口,所以应该可以使用该库来实现。 But creating a new Scalar is quite a lot of work and does not necessarily offer a greater developer experience.但是创建一个新的 Scalar 是一项相当多的工作,并不一定能提供更好的开发人员体验。 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.以下是 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另见这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM