rx-query icon indicating copy to clipboard operation
rx-query copied to clipboard

Feature request: Mutate function

Open IanIsFluent opened this issue 4 years ago • 11 comments

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...)

IanIsFluent avatar Dec 09 '20 13:12 IanIsFluent

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.

timdeschryver avatar Dec 13 '20 16:12 timdeschryver

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 :)

IanIsFluent avatar Dec 14 '20 07:12 IanIsFluent

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.

timdeschryver avatar Dec 14 '20 19:12 timdeschryver

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.

tvildo avatar Oct 15 '21 09:10 tvildo

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.

IanIsFluent avatar Oct 15 '21 09:10 IanIsFluent

Can you paste example code of how to do what he mentioned ?

tvildo avatar Oct 15 '21 10:10 tvildo

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 ?

IanIsFluent avatar Oct 15 '21 13:10 IanIsFluent

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 avatar Oct 15 '21 15:10 timdeschryver

@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

tvildo avatar Oct 15 '21 15:10 tvildo

That's correct @tvildo

timdeschryver avatar Oct 15 '21 15:10 timdeschryver

Feel free to open a PR which makes that possible if you want.

timdeschryver avatar Oct 15 '21 15:10 timdeschryver