Type: AWS::AppSync::Resolver
Properties:
  ApiId: !GetAtt AppSyncAPI.ApiId
  TypeName: "Mutation"
  FieldName: "createWork"
  DataSourceName: !GetAtt AppSyncDynamoDBWorkSource.Name
  RequestMappingTemplate: |
    $util.qr($context.args.input.put("id", $util.defaultIfNull($ctx.args.input.id, $util.autoId())))
    $util.qr($context.args.input.put("owner", $ctx.identity.sub))
    $util.qr($context.args.input.put("sort", $ctx.args.input.id))
    #set( $createdAt = $util.time.nowISO8601() )
    $util.qr($context.args.input.put("createdAt", $util.defaultIfNull($ctx.args.input.createdAt, $createdAt)))
    $util.qr($context.args.input.put("updatedAt", $util.defaultIfNull($ctx.args.input.updatedAt, $createdAt)))
    $util.qr($context.args.input.put("__typename", "Work"))
    #set( $condition = {
      "expression": "attribute_not_exists(#id)",
      "expressionNames": {
          "#id": "id"
      }
    } )
    #if( $context.args.condition )
      #set( $condition.expressionValues = {} )
      #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
    {
      "version": "2018-05-29",
      "operation": "PutItem",
      "key": {
        "owner": $util.dynamodb.toDynamoDBJson($ctx.identity.sub),
        "sort": $util.dynamodb.toDynamoDBJson($ctx.args.input.id)
      },
      "attributeValues": $util.dynamodb.toMapValuesJson($context.args.input),
      "condition": $util.toJson($condition)
    }
  ResponseMappingTemplate: |
    #if( $ctx.error )
      $util.error($ctx.error.message, $ctx.error.type)
    #else
      $util.toJson($ctx.result)
    #end