AWS Amplify 自定义解析程序 - 不支持的操作“BatchPutItem”

Posted

技术标签:

【中文标题】AWS Amplify 自定义解析程序 - 不支持的操作“BatchPutItem”【英文标题】:AWS Amplify Custom Resolver - Unsupported operation 'BatchPutItem' 【发布时间】:2020-07-17 14:07:11 【问题描述】:

我正在遵循一些指南来实现具有 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"

【问题讨论】:

您是否在绑定到Mutation.batchAddBusiness 的 DDB 数据源上启用了数据源版本控制?如果是,那么你不能使用BatchPutItem 嗨,是的,数据源版本控制阻止了像这里讨论的批处理操作:github.com/aws-amplify/amplify-cli/issues/3889 但是你可以写一个答案,因为你在正确的轨道上!谢谢! @KevinRegenrek 你能解决这个问题吗?如果是,那么您能否在此处发布解决方案?我也面临同样的问题。 "Unsupported operation 'BatchPutItem'. Datasource Versioning only supports the following operations (TransactGetItems,PutItem,BatchGetItem,Scan,Query,GetItem,DeleteItem,UpdateItem,Sync)" 我正面临这个问题 【参考方案1】:

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

$ amplify update api
$ Graphql
$ remove datastore for entire API

然后是amplify push,现在它将从每个数据类型(build/schema.graphql)中删除_version 字段。而且,一切都会正常工作,您不再需要传递_version。现在 BatchPutItem 对我来说工作正常。

【讨论】:

以上是关于AWS Amplify 自定义解析程序 - 不支持的操作“BatchPutItem”的主要内容,如果未能解决你的问题,请参考以下文章

为 React 自定义 AWS Amplify 身份验证 UI

aws amplify graphql 计算域内联解析器

通过 AWS amplify 和 ElasticSearch 查找地理和其他字段

在反应本机应用程序中使用 AWS Amplify 在 GraphQL 突变中出错

AWS Amplify 克隆存储库错误:“s-s-r 应用程序不支持基本身份验证。”

AWS Amplify and React - 找不到模块:无法解析“@aws-amplify/analytics”