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

Is there a way for queries to wait for mutations to finish? #107

Closed
gitnarqt opened this issue Jan 29, 2019 · 9 comments
Closed

Is there a way for queries to wait for mutations to finish? #107

gitnarqt opened this issue Jan 29, 2019 · 9 comments
Assignees
Labels
pending-response Issue is pending response from the issue requestor question General question

Comments

@gitnarqt
Copy link

Hello,

We're facing some inconsistency in the data when it comes to executing several mutations and queries at the same time. According to our tests, query operations are not enqueued in the same queue as the mutation operations and thus don't respect the calling order. Is that correct?

Example of deleting images:

  • Delete image sets the getImage(ID) query to null in the Apolo storage and sends a delete mutation to AWS
  • Order in which mutations and queries are called: Delete image 1, Delete image 2, Delete image 3, Download all images (NETWORK_ONLY)
  • Order in which offline mutations are executed: Delete image 1, Delete image 2, Delete image 3
  • Order in which mutations and queries are executed: Delete image 1, Download all images (Image 2 and 3 are back on the client side), Delete image 2, Delete image 3

This might cause confusion for our clients. Is this a bug or is it a correct behaviour of AppSync?

Thanks!

@scb01
Copy link
Contributor

scb01 commented Jan 29, 2019

@gitnarqt

Yes, the mutation and query behavior is as per the architecture of the AppSync. Mutations are put into a queue and executed in a sequential fashion and respect the calling order; Queries are not enqueued and are executed independent of the mutation queue.

An interesting area would be to look into the Client Architecture section in https://aws-amplify.github.io/docs/android/api. This section talks about how you can power the UI using a combination of client side caching/responseFetcher semantics, and prepping the cache by writing "optimistic responses" and updating them with "actual responses" received when executing mutations.

I am also adding @undefobj and @nikhil-dabhade to this thread to provide their thoughts

@undefobj
Copy link
Contributor

@gitnarqt could you go into a layer deeper around the application experience you're trying to deliver, and possibly any code/layout examples around the use case? It's not immediately clear to me why you would perform queries in such succession for overlapping data structures since, by GraphQL spec definition, a mutation is a write followed by an immediate fetch which would fulfill the data requirements.

cc @palpatim

@gitnarqt
Copy link
Author

gitnarqt commented Feb 1, 2019

Hey guys, thank for the quick response! I'll look more into the architecture, might have missed something.

@undefobj we're working on an image gallery app that could be used offline and online. Our users could edit their images offline and at some point when they connect online, they have the ability to refresh their gallery and download their images from the cloud. The issue is occurring when they edit a lot of images in offline mode, then go online and refresh their gallery. Currently, we fixed it by waiting for mutationQueue to finish all mutations before performing any query - not sure this is the optimal way.

@undefobj
Copy link
Contributor

undefobj commented Feb 3, 2019

@gitnarqt thanks for the information on how your app works. I've been thinking about this the past day and I wonder, how would you expect this to work otherwise? Do you want all queries to be blocked until the mutation queue is drained? Or would you want all queries to be executed first? The current design allows UX updates independent of mutations, and if there is a change to the data the client can receive a subscription notification.

@sunchunqiang sunchunqiang added the pending-response Issue is pending response from the issue requestor label Feb 4, 2019
@gitnarqt
Copy link
Author

gitnarqt commented Feb 5, 2019

Sorry for the late response @undefobj. Thanks for the tip! We didn't implement subscription which I think would solve the issue we're facing with inconsistent data. Eventually, the user will be editing outdated images but I assume a conflict resolver would handle such a case. Thanks again!

@gitnarqt gitnarqt closed this as completed Feb 5, 2019
@HashCode55
Copy link

@gitnarqt I'm facing the same issue. Can you elaborate how implementing a subscription would solve this issue?

@gitnarqt
Copy link
Author

gitnarqt commented Sep 5, 2019

@HashCode55 we didn't use subscription as it would require some time to implement. What we ended up doing is waiting for the mutation queue to be empty before performing any query from the cloud. This solved our problem and so far it's running smoothly!

@HashCode55
Copy link

HashCode55 commented Sep 5, 2019

@gitnarqt Thanks for the fast response. I'm suffering from the same problem right now! I'm using javascript SDK though, can you send me a snippet how you are waiting on the mutation queue? I couldn't find any documented source.

Edit: And how are you handling the case if the user reopens the app and is still offline? Waiting on the mutation queue would halt the app in this case or do you explicitly check the network connection as well?

@gitnarqt
Copy link
Author

gitnarqt commented Sep 5, 2019

@HashCode55 we're using the Java SDK version which includes the isMutationQueueEmpty() and the clearMutationQueue(). Check issue #96 for a sample code

As for the check, indeed we do a network check. If there is no internet, we just prevent it from waiting for the mutation queue.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-response Issue is pending response from the issue requestor question General question
Projects
None yet
Development

No branches or pull requests

6 participants