allen
allen2y ago

Optimistic Update on Paginated Query

Is there an example of an optimistic update on a paginated query? I'm unsure of how the paginationOpts are cache-keyed to make sure I can get the right item from cached.
const current = store.getQuery('messages/queries/messages', {
type: 'inbox',
paginationOpts: {}, //what goes here to make sure I get whatevers current?
});
const current = store.getQuery('messages/queries/messages', {
type: 'inbox',
paginationOpts: {}, //what goes here to make sure I get whatevers current?
});
For context, what I'm trying to accomplish is when a doc is soft deleted, ensure it is optimistically removed from the paginated result set(s) its present in.
8 Replies
sshader
sshader2y ago
Not quite an answer to your question, but I believe you could use store.getAllQueries and filter to the ones with type: 'inbox' (but allow any value for paginationOpts) and remove your soft deleted element from the query values where it's present?
allen
allenOP2y ago
Ok I can take that approach for now any talk of getQuery supporting partial matching of args?
sshader
sshader2y ago
Does the partial match on type: 'inbox' suit your needs here (I was kind of guessing)? Any ideas for what a nice partial match interface might look like? I think there are a bunch of things we could improve about our optimistic updates, so it's good to hear how our current ones are falling short / awkward to use.
allen
allenOP2y ago
Honestly, in these cases, I prefer how Apollo cache handles it, where if i purge a doc from cache, it would cascade across cache by reference. I understand his gets more complex in the convex ecosystem as an item's query may have a different shape than that same referenced item in a paginated result set. Would definitely need to be a way to to declare these relationships by id. ultimately it becomes fragile to remember every query a doc could be referenced in and optimistically update them. It would be easier to optimistically update the doc and have all other queries be reactive to that change by doc id reference (i use 'doc' here loosely -- obviously the result of a query could be any return shape)
ballingt
ballingt2y ago
This is helpful to hear, the Apollo cache style is pretty slick for this. You've hit the nail on the head, a higher level normalized store is a well-explored solution in the broader ecosystem but has Implications. Our top priorities right now are making things possible (in this case, the escape hatches of getAllQueries or building our own local optimistic layer with useState or your own global store with your own context) and building a stable foundation for higher level abstractions like this in the future. An Apollo cache-style normalized store is something I'd like too, but it's not going to be the suggested way to do things soon.
alexcole
alexcole2y ago
Yep, I'd also be excited to eventually build more powerful optimistic updates in our client! In the meantime, if you want some example code, there is a optimisticallyUpdateValueInPaginatedQuery helper for editing results of paginated queries (https://github.com/get-convex/convex-js/blob/main/src/react/use_paginated_query.ts#L405). Writing a helper to delete a value should be similar (and maybe we should add a helper for this to our npm package).
GitHub
convex-js/use_paginated_query.ts at main · get-convex/convex-js
TypeScript/JavaScript client library for Convex. Contribute to get-convex/convex-js development by creating an account on GitHub.
ballingt
ballingt2y ago
whoa nice, there's your real answer!
allen
allenOP2y ago
Thanks guys! Interested in tracking your improvements around this as they are made.

Did you find this page helpful?