amplify-swift: In case of errors on some items during sync, no items are saved

Describe the bug

If during initial sync, AppSync responds with some items on error, these items are null in items response array. The other items which are not in error are not saved in datastore. Here a redacted result for sync Model named Draw with an error on one item:

{
  "data": {
    "syncDraws": {
      "items": [
        {
          "id": "70083950-9b18-4e62-90c6-433ae933def3",
          "drawBatch": {
            "id": "e236042a-7c21-4b82-90b3-d8b73d2c6b99",
            "_deleted": true
          },
          "item": {
            "_deleted": true,
            "id": "fa986c78-ff21-46e2-82ff-8d0a218510bc"
          },
          "owner": "cb8c1cd8-f22c-4e30-8a7c-42fa3e2e16b1",
          "updatedAt": "2023-09-20T09:09:06.134Z"
        },
        null
  },
  "errors": [
    {
      "path": [
        "syncDraws",
        "items",
        17,
        "drawBatch"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 10,
          "column": 7,
          "sourceName": null
        }
      ],
      "message": "Not Authorized to access drawBatch on type Draw"
    }
  ]
}

Steps To Reproduce

Steps to reproduce the behavior:
1. Consider a schema.graphql where 2 objects are linked together with `@hasMany` and `@belongsTo` and each object have the following auth policy: `@auth(rules: [{ allow: owner}])`.
2. Create on object of each type and linked then together.
3. Change the owner of the object with the `@hasMany` relationship.
4. The sync of the object type with `@belongsTo` is no longer working.

Expected behavior

DataStore’s initial sync should reconcile partial GraphQL responses by saving the items in the local Database and emitting the errors to the errorHandler.

Amplify Framework Version

2.19.0

Amplify Categories

DataStore

Dependency manager

Swift PM

Swift version

5.9

CLI version

12.4.0

Xcode version

15.0

Relevant log output

No response

Is this a regression?

No

Regression additional context

No response

Platforms

iOS

OS Version

iOS 17.0.1

Device

iPhone

Specific to simulators

No response

Additional context

See discussion https://github.com/aws-amplify/amplify-swift/issues/3232#issuecomment-1741312878

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 40 (23 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for confirming your tests are working. I’ll keep this issue open until my PR is merged and released. In terms of the Starscream dependency and memory leak patch, i’ll create two new issues to track it, thanks for the details!

#3487 further improves the retryability determination and has been released in https://github.com/aws-amplify/amplify-swift/releases/tag/2.25.6 Please give this update a try.

@gbitaudeau Could you open a feature request for that? I’ll look into flushing out the details. Feel free to reference back to this issue to keep the context. We’d like to resolve this bug since the root cause has been fixed and released.

For replaying the mutation, in flow 3, do you replay all missing creates / updates / deletes against the local database by calling DataStore.save/delete?