aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

[JS] DynamoDB UpdateItem return old values on ConditionalCheckFailedException #343

Open Lorenzohidalgo opened 7 months ago

Lorenzohidalgo commented 7 months ago

Docs state that one should be able to see that data by default on ctx.error.data, but this doesn't seem to be the case.

{
  "arguments": {
    "recordInput": {
      "id": "1",
      "sk": "1"
    }
  },
  "prev": {
    "result": {}
  },
  "stash": {},
  "error": {
    "message": "The conditional request failed (Service: DynamoDb, Status Code: 400, Request ID: 3S...)",
    "type": "DynamoDB:ConditionalCheckFailedException"
  },
  "outErrors": []
}

There is also no returnValuesOnConditionCheckFailure attribute for the UpdateItem operation allowed.

Actual response (where docs state we should also be able to see the old/current record values):

{
  "data": {
    "extendTtl": null
  },
  "errors": [
    {
      "path": [
        "extendTtl"
      ],
      "data": null,
      "errorType": null,
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "The conditional request failed (Service: DynamoDb, Status Code: 400, Request ID: 3S...)"
    }
  ]
}

With TransactWriteItems, where for some reason you can actually use returnValuesOnConditionCheckFailure for an UpdateItem operation, it seems to work as expected and the value is reachable in the ctx:

{
  "arguments": {
    "recordInput": {
      "id": "1",
      "sk": "1"
    }
  },
  "result": {
    "cancellationReasons": [
      {
        "type": "ConditionalCheckFailed",
        "message": "The conditional request failed",
        "item": {
          "test": {
            "test1": true
          },
          "sk": "1",
          "testField": "1-1",
          "id": "1",
          "ttl": 1705338604
        }
      }
    ]
  },
  "prev": {
    "result": {}
  },
  "stash": {},
  "error": {
    "message": "Transaction cancelled, please refer cancellation reasons for specific reasons [ConditionalCheckFailed] (Service: DynamoDb, Status Code: 400, Request ID: OTL...)",
    "type": "DynamoDB:TransactionCanceledException"
  },
  "outErrors": []
}

But using TransactWriteItems isn't really an option, as it doesn't return all values of the updated record...

eriktim commented 2 months ago

According to AWS support the docs are outdated and this field will not be present with APPSYNC_JS. I'm hoping for an official update here as well.