Type: AWS::AppSync::Resolver
Properties:
  ApiId: !GetAtt AppSyncAPI.ApiId
  TypeName: "Mutation"
  FieldName: "updateMaterial"
  DataSourceName: !GetAtt AppSyncDynamoDBMaterialSource.Name
  RequestMappingTemplate: |
    #set( $condition = {
      "expression": "attribute_exists(#id)",
      "expressionNames": {
          "#id": "id"
      },
      "expressionValues": {}
    } )
    $util.qr($context.args.input.put("updatedAt", $util.defaultIfNull($ctx.args.input.updatedAt, $util.time.nowISO8601())))
    #if( $context.args.condition )
      #set( $conditionFilterExpressions = $util.parseJson($util.transform.toDynamoDBConditionExpression($context.args.condition)) )
      $util.qr($condition.put("expression", "($condition.expression) AND $conditionFilterExpressions.expression"))
      $util.qr($condition.expressionNames.putAll($conditionFilterExpressions.expressionNames))
      $util.qr($condition.expressionValues.putAll($conditionFilterExpressions.expressionValues))
    #end
    #if( $condition.expressionValues && $condition.expressionValues.size() == 0 )
      #set( $condition = {
      "expression": $condition.expression,
      "expressionNames": $condition.expressionNames
    } )
    #end
    #set( $expNames = {} )
    #set( $expValues = {} )
    #set( $expSet = {} )
    #set( $expAdd = {} )
    #set( $expRemove = [] )
    #set( $keyFields = ["id"] )
    #foreach( $entry in $util.map.copyAndRemoveAllKeys($context.args.input, $keyFields).entrySet() )
      #set( $entryKeyAttributeName = $entry.key )
      #if( $util.isNull($entry.value) )
        #set( $discard = $expRemove.add("#$entryKeyAttributeName") )
        $util.qr($expNames.put("#$entryKeyAttributeName", "$entry.key"))
      #else
        $util.qr($expSet.put("#$entryKeyAttributeName", ":$entryKeyAttributeName"))
        $util.qr($expNames.put("#$entryKeyAttributeName", "$entry.key"))
        $util.qr($expValues.put(":$entryKeyAttributeName", $util.dynamodb.toDynamoDB($entry.value)))
      #end
    #end
    #set( $expression = "" )
    #if( !$expSet.isEmpty() )
      #set( $expression = "SET" )
      #foreach( $entry in $expSet.entrySet() )
        #set( $expression = "$expression $entry.key = $entry.value" )
        #if( $foreach.hasNext() )
          #set( $expression = "$expression," )
        #end
      #end
    #end
    #if( !$expAdd.isEmpty() )
      #set( $expression = "$expression ADD" )
      #foreach( $entry in $expAdd.entrySet() )
        #set( $expression = "$expression $entry.key $entry.value" )
        #if( $foreach.hasNext() )
          #set( $expression = "$expression," )
        #end
      #end
    #end
    #if( !$expRemove.isEmpty() )
      #set( $expression = "$expression REMOVE" )
      #foreach( $entry in $expRemove )
        #set( $expression = "$expression $entry" )
        #if( $foreach.hasNext() )
          #set( $expression = "$expression," )
        #end
      #end
    #end
    #set( $update = {} )
    $util.qr($update.put("expression", "$expression"))
    #if( !$expNames.isEmpty() )
      $util.qr($update.put("expressionNames", $expNames))
    #end
    #if( !$expValues.isEmpty() )
      $util.qr($update.put("expressionValues", $expValues))
    #end
    {
      "version": "2018-05-29",
      "operation": "UpdateItem",
      "key": {
        "owner": $util.dynamodb.toDynamoDBJson($ctx.identity.sub),
        "sort": $util.dynamodb.toDynamoDBJson($ctx.args.input.id)
      },
      "update": $util.toJson($update),
      "condition": $util.toJson($condition)
    }
  ResponseMappingTemplate: |
    #if( $ctx.error )
      $util.error($ctx.error.message, $ctx.error.type)
    #else
      $util.toJson($ctx.result)
    #end