【问题标题】:Aws Appsync Resolver : How to create a resolver to update item in list map (DynaMoDB)Aws Appsync 解析器:如何创建解析器以更新列表映射 (DynaMoDB) 中的项目
【发布时间】:2019-10-14 13:59:06
【问题描述】:

我在一个表中创建多个对象,我在这里有一个名为“用户”的示例 DynamoDB 表,其中包含字段(名字、姓氏、电子邮件、公司),有人可以知道如何创建解析器以在历史记录中添加新项目?谢谢。

{
  "id": "d7913a57-f458-4b81-8d6e-10f81e2b1dc2",
  "firstName": "John",
  "lastName": "Doe",
  "email": "johndoe@gmail.com",
  "companies" : [
      {
        "companyId": "6059be51-1e66-4f3a-9c8f-ced3bc89d562",
        "createdAt": 321321,
        "histories": [
          {
           "companyHistoryId": "dc9d1a57-fb82-4c25-a6cf-d27dfe114f8f",
           "content": "Company A was created.",
           "createdAt": 321321,
           "createdBy": "d7913a57-f458-4b81-8d6e-10f81e2b1dc2"
          }
        ],
     },
  ]
}

这是我用来添加新公司的解析器

{
    "version" : "2017-02-28",
    "operation" : "UpdateItem",
    "key" : {
        "id": { "S": "${context.arguments.id}" },
    },
    "update" : {
        "expression" : "SET companies = list_append(if_not_exists(companies, :emptyList), :newCompany)",
        "expressionValues" : {
            ":emptyList": { "L" : [] },
            ":newCompany" : { "L" : [
                { "M": {
                    "name": { "S" : "${context.arguments.name}" },
                    "createdAt": { "N" : "${context.arguments.createdAt}" },
                    "companyId": { "S": "$util.autoId()" },
                    "isActive": { "BOOL" : true },
                    "histories": { "L": [
                        { "M": {
                            "companyHistoryId": { "S": "$util.autoId()" },
                            "content" : { "S" : "${context.arguments.name} was created." },
                            "createdAt": { "N" : "${context.arguments.createdAt}" },
                            "createdBy": { "S": "${context.arguments.id}" }
                        }}
                    ] }
                }}
            ] },
        }
    }
}

【问题讨论】:

    标签: amazon-web-services graphql aws-appsync resolver


    【解决方案1】:

    试试这个:

    {
      "version" : "2017-02-28",
      "operation" : "UpdateItem",
      "key" : {
          "id": { "S": "${context.arguments.id}" },
      },
      "update" : {
          "expression" : "SET companies[$ctx.args.index].histories = list_append(if_not_exists(companies[$ctx.args.index].histories, :emptyList), :history)",
          "expressionValues" : {
              ":emptyList": { "L" : [] },
              ":history" : { "L": [
                  { "M": {
                      "companyHistoryId": { "S": "$util.autoId()" },
                      "content" : { "S" : "${context.arguments.name} was created." },
                      "createdAt": { "N" : "${context.arguments.createdAt}" },
                      "createdBy": { "S": "${context.arguments.id}" }
                  }}
              ] }
          }
      }
    }
    

    记得传递$ctx.args.index,这是companies项目的索引,histories将被更新。

    【讨论】:

      猜你喜欢
      • 2019-01-06
      • 2021-07-18
      • 2020-10-19
      • 2019-12-04
      • 2018-11-15
      • 2018-12-15
      • 2019-05-21
      • 2019-02-04
      • 2019-09-01
      相关资源
      最近更新 更多