Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow intersecting stale results #168

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

Zacherl
Copy link
Contributor

@Zacherl Zacherl commented Mar 11, 2024

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:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • 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
  • No

When the "intersectStaleData" flag is passed, the returned data of the queries will also be intersected if every query has data.
When the "intersectStaleData" flag is passed, the returned data of the queries will also be intersected if every query has data.
Copy link

stackblitz bot commented Mar 11, 2024

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

@NetanelBasal
Copy link
Member

Can you explain the use case, please?

@Zacherl
Copy link
Contributor Author

Zacherl commented Mar 11, 2024

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
Copy link
Contributor Author

Zacherl commented Mar 12, 2024

@NetanelBasal Do you think this change is counterproductive?

@NetanelBasal
Copy link
Member

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

@Zacherl
Copy link
Contributor Author

Zacherl commented Mar 12, 2024

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.

@NetanelBasal
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants