0

I have created appsync api with postgresql as datasource. Now it's very straight to create , update and delete any record with graphql. But i want something like, whenever any update takes places , i can update value of that particular column only. Eg lets we have user table with 3 fields:

user_id
name
city

now when we update city for user than only city should update:

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

user update input:

input updateUserInput{
user_id!
name
city

}

Is there any way to do this in vtx template or we have to use lambda as datasource and then do this.

1 Answers1

1

I figured it out how we can achieve it:
you can do like this:

#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'")
]
}

I hope it will help someone.