aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 821 forks source link

Is supported BatchDeleteItem for custom resolver? #1988

Closed iparrabb closed 5 years ago

iparrabb commented 5 years ago

Hello everybody!

I have a problem with a custom resolver using BatchDeleteItem operation.

mutation:

type Mutation {
    batchDeleteDocuments(ids: [ID]): [Document]
}

resolver

"BatchDeleteDocumentsResolver": {
    "Type": "AWS::AppSync::Resolver",
    "Properties": {
        "ApiId": {
            "Ref": "AppSyncApiId"
        },
        "DataSourceName": "DocumentTable",
        "TypeName": "Mutation",
        "FieldName": "batchDeleteDocuments",
        "RequestMappingTemplateS3Location": {
            "Fn::Sub": [
                "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.batchDeleteDocuments.req.vtl",
                {
                    "S3DeploymentBucket": {
                        "Ref": "S3DeploymentBucket"
                    },
                    "S3DeploymentRootKey": {
                        "Ref": "S3DeploymentRootKey"
                    }
                }
            ]
        },
        "ResponseMappingTemplateS3Location": {
            "Fn::Sub": [
                "s3://${S3DeploymentBucket}/${S3DeploymentRootKey}/resolvers/Mutation.batchDeleteDocuments.res.vtl",
                {
                    "S3DeploymentBucket": {
                        "Ref": "S3DeploymentBucket"
                    },
                    "S3DeploymentRootKey": {
                        "Ref": "S3DeploymentRootKey"
                    }
                }
            ]
        }
    }
}

request:

#set($ids = [])
#foreach($id in ${ctx.args.ids})
  #set($map = {})
  $util.qr($map.put("id", $util.dynamodb.toString($id)))
  $util.qr($ids.add($map))
#end

{
  "version" : "2018-05-29",
  "operation" : "BatchDeleteItem",
  "tables" : {
    "DocumentTable": $util.toJson($ids)
  }
}

response:

$util.toJson($ctx.result)

error: image

Is it my mistake or is not supported the BatchDeleteItem operation?

I can see that the actions not allow this operation:

"Action": [
    "dynamodb:BatchGetItem",
    "dynamodb:BatchWriteItem",
    "dynamodb:PutItem",
    "dynamodb:DeleteItem",
    "dynamodb:GetItem",
    "dynamodb:Scan",
    "dynamodb:Query",
    "dynamodb:UpdateItem"
],

How can support be added to this need?

Thanks!

iparrabb commented 5 years ago

I found my mistake thanks to the issue #1079, instead of:

#set($ids = [])
#foreach($id in ${ctx.args.ids})
  #set($map = {})
  $util.qr($map.put("id", $util.dynamodb.toString($id)))
  $util.qr($ids.add($map))
#end

{
  "version" : "2018-05-29",
  "operation" : "BatchDeleteItem",
  "tables" : {
    "DocumentTable": $util.toJson($ids)
  }
}

must be:

#set($ids = [])
#foreach($id in ${ctx.args.ids})
  #set($map = {})
  $util.qr($map.put("id", $util.dynamodb.toString($id)))
  $util.qr($ids.add($map))
#end

{
  "version" : "2018-05-29",
  "operation" : "BatchDeleteItem",
  "tables" : {
    "Document-irlvgawxhbekzpuxpzjwg4o57y-dev": $util.toJson($ids)
  }
}

It is confusing :(, the TableName really is DocumentTable, at least in AppSync, but on DynamoDB the TableName is Document-irlvgawxhbekzpuxpzjwg4o57y-dev, that's how it works.

SwaySway commented 5 years ago

Yea the issue the resolver needing the exact name of the table listed in dynamodb, though we are looking at some ways can address this.

artecoop commented 5 years ago

I tried to replicate this exact same behavior, but using the mock api. The code compiles but executing it does nothing. Is an expected behavior for local db for mocking?

kldeb commented 4 years ago

@SwaySway is there a way to address having the exact table name in the batch resolver?

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.