amplify-swift: Error: "The key __typename was not found" when using DataStore with existing data in database.

Describe the bug If the api is created without enabling conflict detection and data is saved to the backend using the API plugin then after conflict detection is added DataStore fails with The key __typename was not found

To Reproduce Steps to reproduce the behavior:

  1. Create new api as described in the tutorials using amplify cli - amplify api add without enabling conflict detection under the advanced settings option
  2. push changes amplify push
  3. Save data to the backend using Amplify.API.mutate function
  4. Use the Amplify CLI to edit the conflict detection settings. amplify update api
  5. push changes amplify push
  6. Use the Amplify.DataStore api to fetch data. Amplify.DataStore.query
  7. Error is shown in console and data is not synced from backend:
APIError: failed to process graphqlResponseData
Caused by:
DataStoreError: The key `__typename` was not found
Recovery suggestion: Check if the parsed JSON contains the expected `__typename`

Expected behavior The data to sync from the backend Environment(please complete the following information):

  • Amplify Framework Version: 1.0
  • Dependency Manager: Cocoapods
  • Swift Version : 5.1

Device Information (please complete the following information):

  • Device: Simulator
  • iOS Version: iOS 13

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 26 (8 by maintainers)

Most upvoted comments

same error after add a many to many link.

made changes to DynamoDB from Python Lambda function

That would definitely cause issues. By default, direct updates to the backing DDB tables would not trigger any subscriptions, since AppSync isn’t aware that the data has been modified. Secondly, mutations have particular constructions including version control and conflict resolution fields that have to be kept in sync. Thirdly, if you do have DataStore syncing enabled, updates posted directly to DDB would bypass the sync tables that we use to keep track of updates to a record, meaning that even if you do store the value in the DDB table correctly, you wouldn’t necessarily see it in your application if it does a “sync” query–only a full query would return the posted value.

The recommendation for DDB tables backing DataStore applications is to always post mutations via DataStore, or via well-structured queries through the AppSync GraphQL endpoint.

Error: DataStoreError: The key __typename was not found

General root cause: There is an inconsistency with database/tables/items/entry value. This means that you have an entry that does not match your defined schema structure, or the database on the cloud is in contradict with the database on the device.

Solution: You need to find that particular non-conforming table, or item, or entry value and amend/delete that from database on the cloud. Restart, and you are good to go.

Just received for the first time, made no changes to schema. I did switch from graphQL to datastore a month ago (so had to turn on conflict resolution) but it was syncing and working perfectly for that long until yesterday? so strange. Now what do I have to do?

CLI gh issue was closed because this needs more details design and require investigating all the use cases and side effects. I will keep this ticket open and will update here when we have more info.

@drochetti for me it was via the Amplify Console and via the mobile DataStore library.

@drochetti - I was using the AppSyncClient SDK. i.e. Amplify.API.mutate()