Aws Appsync 解析器:如何创建解析器以更新列表映射 (DynaMoDB) 中的项目
Posted
技术标签:
【中文标题】Aws Appsync 解析器:如何创建解析器以更新列表映射 (DynaMoDB) 中的项目【英文标题】:Aws Appsync Resolver : How to create a resolver to update item in list map (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"
]
] ,
【问题讨论】:
【参考方案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
将被更新。
【讨论】:
以上是关于Aws Appsync 解析器:如何创建解析器以更新列表映射 (DynaMoDB) 中的项目的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Aws AppSync 将 JWT 令牌(或任何其他变量)从父解析器传递给子解析器
AWS AppSync Lambda 解析器获取查询返回类型