AWS amplify datastore wont synchronise with server

Question:

So I’m trying to get AWS Amplify’s datastore library working with a reactJS web app I’m building. I was sending data to and from the api just fine using the graphql mutations manually, and then I tried to set up DataStore by following this tutorial. Everything is working fine in the client with the indexed DB, but I can’t get the data to synchronise with the api. I get the following error in the console:

I doubled checked, and I’m definitely including my AWS export as so:

And the content of the generated ./aws-exports.js is the following:

I’ve double checked the credentials for aws_appsync_graphqlEndpoint and aws_appsync_apiKey and they match the values returned from amplify status in the cli. I’ve also run amplify push and my Api has a status of No Change. When setting up the api I made sure to enable the conflict resolution too, as specified in the tutorial.

Interestingly, I also tried the following when setting up amplify:

This solved the DataStore - Data won't be synchronised error, but I got the following errors instead, and the data still isn’t being sent to the api.

Does this mean my API is somehow not configured properly? How Would I go about fixing it?

Answer:

I have been fiddling with the DataStore syncing for a while in a Angular project. Here is what I think I learned, from my observations or answers posted by others

  1. Can’t use API key authentication, that does not work. I use Cognito user pool and that works
  2. Had to enable Optimistic Concurrency as conflict resolution
    strategy, but I think it works even with the default Auto Merge, not sure exactly when is the first needed
  3. All types need an auto-generated id of type “ID!”. Without that, you
    get errors when the listeners are setup and syncing does not work
    even for types that have it.
  4. Make sure the code is regenerated and pushed, and DataStore stays
    enabled after each schema change (sometimes that setting may reset)

For troubleshooting, enable tracing (Amplify.Logger.LOG_LEVEL = 'DEBUG') and filter in browser’s console for messages that contain “err”. If any such messages exist, the entire sync does not work. That is how you can tell if you have one of the issues above.

Another trick is to delete the local amplify-datastore IndexDB in Chrome Dev Tools’ Application/Storage. If sync works, it will be re-populated from the DynamoDB table data in the cloud next time you reload your app.

Leave a Reply