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
Update method query observer base result #3636
Update method query observer base result #3636
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 06d4128:
|
I'm not sure there is a real use-case for this function. Usually, you'd do what you are trying to do in the |
Of course the same result could be achieved using |
https://swr.vercel.app/docs/mutation#optimistic-updates |
@u3u You are right. Importing |
You can say the same thing about all functions on the query client, like Adding new functionality that can already be achieved in different ways is always a trade-off. Some people already complain about bundle size, so it's very hard to make everybody happy. Adding a feature should imo yield a distinct functionality for a common case. The biggest gain that I can see currently by this proposal is the type-safety, because
It's documented here on how to do that with the current apis. Yes, it involves I will need to think about the tradeoffs a bit more. In the meantime, you can already build that quite easily in user-land:
|
Yeah, currently, I'm not using typescript. However, if, by any chance, bounding |
@sahilmob the main point would actually be:
Can you show a concrete example where the function would be useful? |
I would argue that often, especially in tabular data, mutations happen in the same component where the query happened, for example, this simple todo list, toggling a todo status and the query to get the todos both happened in the same component. This is also true if you're using a components library, i.e, Material UI, which allows you to create a fully functional table with no more than 25 lines of code, including the logic for rendering a single row, which might contain a toggle for activating/deactivating a row for example. A transfer list is also a good example. |
Thanks for the examples - let me try to explain why I don't think that they are representative:
if checking off the todo should perform an optimistic update, this example leaves out a bunch of code. The toggling itself would happen within the
Let's assume the mutation should happen once you click the buttons in the middle that transfer entries from left to right. Selecting entries on the left is pure client state up to the point where you trigger the mutation. You should definitely not write to the queryCache when the user checks one of those boxes, because that selection would be overwritten with any background update. Suppose the following scenario:
Things like this are the reason why I think exposing this function is not a good idea - it will encourage people to write directly to the query cache on the client side, which you likely shouldn't be doing. The query cache is a client side "mirror" of the state the server currently holds. The only situation where it makes sense to write to it directly is for optimistic updates, where you are telling the cache that this is the state we expect the server to be in anyways in a couple of seconds, or to update from mutation responses, where the server tells you that this is the current server state, and you want to set it directly instead of refetching. |
Thanks for your detailed answer. However, If you ask me, I would not do any assumptions around how people would wan't to use any api exposed to them. because they could update the query cache in a way that doesn't reflect the server state with |
The aim of this pr is to expose a convenience
update
function that has the query key in its closure scope and internally callsqueryClient.setQueryData(key, updater)
.In many cases, there is a need to optimistically update query data inside the same component that calls
useQuery
, so instead of usingqueryClient.setQueryData(key, updater)
,update
comes preloaded with the right query key in its closure scope and internally callsqueryClient.setQueryData(key, updater)
.So it allows for the following
instead of