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

(core) - Reemit results as stale if they're refetched in the background #1375

Merged
merged 5 commits into from
Feb 10, 2021

Conversation

kitten
Copy link
Member

@kitten kitten commented Feb 10, 2021

Resolve #1372

Summary

Usually we have a couple of refetches that are emitted as cache-and-network. In these cases the cache exchanges (default or Graphcache) take care of emitting a first result with the stale flag set. In other cases we may have a subsequent network-only reexecution which happens in the background. In these cases no part of the exchange pipeline or the Client took responsibility of emitting a stale result.

This PR changes this behaviour to allows the Client's executeRequestOperation stream to add-on a single stale result onto a prior result if it detects a network-only operation.

  • Has been tested in threed-web against Graphcache's cache.invalidate which triggers a network-only refetch.
  • This has also been fixed in 5c1b1fd to be applied to all operations that are not cache-only

Set of changes

  • Add a switchMap add-on stream per result that reemits the result as stale if it detects a network-only operation.
    • Only emit this stale add-on result when the previous result wasn't already stale
    • Ensure that this won't emit when the cache responds synchronously
    • Apply this to cache-and-network and cache-first (with cache misses) too
    • Emit this at most once since we expect a result afterwards

@changeset-bot
Copy link

changeset-bot bot commented Feb 10, 2021

🦋 Changeset detected

Latest commit: 5c1b1fd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@urql/core Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kitten
Copy link
Member Author

kitten commented Feb 10, 2021

I've tested this against our threed-web example by invalidating a thread when we create a reply for it. Previously this would only refetch the thread query in the background while we now see stale: true pop up correctly.

@kitten kitten changed the title (core) - Reemit an results as stale if they're reexecute as network-only background refetches (core) - Reemit results as stale if they're refetched in the background Feb 10, 2021
@kitten kitten merged commit 1de153b into main Feb 10, 2021
@kitten kitten deleted the feat/active-stale-state branch February 10, 2021 14:33
@github-actions github-actions bot mentioned this pull request Feb 10, 2021
@Coobaha
Copy link

Coobaha commented May 25, 2022

@kitten @JoviDeCroock What will be the recommended way of re-executing background queries without marking results as stale? My use case is custom refocus exchange, that compares refetched data with existing and only updates if it was actually changed so that we are not triggering rendering without a need. I don't know if exactly this PR is the reason but it was working before correctly, now first background refetch on focus is marking data as stale (and triggers re-renders), but all the subsequent background refetches are not marking it and views are not re-rendered.

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.

RFC: Stale to be "true" if any background fetching is happening
3 participants