AWS Amplify Custom Resolver - неподдерживаемая операция 'BatchPutItem' - PullRequest
0 голосов
/ 05 апреля 2020

Я следую некоторым инструкциям по реализации пользовательского распознавателя с операцией 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 RequestMapping
{
    "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}"
}
...