0

I am trying to make a custom resolver in VTL that will remove a certain string from a dynamodb list. I was trying to use $util.list.copyAndRemoveAll to remove the friend from the list. But I am not quite sure how to use it and the documentation isn't very clear to me and I don't even know if I am using the function correctly within VTL. https://docs.aws.amazon.com/appsync/latest/devguide/list-helpers-in-util-list.html

Thank you in advance.

GraphQL mutation

type Mutation {
    removeFromIncomingFriendList(pk: String!, sk: String!, friend: String!): String!
}

#user_incoming_friend_requests is a list of usernames of users that have sent friend requests.

{
    "version" : "2018-05-29",
    "operation" : "UpdateItem",
    "key" : {
        "pk" : { "S" : "USER#USERNAME" },
        "sk" : { "S" : "METADATA#USERNAME" },
    },
    "update": {
        "expression" : "SET #user_incoming_friend_requests = :newList ADD version :plusOne",
        "expressionNames": {
            "#user_incoming_friend_requests" : "user_incoming_friend_requests",
        },
        "expressionValues" : {
          ":newList": $util.list.copyAndRemoveAll("#user_incoming_friend_requests", [${context.arguments.friend}])
          ":plusOne" : {"N":1}
        }
    }
}

1 Answers1

0

I've found a solution for this under the post Add or remove an entry from a List type attribute in a DynamoDB table item

Or you can check out the official documentation here https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.REMOVE

These explain that you need the index of the value you want to remove, index. Then the update expression will be REMOVE user_incoming_friend_requests[index]

The part I am still unsure about, and maybe someone can help clarify, is the best way to find the index for the value we want to remove. It looks like you are already using versions to maintain data accuracy, so you should be able to find the index client side and pass it when you call the api. Another possible solution would be to use a pipeline resolver and query for the index in a linked resolver just prior to the resolver that removes the value.