首页 > 解决方案 > AppSync - creating nested mutation with array and objects?

问题描述

Does AppSync support nested single mutation?

I want to call a single mutation which will insert records into two tables, eg: User and Roles tables in DynamoDB.

Something like this for example:

createUser(
   input: {
      Name: "John"
      Email: "user@domain.com"
      LinesRoles: [
        { Name: "Role 1" }
        { Name: "Role 2" }
      ]
   }) {
        Id
        Name
        LinesRoles {
          Id
          Name
        }
      }

Do I need to create two resolvers in AppSync for User and Roles to insert the records in both tables?

标签: amazon-web-servicesgraphqlaws-appsync

解决方案


我可以想到三种方法来实现这一点:

  1. 使用BatchPutItem一次将记录保存到两个表中。但是,您将无法使用任何ConditionExpression
  2. 使用具有两个 AppSync 函数的管道解析器,其中一个函数将 PutItem 生成到Roles表中,另一个生成到User表中。但是,您需要对可能不一致的情况感到满意,即记录已插入到一个表中但未插入另一个表中。
  3. 使用Lambda解析器在 DynamoDB 事务中写入 2 个表。

推荐阅读