Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async/await order between create and update mutations is not respected with latest DataStore version #7888

Closed
TheMoums opened this issue Mar 5, 2021 · 9 comments
Assignees
Labels
bug Something isn't working DataStore Related to DataStore category

Comments

@TheMoums
Copy link

TheMoums commented Mar 5, 2021

Describe the bug
Hello again,

I have found another piece of code that was working with the previous DataStore version but not working anymore.
I sometimes have the need to update a DataStore object just after its creation. If I just save and update it without doing any other DataStore request on the side, it is working.
But if I there are some other create mutations being processed before the create/update call, the update mutation seems to be executed before its creation.

To Reproduce
Let's say I have a very simple schema of the shape

type Building @model {
  id: ID!
  name: String!
  readId: ID!
}

Here is a simple code to reproduce the bug

const buildingPromises = [];
const putCongestion = true;
if (putCongestion) {
  for (let i = 0; i < 3; i++) {
    buildingPromises.push(
      DataStore.save(
        new Building({
          name: `Dummy building ${i}`,
          readId: 'ReadId1',
        })
      )
    );
  }
}

const createAndUpdateDumyBuilding = async () => {
  const dummyBuilding = await DataStore.save(
    new Building({
      name: 'Dummy building',
      readId: 'ReadId1',
    })
  );
  await DataStore.save(
    Building.copyOf(dummyBuilding, (updated) => {
      Object.assign(updated, {
        name: 'Dummy building updated',
      });
    })
  );
};
buildingPromises.push(createAndUpdateDumyBuilding());
await Promise.all(buildingPromises); 

If I set the variable putCongestion to false, only the creation mutation and its update are executed and everything is working fine.
But, if I set it to true, I perform 3 other creation mutations in parallel.
What happens is shown on the picture below (notice there are only 4 graphql calls instead of 5 i.e. 4 create and 1 update)
bug
DataStore, somehow, replaces the creation+update mutation by a create mutation with the update input (as shown on the picture above, it is doing the mutation 'createBuilding' with the input of the update mutation).

My guess would be that the creations calls are put in a queue and, while we are waiting for the 4th one to be called, the update mutation is already fired on an empty building and therefore tries to create one with its input.

Expected behavior
The async order between mutations should be kept, like previous version, no matter the congestion.

"@aws-amplify/api": "3.2.24",
"@aws-amplify/auth": "3.4.24",
"@aws-amplify/cache": "3.1.49",
"@aws-amplify/core": "3.8.16",
"@aws-amplify/datastore": "2.9.10",
"@aws-amplify/predictions": "3.2.24",
"@aws-amplify/storage": "3.3.24",

amplify --v : 4.44.1

@TheMoums TheMoums added the to-be-reproduced Used in order for Amplify to reproduce said issue label Mar 5, 2021
@iartemiev iartemiev added the DataStore Related to DataStore category label Mar 8, 2021
@iartemiev
Copy link
Member

Hey @TheMoums, the expected behavior here would be for the updated field(s) from the update mutation to be applied to the queued create mutation. They should still be sent in a single request, but you shouldn't be losing the readId field like it appears you are in the screenshot.

This is related to the same bug that's causing the issue you opened last week. I'm currently working on fixing it and recommend downgrading to @aws-amplify/datastore@2.9.3 until it's resolved.

@iartemiev iartemiev added bug Something isn't working and removed to-be-reproduced Used in order for Amplify to reproduce said issue labels Mar 8, 2021
@iartemiev iartemiev self-assigned this Mar 8, 2021
@TheMoums
Copy link
Author

TheMoums commented Mar 8, 2021

Alright, thanks a lot for your response!

@TheMoums
Copy link
Author

Same problem as #7950, @aws-amplify/datastore@2.9.15 did not fix the issue.

@iartemiev
Copy link
Member

My apologies, we solved the consecutive saves issues when an update immediately succeeds another update (#6363), but I now see that we need to make an additional adjustment specifically for an update right after a create. I'll have a PR up today

@sanyashvets
Copy link

any updates on this issue? May I ask when to expect fix for it?

@iartemiev
Copy link
Member

iartemiev commented Mar 29, 2021

PR is up. I'll post another update once we merge/deploy.

@Eugene-larychev
Copy link

up

@TheMoums
Copy link
Author

It works now with version 2.9.16 @aws-amplify/datastore .
Thanks a lot @iartemiev

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working DataStore Related to DataStore category
Projects
None yet
Development

No branches or pull requests

4 participants