query
query copied to clipboard
Allow intersecting stale results
This change allows intersecting not only current, but also stale data from multiple queries.
PR Checklist
Please check if your PR fulfills the following requirements:
- [x] The commit message follows our guidelines: https://github.com/ngneat/query/blob/master/CONTRIBUTING.md#commit
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
PR Type
What kind of change does this PR introduce?
- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, local variables)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Documentation content changes
- [ ] Other... Please describe:
What is the current behavior?
At the moment, the data is only intersected if all queries are successful.
Issue Number: N/A
What is the new behavior?
If an option is passed, data will also returned if all queries contain data (stale or current).
Does this PR introduce a breaking change?
- [ ] Yes
- [x] No
Run & review this pull request in StackBlitz Codeflow.
Can you explain the use case, please?
Sure! If previous fetches were successful, the query library returns data even if the last fetch failed due to an error. We use this feature extensively to show data to the user even if it is stale, for example due to a bad internet connection. The data does not even need to be stale if the query was not yet invalidated but triggered again via refetch.
With the intersectResults functionality, so far we lose that feature - only if all queries are successful, data is returned. I think this kind of breaks the contract of the @tanstack/query interface.
While I'm writing about it - I think the "isStale" flag from the tanstack interface should also be added.
@NetanelBasal Do you think this change is counterproductive?
Yes, I don't know it this is always the expected behaviour. It feels like Promise.allSettled a little bit
Yes, it feels a bit similar to Promise.allSettled. I think that is a useful behavior.
If I have a normal query (not intersected), as long I have cached results I get data even if the latest fetch failed and I can display that data (maybe display some information that it is stale via the isStale flag and so on, you can see the flag description here: https://tanstack.com/query/latest/docs/framework/react/reference/useQuery).
With the current implementation of intersectResults, I get only data if the latest fetch for all intersected queries was successful and only in this case I can display data.
We currently implemented this utility in our own code, I just thought it might be useful for others as well.
Of course, if you think it should not be in the library, feel free to close the PR.
I will leave the PR open and see if anyone else wants this functionality. Thanks.