amazon-web-services - AWS Amplify 自定义解析器 - 不支持的操作 'BatchPutItem'

标签 amazon-web-services amazon-dynamodb aws-amplify aws-appsync vtl

我正在遵循一些指南来使用 BatchPutItem 实现自定义解析器手术。目前我面临一些我无法解决的问题。

  • 错误输出

  • {
      "data": {
        "batchAddBusiness": null
      },
      "errors": [
        {
          "path": [
            "batchAddBusiness"
          ],
          "data": null,
          "errorType": "MappingTemplate",
          "errorInfo": null,
          "locations": [
            {
              "line": 2,
              "column": 3,
              "sourceName": null
            }
          ],
          "message": "Unsupported operation 'BatchPutItem'."
        }
      ]
    }
    

    这是必要的数据:
  • 架构

  • type Business 
      @model (subscriptions: { level: off })
      @auth (rules: [
        {allow: groups, groups: ["Admin"]},
        {allow: groups, groups: ["BusinessOwner"]}
      ])
      @key(fields: ["id", "createdAt"])
    {
      id: ID!
      name: String!
      phone: String!
      email: String!
      createdAt: String!
      hours: [OpenHours]
      specialHours: [SpecialHours]
      category: [BusinessCategory] @connection(keyName: "byBusiness", fields: ["id"])
      lists: [BusinessList] @connection(keyName: "byBusiness", fields: ["id"])
      tags: [BusinessTags] @connection(keyName: "byBusiness", fields: ["id"])
      users: [BusinessUsers] @connection(keyName: "byBusiness", fields: ["id"])
    }
    
    type Mutation {
      batchAddBusiness(business: [CreateBusinessInput]): [Business]
    }
    
  • 突变

  • mutation batchAdd {
      batchAddBusiness(business: [
      {
        id: "6726da58-a8af-4656-9a8a-9c7453455e28",
        name: "Company A",
        phone: "+12341235",
        email: "office@notexists2.com",
        createdAt: "2020-03-01"
      },
      {
        id: "6726da58-a8af-4656-9a8a-9c7453455e23",
        name: "Asdf",
        phone: "+12341235",
        email: "office@notexists.com",
        createdAt: "2020-03-02"
      }
      ]) {
        id name phone email createdAt
      }
    }
    
  • Mutation.batchAddBusiness.req.vtl
  • ## [Start] Determine request authentication mode **
    #if( $util.isNullOrEmpty($authMode) && !$util.isNull($ctx.identity) && !$util.isNull($ctx.identity.sub) && !$util.isNull($ctx.identity.issuer) && !$util.isNull($ctx.identity.username) && !$util.isNull($ctx.identity.claims) && !$util.isNull($ctx.identity.sourceIp) && !$util.isNull($ctx.identity.defaultAuthStrategy) )
    #set( $authMode = "userPools" )
    #end
    ## [End] Determine request authentication mode **
    ## [Start] Check authMode and execute owner/group checks **
    #if( $authMode == "userPools" )
        ## [Start] Static Group Authorization Checks **
        #set($isStaticGroupAuthorized = $util.defaultIfNull(
                    $isStaticGroupAuthorized, false))
        ## Authorization rule: { allow: groups, groups: ["Admin"], groupClaim: "cognito:groups" } **
        #set( $userGroups = $util.defaultIfNull($ctx.identity.claims.get("cognito:groups"), []) )
        #set( $allowedGroups = ["Admin"] )
        #foreach( $userGroup in $userGroups )
            #if( $allowedGroups.contains($userGroup) )
            #set( $isStaticGroupAuthorized = true )
            #break
            #end
        #end
        ## Authorization rule: { allow: groups, groups: ["BusinessOwner"], groupClaim: "cognito:groups" } **
        #set( $userGroups = $util.defaultIfNull($ctx.identity.claims.get("cognito:groups"), []) )
        #set( $allowedGroups = ["BusinessOwner"] )
        #foreach( $userGroup in $userGroups )
            #if( $allowedGroups.contains($userGroup) )
            #set( $isStaticGroupAuthorized = true )
            #break
            #end
        #end
        ## [End] Static Group Authorization Checks **
    
        ## No Dynamic Group Authorization Rules **
        ## No Owner Authorization Rules **
    
        ## [Start] Throw if unauthorized **
        #if( !($isStaticGroupAuthorized == true || $isDynamicGroupAuthorized == true || $isOwnerAuthorized == true) )
            $util.unauthorized()
        #end
        ## [End] Throw if unauthorized **
    #end
    ## [End] Check authMode and execute owner/group checks **
    
    #set($businessdata = [])
    #foreach($item in ${ctx.args.business})
        $util.qr($item.put("createdAt", $util.time.nowISO8601()))
        $util.qr($item.put("updatedAt", $util.time.nowISO8601()))
        $util.qr($item.put("__typename", "Business"))
        $util.qr($item.put("id", $util.defaultIfNullOrBlank($item.id, $util.autoId())))
        $util.qr($businessdata.add($util.dynamodb.toMapValues($item)))
        ## $util.qr($ctx.stash.put("debugthis", $businessdata))
    #end
    {
      "version": "2018-05-29",
      "operation": "BatchPutItem",
      "tables": {
          "Business-asdf1234-dev": $utils.toJson($businessdata)
      }
    }
    
  • Mutation.batchAddBusiness.res.vtl
  • #if ($ctx.error)
        $util.appendError($ctx.error.message, $ctx.error.type, null, $ctx.result.data.unprocessedKeys)
    #end
    $util.toJson($ctx.result.data["Business-asdf1234-dev"])
    
  • Cloudwatch 请求映射

  • {
        "logType": "RequestMapping",
        "path": [
            "batchAddBusiness"
        ],
        "fieldName": "batchAddBusiness",
        "resolverArn": "arn:aws:appsync:eu-west-1:12312312123:apis/asdf1234/types/Mutation/fields/batchAddBusiness",
        "requestId": "7ba690f3-74eb-400c-bbe9-49325241bc45",
        "context": {
            "arguments": {
                "business": [
                    {
                        "id": "6726da58-a8af-4656-9a8a-9c7453455e28",
                        "name": "Company A",
                        "phone": "+12341235",
                        "email": "office@notexists2.com",
                        "createdAt": "2020-04-05T15:37:43.614Z",
                        "updatedAt": "2020-04-05T15:37:43.614Z",
                        "__typename": "Business"
                    },
                    {
                        "id": "6726da58-a8af-4656-9a8a-9c7453455e23",
                        "name": "Asdf",
                        "phone": "+12341235",
                        "email": "office@notexists.com",
                        "createdAt": "2020-04-05T15:37:43.614Z",
                        "updatedAt": "2020-04-05T15:37:43.614Z",
                        "__typename": "Business"
                    }
                ]
            },
            "stash": {},
            "outErrors": []
        },
        "fieldInError": true,
        "errors": [
            "Unable to transform Response Template"
        ],
        "parentType": "Mutation",
        "graphQLAPIId": "alsdkasldksaasdlkasdkl",
        "transformedTemplate": "                                                    \n\n    \n\n    \n\n            \n    \n    \n    \n    \n    \n        \n    \n    \n    \n    \n    {\n  \"version\": \"2018-05-29\",\n  \"operation\": \"BatchPutItem\",\n  \"tables\": {\n      \"Business-asdf1234-dev\": [{\"createdAt\":{\"S\":\"2020-04-05T15:37:43.614Z\"},\"phone\":{\"S\":\"+12341235\"},\"__typename\":{\"S\":\"Business\"},\"name\":{\"S\":\"Company A\"},\"id\":{\"S\":\"6726da58-a8af-4656-9a8a-9c7453455e28\"},\"email\":{\"S\":\"office@notexists2.com\"},\"updatedAt\":{\"S\":\"2020-04-05T15:37:43.614Z\"}},{\"createdAt\":{\"S\":\"2020-04-05T15:37:43.614Z\"},\"phone\":{\"S\":\"+12341235\"},\"__typename\":{\"S\":\"Business\"},\"name\":{\"S\":\"Stiegenwirt\"},\"id\":{\"S\":\"6726da58-a8af-4656-9a8a-9c7453455e23\"},\"email\":{\"S\":\"office@notexists.com\"},\"updatedAt\":{\"S\":\"2020-04-05T15:37:43.614Z\"}}]\n  }\n}"
    }
    

    最佳答案

    我通过禁用整个 api 的数据存储解决了这个问题。
    这是方法,您也可以这样做:

    $ amplify update api
    $ Graphql
    $ remove datastore for entire API
    
    然后amplify push ,现在它将删除 _version每个字段的数据类型( build/schema.graphql )。而且,一切都会好起来的,您不再需要通过 _version .现在 BatchPutItem 对我来说工作正常。

    关于amazon-web-services - AWS Amplify 自定义解析器 - 不支持的操作 'BatchPutItem',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61045181/

    相关文章:

    amazon-web-services - AWS Elastic Beanstalk : Custom Cloudwatch Logs not showing despite IAM permissions and custom config

    amazon-web-services - 对 dynamodb 使用 cloudwatch 指标的一些疑问

    javascript - 在 React Native 应用程序中使用 AWS Amplify 在 GraphQL Mutation 中获取错误

    angular - 使用 AWS Amplify 身份验证模块禁用用户池的属性

    amazon-web-services - 对象放大模式定义中未显示一对多关系

    java - 对于超过可终止集群最大数量的请求,Amazon EMR 终止JobFlows

    python - 在另一个区域调用 Lambda 并在调用的区域进行更改

    postgresql - AWS 无法从网络访问 RDS (Postgres)

    amazon-s3 - 在 AWS Glue 中将增量数据从 Dynamodb 加载到 S3

    node.js - 删除 dynamodb 中的嵌套属性