Amazon web services Appsync解析器UpdateItem忽略空参数?
我正在我的Appsync解析器中执行此操作:Amazon web services Appsync解析器UpdateItem忽略空参数?,amazon-web-services,amazon-dynamodb,aws-appsync,Amazon Web Services,Amazon Dynamodb,Aws Appsync,我正在我的Appsync解析器中执行此操作: { "version" : "2017-02-28", "operation" : "UpdateItem", "key" : { "pk" : { "S" : "Container" }, "id" : { "S" : "${ctx.args.id}" } }, "update" : { "expression" : "SET #name = :name, de
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"pk" : { "S" : "Container" },
"id" : { "S" : "${ctx.args.id}" }
},
"update" : {
"expression" : "SET #name = :name, description = :description",
"expressionNames": {
"#name" : "name"
},
"expressionValues": {
":name" : { "S": "${context.arguments.name}" },
":description" : { "S": "${context.arguments.description}" },
}
}
}
但有时我可能无法同时传递姓名和描述。当这些参数为空时,如何使其不设置这些列?这是非常可能的。您只需添加一个简单的if语句来检查值是否存在。在这里的文档中可以看到一个类似的示例: 具体地说,下面的示例将可选参数应用到列表操作中
{
“版本”:“2017-02-28”,
“操作”:“扫描”
#if(${context.arguments.count})
,“限制”:${context.arguments.count}
#结束
#if(${context.arguments.nextToken})
,“nextToken”:“${context.arguments.nextToken}”
#结束
}
只要应用if的null检查就可以了。您需要做的就是根据需要创建自己的
SET expression
,并选中条件。下面的表达式检查是否有任何参数为null或空
,我不想更新它
#set( $expression = "SET" )
#set( $expValues = {} )
## NAME
#if( !$util.isNullOrEmpty(${context.arguments.name}) )
#set( $expression = "${expression} name = :name" )
$!{expValues.put(":name", { "S" : "${context.arguments.name}" })}
#end
## DESCRIPTION
#if( !$util.isNullOrEmpty(${context.arguments.description}) )
#if( ${expression} != "SET" )
#set( $expression = "${expression}," )
#end
#set( $expression = "${expression} description = :description" )
$!{expValues.put(":description", { "S" : "${context.arguments.description}" })}
#end
{
"version" : "2017-02-28",
"operation" : "UpdateItem",
"key" : {
"pk" : { "S" : "Container" }
"id" : { "S" : "${context.arguments.id}" }
},
"update" : {
"expression" : "${expression}",
"expressionValues": $util.toJson($expValues)
}
}
希望它有用