Appsync Resolver UpdateItem 忽略空参数?

ryg*_*go6 2 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, description = :description",
        "expressionNames": {
            "#name" : "name"
        },
        "expressionValues": {
            ":name" : { "S": "${context.arguments.name}" },
            ":description" : { "S": "${context.arguments.description}" },
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Koi*_*Dev 6

所有你需要做的就是创建自己SET expressioncondition checked根据您的需要。下面的表达式检查是否有任何参数null or 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

希望有用!