aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

Selective sync ignores delta sync #7495

Closed TheMoums closed 3 years ago

TheMoums commented 3 years ago

Describe the bug We are trying out the selective sync but the sync operations seem to be called from scratch on every page reload (like if delta sync was not taken into account).

When not using any selective sync and on DataStore.start(), on page refresh, sync operations were returning: {data: [] } because nothing had changed.

But, with the selective sync, when reloading the page, all sync operations are returning all the data again which is a huge performance drawback.

This issue was supposed to be solved by https://github.com/aws-amplify/amplify-js/pull/7200 But the problem persists anyway. To Reproduce The following configuration

const getClientId = async () => { 
const user = await Auth.currentAuthenticatedUser(); const userAttributes = await Auth.userAttributes(user); 
const clientIdAttribute = userAttributes.find((attribute) => attribute.getName() === ‘custom:clientId’); 
return !isNil(clientIdAttribute) && !isNil(clientIdAttribute.getValue()) ? clientIdAttribute.getValue() : null; 
}; 

DataStore.configure({ 
syncExpressions: [ 
    syncExpression(Address, async () => { 
    const clientId = await getClientId(); 
    return (t: ModelPredicate<Address>) => t!.clientId(‘eq’, clientId!); 
    }), 
  syncExpression(Contact, async () => { 
  const clientId = await getClientId(); 
  return (t: ModelPredicate<Contact>) => t!.clientId(‘eq’, clientId!); }); 
   }),
 ],
});

Refetch all data in the sync queries at each refresh, like nothing was synced. (The clientId is the same each time as the user stays logged in and just refreshing the page.) Expected behavior If nothing has changed in the database, the sync operations should be returning {data: [] }

What is Configured? I'm using the latest version of @aws-amplify/datastore

If applicable, please provide what is configured for Amplify CLI:

iartemiev commented 3 years ago

@TheMoums if you inspect the network request for that graphql query in dev tools, do you see a timestamp for lastSync in the request payload? (Specifically after refreshing the page)

iartemiev commented 3 years ago

Could you share screenshots or copy-paste the contents of the request payload on initial load (e.g., delete the IDB database and reload) and then after refreshing the page again.

For example, in my sample app, on initial load I see: Screen Shot 2021-01-05 at 10 27 51 AM (Note lastSync = 0, which will result in a base sync and return all of the data that matches the predicate as expected)

But then on refresh, I get Screen Shot 2021-01-05 at 10 28 12 AM Which results in a Delta sync and returns data: []

TheMoums commented 3 years ago

Hi @iartemiev

My first syncing query looks like

initial

And results with

answer_initial

If I reload the page (same filter i.e. syncExpression), the lastSync variable is correctly set

reload

But everyhing is refetched from the database as shown below

answer_reload

iartemiev commented 3 years ago

Thanks for sharing that info. The library itself seems to be working as expected then. The issue might be with the AppSync resolver. Are you using a recent version of amplify-cli? If so, are you using custom resolvers (specifically for syncContacts)?

TheMoums commented 3 years ago

I'm not using using any custom resolver. However, I'm still using an old amplify-cli version (4.32.1) as I'm waiting for some specific bugs to be fixed to upgrade it.

What's the minimum cli version needed?

iartemiev commented 3 years ago

@TheMoums selective sync support was added to @aws-amplify/cli in version 4.32.0, however, there may be bugs in the CLI version you're using that have since been fixed that are causing this issue. Could you perhaps try creating a duplicate project with the latest CLI version to see if you're still experiencing the issue there?

TheMoums commented 3 years ago

I have upgrraded my cli to the latest version (4.41.1) and the problem unfortunately persists

iartemiev commented 3 years ago

Do you see the same behavior if you do:

  1. Start app in 2 browsers
  2. Perform some mutations in browser 1
  3. Reload the page in browser 2

In other words, I'm curious if AppSync still performs a base sync for you when there are items in the delta table.

TheMoums commented 3 years ago

Yes I see the same behaviour.

Below, I have started two pages on localhost:3000 in chrome as well as in edge.

If I perform a mutation, for instance createContact in my chrome browser:

chrome_request

And I reload my edge browser:

edge_reload

I get my new contact, but I'm still syncing all the other ones. (And I'm still re-syncing all the other tables).

TheMoums commented 3 years ago

@iartemiev Any news on this?

iartemiev commented 3 years ago

I've attempted multiple different ways but was not able to reproduce this issue.

Let's try this: could you share your Contact model definition from your schema.graphql and the corresponding sync query resolver (should be amplify/backend/api/<app_name>/build/resolvers/Query.syncContacts.req.vtl)

TheMoums commented 3 years ago

Is there a way I can send you this by mail or any other private way?

My contract forbids me to post this code online

iartemiev commented 3 years ago

Perhaps you could create a private GitHub repo, upload the files containing the requested information, and then add me as a contributor to the repo?

iartemiev commented 3 years ago

If uploading to a private repo is still considered posting it online per your contract, feel free to reach out to me directly via Discord. My handle is ObjectiveCat#3157

iartemiev commented 3 years ago

Your teammate SebS reached out to me on Discord and provided the requested files. I was able to reproduce and opened a PR in the CLI repo with a minor bug fix.

TheMoums commented 3 years ago

It is now working with the latest version of amplify cli.

Thanks a lot!!

iartemiev commented 3 years ago

Great! Thank you for letting us know

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.