query icon indicating copy to clipboard operation
query copied to clipboard

Allow intersecting stale results

Open Zacherl opened this issue 11 months ago • 7 comments

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

Zacherl avatar Mar 11 '24 07:03 Zacherl

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

stackblitz[bot] avatar Mar 11 '24 07:03 stackblitz[bot]

Can you explain the use case, please?

NetanelBasal avatar Mar 11 '24 08:03 NetanelBasal

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.

Zacherl avatar Mar 11 '24 08:03 Zacherl

@NetanelBasal Do you think this change is counterproductive?

Zacherl avatar Mar 12 '24 09:03 Zacherl

Yes, I don't know it this is always the expected behaviour. It feels like Promise.allSettled a little bit

NetanelBasal avatar Mar 12 '24 10:03 NetanelBasal

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.

Zacherl avatar Mar 12 '24 10:03 Zacherl

I will leave the PR open and see if anyone else wants this functionality. Thanks.

NetanelBasal avatar Mar 12 '24 10:03 NetanelBasal