Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$util.map.copyAndRemoveAllKeys is not working #106

Closed
yukitaka13-1110 opened this issue Jun 27, 2021 · 8 comments
Closed

$util.map.copyAndRemoveAllKeys is not working #106

yukitaka13-1110 opened this issue Jun 27, 2021 · 8 comments
Labels
wontfix This will not be worked on

Comments

@yukitaka13-1110
Copy link

yukitaka13-1110 commented Jun 27, 2021

I'm developing appsync with serverless-framewrok.
I use dynamodb so I implemented vtl resolver files.
But copyAndRemoveAllKeys is not working.

See the following example code.
This is a respose.vtl code.

#if($ctx.error)
    $util.error($ctx.error.message, $ctx.error.type)
#end
#set($messages = $ctx.result.items)
#set($mergedMessages = [])
#set($users = $ctx.prev.result.items)
#foreach($m in $messages)
  #set($uid = $m.uid)
  #foreach($user in $users)
    #if($uid == $user.id)
      #set($m = $util.map.copyAndRemoveAllKeys($m, ["uid"])) ## This line.
      $util.qr($m.put("user", $user))
      $util.qr($mergedMessages.add($m))
      #break
    #end
  #end
#end
#set($result = { "items": $mergedMessages })
$util.toJson($result)

This always returns null and no error occurred.
I deployed this code and query it.
Unfortunately, I could retrieve expected response, not null.

query

query MyQuery {
  listChatMessages(groupId: "group-001") {
    items {
      id
      postDate
      user {
        color
        groupId
        id
      }
    }
  }
}

AppSync console result

{
  "data": {
    "listChatMessages": {
      "items": [
        {
          "id": "group-001",
          "postDate": "2021-06-23T01:00:01Z",
          "user": {
            "color": "blue",
            "groupId": "group-001",
            "id": "user-001"
          }
        },
        {
          "id": "group-001",
          "postDate": "2021-06-23T01:02:01Z",
          "user": {
            "color": "red",
            "groupId": "group-001",
            "id": "user-002"
          }
        }
      ]
    }
  }
}

Local AppSync Simulator

{
  "data": {
    "listChatMessages": null
  }
}

Is this bug?

Environment

"dependencies": {
    "serverless-appsync-plugin": "^1.11.3",
    "serverless-appsync-simulator": "^0.17.0",
    "serverless-dynamodb-local": "^0.2.39",
    "serverless-offline": "^7.0.0",
    "webpack": "^5.40.0",
    "webpack-cli": "^4.7.2",
    "webpack-node-externals": "^3.0.0"
  }
@yukitaka13-1110 yukitaka13-1110 changed the title copyAndRemoveAllKeys is not working $util.map.copyAndRemoveAllKeys is not working Jun 27, 2021
@bboure
Copy link
Member

bboure commented Jul 6, 2021

@yukitaka13-1110 This is a known issue in amplify-cli

I have noticed it too and haven't had time to fix it.
a PR/issue should be opened in amplify-cli.

It happens because ["uid"] is not transformed into a JavaArray

In the meantime, here is the workaround I use:

#set($remove=["uid"])
#set($m = $util.map.copyAndRemoveAllKeys($m, $remove)) ## This works

@yukitaka13-1110
Copy link
Author

@bboure Thank you for your response!
I'll try it.

Additionally, I found $HashMap.size() is not working with the following error.

Error: Missing request mapping template.

Do you have any idea about this?

@yukitaka13-1110
Copy link
Author

@bboure I confirmed my code worked correctly in AppSync console query.

@yukitaka13-1110
Copy link
Author

@bboure Sample code.

#set($obj = {})
#set($count = -1)

#set($count = $obj.size())

Local simulator: Missing request mapping template errror
AppSync Console: no error

@bboure
Copy link
Member

bboure commented Jul 9, 2021

Thanks @yukitaka13-1110
Is that the full template? If so, it resolves to an empty template. I am not sure un what case that would happen? (maybe in pipelines??)

I know by experience that empty templates don't work, but I thought it was an AppSync, not simulator.
Templates should usually be JSON parsable. If the template does not return anything, I usually return {}

I will have a try at this. In any case, that would also be an issue in amplify-cli that we could fix there.

@yukitaka13-1110
Copy link
Author

yukitaka13-1110 commented Jul 9, 2021

@bboure I found node version v16.0.0 caused this error.
Change to v12.0.0 then error disappeared but size() !== actual.

See this.

#set($records = [
  { "id": "id-001"},
  { "id": "id-002"},
  { "id": "id-001"},
  { "id": "id-002"},
])

#set($obj = {})
#foreach($record in $records)
    #if($util.isNull($obj[$record.id]))
        $util.qr($obj.put($record.id, 1))
    #else
        #set($count = $obj[$record.id] + 1)
        $util.qr($obj.put($record.id, $count))
    #end
#end

## $obj = \":{\"id-001\":1,\"id-002\":1}
## $obj.size() -> 4 but expected 2

I think this occurs when put same key twice.

#set($records = [
  { "id": "id-001"},
  { "id": "id-002"},
])

#set($obj = {})
#foreach($record in $records)
    #if($util.isNull($obj[$record.id]))
        $util.qr($obj.put($record.id, 1))
    #else
        #set($count = $obj[$record.id] + 1)
        $util.qr($obj.put($record.id, $count))
    #end
#end

## $obj = \":{\"id-001\":1,\"id-002\":1}
## $obj.size() -> 2 expected 2

@yukitaka13-1110
Copy link
Author

@bboure In AppSync console, I got expected result.

#set($records = [
  { "id": "id-001"},
  { "id": "id-002"},
  { "id": "id-001"},
  { "id": "id-002"}
])

#set($obj = {})
#foreach($record in $records)
    #if($util.isNull($obj[$record.id]))
        $util.qr($obj.put($record.id, 1))
    #else
        #set($count = $obj[$record.id] + 1)
        $util.qr($obj.put($record.id, $count))
    #end
#end
#set($count = $obj.size())
#set($result = { "obj": $obj, "count": $count })
$util.error($util.toJson($result), "debug")
"{\"obj\":{\"id-001\":2,\"id-002\":2},\"count\":2}

@stale
Copy link

stale bot commented Sep 7, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants