rtk-incubator / rtk-query

Data fetching and caching addon for Redux Toolkit
https://redux-toolkit.js.org/rtk-query/overview
MIT License
626 stars 31 forks source link

Add unwrap() to LazyQuery fetch #195

Closed SaulMoro closed 3 years ago

SaulMoro commented 3 years ago

Hi there!

I am the creator of https://github.com/SaulMoro/ngrx-rtk-query, implementing the LazyQuery feature in the library, I have found that it would be nice to have the same unwrap() functionality as in the mutations.

For example, in the following use case I could reset the input of the form only if the request is correct. It is a stupid use case, but it may be the use case where it is most necessary.

phryneas commented 3 years ago

The thing is that useLazyQuery does not necessarily actually fire a query. If the data would already be available in the store, that .unwrap would actually throw - which would be very unexpected.

That's pretty much the reason we have no promise on useQuery().refetch() and useLazyQuery()[0].

If you have any suggestions on how to perfectly handle those cases, I'm all ears :)

phryneas commented 3 years ago

Adding to that, as I just remembered: We were thinking about instantly resolving with the correct value, but then there is also this edge case:

SaulMoro commented 3 years ago

Ohh you are right! Anyway, the use case can be solved in Angular with rxjs tap pipe (query$.pipe(tap((result) => ...))) in the component or with a NgRx effect. On the other hand, in React can be solved with useEffect.