amazon-web-services - Appsync Resolver UpdateItem 忽略空参数?

标签 amazon-web-services amazon-dynamodb aws-appsync

我在我的 Appsync Resolver 中执行此操作:

{
    "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}" },
        }
    }
}

但有时我可能不会同时传递名称和描述。当这些参数为空时,我如何使它不设置那些列?

最佳答案

您需要做的就是根据您的需要创建自己的 SET 表达式 并带有 condition 检查。下面的表达式检查任何参数是否为 null 或 empty,我不想更新它。

#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)
    }
}

希望有用!

关于amazon-web-services - Appsync Resolver UpdateItem 忽略空参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52230525/

相关文章:

amazon-web-services - SageMaker Train 作业未创建/opt/ml/input/data/training 目录

c# - 在 AWS SDK .net 中获取时间戳错误

ios - 使用 Cognito userId 将数据存储到 DynamoDB

javascript - 查询特定的GET字段?

aws-appsync - AppSync 的订阅是否仅限于一种特定用例?

mongodb - 可以在没有 dynamoDB 的情况下使用 AWS App-Sync

amazon-web-services - AWS API Gateway - 自定义域 - 由于并发修改而无法完成操作

json - "Encountered unsupported property Type"Route53 的 Cloudformation 错误

php - 如何扫描 DynamoDB 以查找列表中的文本?

python - 使用查询从 DynamoDB 检索所有项目?