0

我已经使用 postgresql 作为数据源创建了 appsync api。现在使用 graphql 创建、更新和删除任何记录都非常简单。但我想要这样的东西,每当发生任何更新时,我只能更新该特定列的值。例如,让我们拥有包含 3 个字段的用户表:

user_id
name
city

现在,当我们为用户更新城市时,应该只更新城市:

mutation update{ updateUser(input:{user_id:"xxxxx",city:"xyz"}){ user_id name city }}

用户更新输入:

input updateUserInput{
user_id!
name
city

}

有没有办法在 vtx 模板中执行此操作,或者我们必须使用 lambda 作为数据源然后执行此操作。

4

1 回答 1

0

我想出了我们如何实现它:
你可以这样做:

#set ($updates = "")
#foreach( $item in $ctx.args.input.keySet())
    #if($item)
        #set ($updates = "$updates, $item = '$ctx.args.input.get($item)'")
    #end
#end
--------used cognito as authentication , so user id will come in indentity
#set($user_id = ($ctx.identity.username))  
#set($updates = "$updates, updated_at = '$util.time.nowFormatted('yyyy-MM-dd HH:mm:ssZ')'")
{
"version": "2018-05-29",
"statements": [
    $util.toJson("update public.tableName SET $updates.substring(1)  WHERE user_id = '$user_id'"),
    $util.toJson("select * from public.tableName WHERE user_id = '$user_id'")
]
}

我希望它会帮助某人。

于 2020-05-08T13:26:53.607 回答