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

How to make optimistic updates while creation is still in progress #4105

Closed
priomsrb opened this issue Nov 6, 2018 · 7 comments
Closed

How to make optimistic updates while creation is still in progress #4105

priomsrb opened this issue Nov 6, 2018 · 7 comments

Comments

@priomsrb
Copy link

priomsrb commented Nov 6, 2018

Related stackoverflow post: https://stackoverflow.com/questions/53180817/apollo-client-making-optimistic-updates-while-creation-is-still-in-progress

I want to be able to do updates on an object while it is still being created.

For example: Say I have a to-do list where I can add items with names. I also want to be able to edit names of items.

Now say a user with a slow connection creates an item. In that case I fire off a create item mutation and optimistically update my UI. That works great. So far no problem.

Now let's say the create item mutation is taking a bit of time due to a slow network. In that time, the user decides to edit the name of the item they just created. For an ideal experience:

  1. The UI should immediately update with the new name
  2. The new name should eventually be persisted in the server

I can achieve #2 by waiting for the create mutation to finish (so that I can get the item ID), then making an update name mutation. But that means parts of my UI will remain unchanged until the create item mutation returns and the optimistic response of the update name mutation kicks in. This means #1 won't be achieved.

So I'm wondering how can I achieve both #1 and #2 using Apollo client.

Note: I don't want to add spinners or disable editing. I want the app to feel responsive even with a slow connection.

@joe-re
Copy link
Contributor

joe-re commented Nov 7, 2018

@priomsrb probably you can use update functions for that
https://www.apollographql.com/docs/react/essentials/mutations.html#update

@priomsrb
Copy link
Author

priomsrb commented Nov 8, 2018

@joe-re Thanks for the link. I had a look at the update function, but I'm not sure if it will help in this scenario. Here's my thinking why:

  1. User creates a todo item
  2. The create item mutation's update() function gets called with optimistic changes. Now let's say the server will take a long time to respond.
  3. In the mean time, the user edits the todo item
  4. The create item mutation hasn't returned from the server yet. So we don't know what ID of the todo item to update. So we wait until the creation is complete. In this time the UI is out of sync
  5. The create item mutation's response from the server is received. update() gets called on the create item mutation.

In step 4, there's no update() function for us to utilize. So the UI goes out of sync.

Let me know if I missed something or there's a different way to solve this problem.

Thanks.

@jonmifsud
Copy link

Hi @priomsrb, I'm assuming you've fixed this but in case someone else may be reading; what we generally do is we set the id via a generated uuid beforehand - then updates would work on the same ID.

@priomsrb
Copy link
Author

priomsrb commented Apr 5, 2019 via email

@frederikhors
Copy link

@priomsrb you can close this.

@bitofbreeze
Copy link

@priomsrb I'm worried about allowing the client to specify the id. I guess with uuid you're not likely to get collisions but what if someone intentionally forces collisions querying the endpoint directly? I guess your resolver could account for that. Are there any other potential shortcomings with allowing the user to specify an id?

@priomsrb
Copy link
Author

@CSFlorin I can't actually think of any shortcomings with someone intentionally forcing a collision. Because this is an "upsert" operation, by forcing a collision, you are effectively just making a regular update operation. As long as your update operation does proper permission checking etc, there shouldn't be any issues.

One potential shortcoming of letting clients decide IDs is that if it gets displayed somewhere, they may try to get "creative" with how they name it. For example if youtube video IDs could be decided by clients they could put offensive words in the URL. Though you could argue that you can run the URL through some "safe language filter" that you would run on the video title anyway.

@priomsrb priomsrb reopened this Jun 14, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants