AppSync 中的 GraphQL 字段级验证

GraphQL field-level validation in AppSync

我有一个主要由 DynamoDB 存储支持的 AppSync API。大多数解析器直接连接到 DynamoDB 源,而不是使用 lambda。

一些字段应该有验证约束,例如长度或正则表达式。在一种特殊情况下,我想要求 state 字段包含一个 ISO 3166-2 值,如 US-NY。 (GraphQL 枚举值不能包含连字符,所以这不是这里的一个选项。)

除了用 lambda 替换一些解析器之外,我能想到的应用这些验证规则的唯一方法是在 RequestMappingTemplate 中的 VTL 中进行。那会起作用,但是会很乏味并且可能导致重复代码。有其他选择吗?

不幸的是,没有 lambda 的唯一方法是 VTL,我建议不要直接在 RequestMappingTemplate 中编写验证,而是使用管道解析器。(减少重复)

Pipeline Resolvers contain one or more Functions which are executed in order.

Functions allow you to write common logic for reuse across multiple Resolvers in your schema. They are attached directly to a data source and like a Unit resolver, contain the same request and response mapping template format.


你可以找到一个很好的例子 here.