rx-query
rx-query copied to clipboard
Feature request: Mutate function
I can see that queries have a mutator
- but I generally want mutation and queries to be separate bits of code (command/query segregation etc. etc) - how would I do that with this library?
Note: react-query has useQuery
and useMutation
- totally separate, and what I was expecting to see (obviously i'd need to keep the associated query's cache up-to-date...)
I agree.
The reasoning why I added mutate
was so rx-query can refresh the data automatically (which was something I needed in a project). With react-query, the user has to invalidate and refresh the cache(s) manually and thus the user needs to know the cache key before doing that.
I didn't add useMutation
because I believe that Angular is perfectly capable of making these, and with RxJS you can easily hook into the stream to invalidate and refresh the cache.
In the project that I'm working on, we're mainly only using rx-query to fetch data.
Oh, OK! The reason I want useMutation
is to get your consistent Pending
type around it. So I can check the .status
. It seems strange for me to re-implement that, and try to make it all match as closely as possible?
I understand that your approach to mutation seems better than keeping them separate like react-query does usually - BUT I want ALL THE OPTIONS :)
That's a valid argument to have (that I didn't think of) 😅
useMutation
can actually be a wrapper around useQuery
with different settings (I believe that's also the case with react query IIRC). Feel free to create a PR for this.
Hey, Is there a way to do manual query invalidation like: https://react-query.tanstack.com/guides/invalidations-from-mutations ? It will be nice to be able to refresh specific query or queries after mutation is done. I looked in source code and it seems that right now you can't update cache from outside.
Hey, Is there a way to do manual query invalidation like: https://react-query.tanstack.com/guides/invalidations-from-mutations ? It will be nice to be able to refresh specific query or queries after mutation is done. I looked in source code and it seems that right now you can't update cache from outside.
according to Tim:
with RxJS you can easily hook into the stream to invalidate and refresh the cache.
Can you paste example code
of how to do what he mentioned ?
Can you paste
example code
of how to do what he mentioned ?
No, I have no idea why it is easy to hook into the stream and invalidate and refresh the cache, sorry.
Does he mean using share in RxJS? Like is talked about here: https://www.prestonlamb.com/blog/rxjs-cache-and-refresh-in-angular ?
Something like this:
person$ = query('mutate-person', 1, (id) => this.http.get(`/persons/${id}`), {
mutator: (data, { queryParameters }) => {
return (
this.http
.post(`/persons/${queryParameters}`, data)
// 👇👇👇
.pipe(
tap(() => refreshQuery('persons'))
)
);
},
});
@timdeschryver I see one problem with that approach, if I have parameters in 'persons' query I need exactly those parameter values for refreshQuery, because it is cache key as mentioned in: https://github.com/timdeschryver/rx-query#refresh-a-query.
But I won't have those if I defined query and mutation in separate components
That's correct @tvildo
Feel free to open a PR which makes that possible if you want.