简体   繁体   English

具有业务逻辑的AWS Appsync + DynamoDB

[英]AWS Appsync + DynamoDB with business logic

Is it possible to have business logic on my AppSync's mutation when the datasource is dynamoDB? 当数据源是dynamoDB时,是否有可能在我的AppSync突变上有业务逻辑?

I'm fairly new to GraphQL and Appsync. 我是GraphQL和Appsync的新手。 My understanding is when you're using plain GraphQL you can have business logic inside your resolver to validate before updating. 我的理解是,当您使用普通的GraphQL时,您可以在解析器中拥有业务逻辑,以便在更新之前进行验证。 How would you achieve the same thing with AWS AppSync when you pass it the GraphQL schema with DynamoDB as the datasource? 当您使用DynamoDB作为数据源传递GraphQL架构时,您将如何使用AWS AppSync实现相同的功能?

In AWS AppSync, you would attach a resolver to the field that is mapped to the DynamoDB DataSource. 在AWS AppSync中,您可以将解析程序附加到映射到DynamoDB DataSource的字段。 You can then use the Velocity template to have your custom business logic inside the resolver for any validations. 然后,您可以使用Velocity模板将自定义业务逻辑放在解析程序中以进行任何验证。

Please refer to the following documentation on the Resolver Mapping Template programming guide from AWS. 请参阅AWS的Resolver Mapping Template编程指南中的以下文档

Here is the mapping template reference for DynamoDB resolvers. 以下是 DynamoDB解析器的映射模板参考

According to AWS AppSync's Website : With AppSync, your app can access data in Amazon DynamoDB, trigger AWS Lambda functions, or run Amazon Elasticsearch queries . 根据AWS AppSync的网站通过AppSync,您的应用可以访问Amazon DynamoDB中的数据,触发AWS Lambda功能或运行Amazon Elasticsearch查询

You can think of it as a gateway for clients to access different backends (data sources), defined by mapping templates attached to GraphQL fields (resolvers). 您可以将其视为客户端访问不同后端(数据源)的网关,这些后端由附加到GraphQL字段(解析器)的映射模板定义。

AppSync supports DynamoDB and ElasitcSearch queries natively, but if you want to perform business logic you will have to add a AWS Lambda data source and then use AWS SDKs to R/W DynamoDB or anything else such as another API or maybe even an excel file! AppSync本身支持DynamoDB和ElasitcSearch查询,但是如果要执行业务逻辑,则必须添加AWS Lambda数据源,然后将AWS SDK用于R / W DynamoDB或其他任何其他API,甚至是excel文件!

Additionally, you can use Apache VTL along with AppSync's available helpers such as $context to help you perform authorization or field data access based on permissions. 此外,您可以将Apache VTL与AppSync的可用帮助程序(如$ context)一起使用,以帮助您根据权限执行授权或字段数据访问。 Keep in mind that your DynamoDB or ES resolver can only perform 1 operation at the end, Apache VTL only helps you build the resolver that will be run by AppSync. 请记住,您的DynamoDB或ES解析程序最后只能执行1个操作,Apache VTL只能帮助您构建将由AppSync运行的解析程序。

Here are two diagrams that compare a traditional approach vs appsync 以下是两个比较传统方法与appsync的图表

传统方法

的AppSync

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

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