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

Question: Can you prevent Root Queries from being refetched on a nested entity TTL expire? #58

Open
jwarykowski opened this issue Jun 29, 2022 · 1 comment

Comments

@jwarykowski
Copy link

Hey @danReynolds,

When you have a TTL entity e.g a Person and then have a root query e.g GetPeople, if any of the underlying entities under that query expire then it will cause the entire query to be refetched. When explaining the TTL feature you state:

When an entity is attempted to be read from the cache, it will be lazily evicted if it has been in the cache longer than it's TTL duration (specified in milliseconds) and will trigger any queries watching that data to rerun in order to fetch new data.

So in my scenario this works exactly as you've described, if I have a list of people (with some or many which become expired), upon navigating away and then returning to the list view/page the query is refetched.

My question is, is there a way in which some queries can be ignored when setting the type invalidation policy? e.g.

invalidationPolicies: {
  types: {
    People: {
      ignoreQueries: ['GetPeople'],
      timeToLive: 15000,
      renewalPolicy: RenewalPolicy.WriteOnly,
    },
}

Currently the application we are building has a CQRS backend, meaning that its all event driven. When we send a mutation we simply get a response that the event has been accepted and the action will be done in the near future. On response we modify our cache locally and I was hoping we could use the TTL to persist our changes on write for 15 seconds, upon this becoming expired it should be safe to re query this entity on other calls e.g GetPerson.

However, when I navigate back to our list view we re query as an underlying entity has been modified which can lead to us display stale data as the backend has yet to apply the change.

@danReynolds
Copy link
Collaborator

Got it, I think I understand the problem. So is this the only time you need to evict the People type? 15s after the mutation? If so, I'd recommend using an invalidation policy instead of a TTL which would be setup a timer to remove the People query in 15s using the evict API or just re-fetch the query directly. You can read about invalidation policies here: https://github.com/NerdWalletOSS/apollo-cache-policies#specification-1

If we do need a 15s TTL on the People type then if the mutation happens and the data will be stale if it re-fetches before the backend has updated the entities, then I'm not sure what might be best. I think something like ignoreQueries could complicate the API and be error-prone since other queries might get added that also need to be ignored and it could be hard to keep track of.

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

No branches or pull requests

2 participants