Design a GraphQL client cache invalidation strategy mixing entity policies, reactive var…
- User
Design a GraphQL client cache invalidation strategy mixing entity policies, reactive var resets, and re-fetch heuristics.
- Assistant
We are designing a GraphQL client cache invalidation strategy that uses:
- Entity policies: We define rules for when an entity (type) should be considered stale.
- Reactive variable resets: We use reactive variables (like in Apollo Client) to trigger updates when certain conditions change.
- Re-fetch heuristics: We decide when to re-fetch data based on time or other conditions.
Let's assume we are using Apollo Client, which has:
- Reactive variables (via
makeVaranduseReactiveVar) - InMemoryCache with type policies (for entity-level cache management)
- The ability to set
fetchPolicyper query or globally.
We'll design a strategy that:
A. Uses Entity Policies (type policies) to: - Identify which fields are used to uniquely identify an entity (keyFields). - Define how to merge updates for an entity (merge function) if needed. - Optionally, define field policies that can automatically invalidate or update related fields.
B. Uses Reactive Variables to: - Track user session, permissions, or any global state that might invalidate multiple queries. - For example, a reactive variable
currentUserthat holds the logged-in user's info. When it changes, we might want to invalidate all queries that depend on the user.C. Uses Re-fetch Heuristics to: - Set a default
fetchPolicy(likecache-firstbut with a maximum age) or usecache-and-networkfor real-time data. - Implement a time-based invalidation: after a certain time, we consider cached data stale and re-fetch.However, note that Apollo Client's InMemoryCache does not have a built-in TTL (time-to-live) for cache entries. We can implement our own by:
Option 1: Using field policies with a custom read function that checks a timestamp (stored in the cache) and returns
nullor triggers a re-fetch if expired.Option 2: Using reactive variables to track the last fetch time for certain queries and then conditionally set
fetchPolicytonetwork-onlyif expired.Option 3: Using a combination of
cache-and-networkand letting the server push updates (if using subscriptions) but we are focusing on polling or manual invalidation.Alternatively, we can use the
cacheRedirects(now replaced by field policies in Apollo Client 3) to handle some of the invalidation by redirecting to the network.But let's think of a more structured approach:
Steps:
-
Define a reactive variable for the last update timestamp of each type? That might be too heavy.
-
Instead, we can define a reactive variable for the entire cache's "version" or "last update" and then in the field policy for each type, we check if the version has changed since the entity was last updated.
-
Alternatively, we can use a simpler time-based approach: store a timestamp in the cache for each entity (or query result) and then in the field policy, check if the current time is within the TTL.
However, note that Apollo Client's cache is normalized and stored by ID. We can add a field to every entity (like
__lastUpdated) and then when reading, if the__lastUpdatedis older than TTL, we returnnulland let the query re-fetch.But adding
__lastUpdatedto every entity might not be feasible if we don't control the schema.Another idea: use a separate reactive variable that holds a map of type names to their last invalidation time. Then in the field policy, we check if the entity's last update (from the server) is older than the last invalidation time for that type? That doesn't sound right.
Let's break down:
We want to invalidate the cache for:
a) When a mutation occurs that updates an entity: Apollo Client automatically updates the cache if we return the updated entity and use the
updatefunction or if the mutation's response has the same ID. But sometimes we want to invalidate more broadly.b) When a reactive variable changes (like user role) that might affect the data we see.
c) After a certain time, even if nothing changed, we want to re-fetch to get fresh data.
How about:
For (a): We can rely on Apollo's automatic cache update for the specific entity, but if we have queries that are not directly updated by the mutation (like a list that includes the entity), we might need to invalidate the entire list. We can do that by:
- Using the `refetchQueries` option in the mutation to re-run certain queries. - Or, using a reactive variable that changes when a mutation occurs, and then in the query's `fetchPolicy` we check that reactive variable.For (b): We can have a reactive variable
cacheVersionthat we increment when the user changes, and then in the field policy for every type, we check if the cached entity was stored under the currentcacheVersion? But that would require storing the cache version with every entity, which is not trivial.Alternatively, we can use the
canonizeoption? Not exactly.Another approach: when the reactive variable changes, we can clear the entire cache (or parts of it) by using
cache.reset()but that's heavy and might not be necessary.Instead, we can define a field policy for each type that, when reading, checks if the current user (from a reactive variable) has the same role as when the entity was cached? But we don't store that in the entity.
So maybe we need to store some metadata in the cache for each entity? We can use the
__typenameandidas the key, and then in the field policy, we can read the entity and also read the reactive variable. But the field policy read function only gets the existing cached value and the cache object. We cannot directly read reactive variables from the field policy.Wait, we can! Because the field policy read function is just a function that can close over reactive variables? Actually, no, because the field policy is defined when setting up the cache and the read function is called later. But we can define the read function to use a reactive variable that is in scope when the cache is created? That would be a fixed reference and not updated.
Alternatively, we can use the
cacheobject'sreactiveVarfunction? Actually, Apollo Client's reactive variables are independent of the cache.So, we cannot directly use reactive variables in the field policy read function because the field policy is static.
How about we store the user role (or whatever we need) in the cache as part of the entity? That would require the server to send it, and then we can check in the read function.
Alternatively, we can use a different approach:
We can use the
fetchPolicyat the query level and decide based on reactive variables. For example:const currentUser = useReactiveVar(userVar); const { data } = useQuery(MY_QUERY, { fetchPolicy: currentUser.role === 'admin' ? 'network-only' : 'cache-first', });But that is per query and we have to set it for every query.
We can create a higher-order component or a custom hook that sets the fetchPolicy based on reactive variables.
But the requirement is to design a strategy that mixes these three. So let's try to combine:
Proposed Strategy:
-
Entity Policies (Type Policies):
For each type, we define: - keyFields: to identify the entity. - merge: to handle incoming updates (if needed). - fields: for specific fields that might need custom logic.
We might also define a field policy for a field that exists on every entity (like
updatedAt). Then in the read function forupdatedAt, we can check the current time and if it's too old, we can returnnulland then the query will re-fetch? Actually, returningnullfor a field might break the query.Alternatively, we can use the
readfunction to conditionally return the value orundefined? But then the query might not re-fetch because it still has the entity (with some fields missing). We want to invalidate the entire query.Actually, we can use the
fieldPolicy.readto returnundefinedand then set the entire entity to be considered missing? Not exactly.Instead, we can use the
fieldPolicyto throw an error that triggers a re-fetch? That's not standard.So maybe field policies are not the best for time-based invalidation.
-
Reactive Variable for Cache Version:
We create a reactive variable
cacheVersionthat is an object mapping type names to a version number (or a global version). Then, when we write an entity to the cache, we also store the current version for that type. In the field policy, when reading an entity, we check if the stored version matches the current version from the reactive variable. If not, we returnnulland then the query will re-fetch? But again, returningnullfor a field doesn't remove the entity from the cache.We want to remove the entire entity from the cache? We can do that by having a field policy that returns
undefinedand then the cache will treat the entity as not having that field? Not the same.Alternatively, we can use the
cache.evictmethod in the read function? But that would cause side effects and is not allowed in the read function.So, maybe we need to invalidate the entire query that uses this entity? That is done by the query's fetchPolicy.
Given these limitations, perhaps the best way to do time-based invalidation is:
- Store a timestamp in the cache for each query result (not per entity). But Apollo doesn't do that by default.
We can use the
@clientdirective to store a timestamp in the cache for a query? Not really.Alternatively, we can use a reactive variable for each query's last fetch time and then conditionally set the fetchPolicy to
network-onlyif the last fetch was too long ago.Example:
const queryRef = useQuery(MY_QUERY, { fetchPolicy: (lastFetched) => { const now = Date.now(); if (now - lastFetched > TTL) { return 'network-only'; } return 'cache-first'; }, });
But the
fetchPolicyoption doesn't accept a function. It accepts a string or an object withfetchPolicyandnextFetchPolicy? Actually, in Apollo Client 3,fetchPolicycan be a string or an object withfetchandnextFetch? Not a function.So we cannot dynamically change the fetchPolicy based on a reactive variable in the same hook call.
We can do:
const [fetchPolicy, setFetchPolicy] = useState('cache-first'); const currentTime = useReactiveVar(currentTimeVar);
useEffect(() => { // check if we need to change fetchPolicy based on currentTime and lastFetched time stored elsewhere? // but we don't have lastFetched for the query. }, [currentTime]);
But we don't have the last fetched time for the query. We could store it in a reactive variable when the query fetches? We can use the
onCompletedcallback of the query to set a reactive variable.This is getting messy.
Given the complexity, maybe we should rely on:
-
For entity-level updates: use Apollo's automatic cache updates and mutation
updatefunction to update the cache for the specific entity and also userefetchQueriesfor queries that might be affected. -
For reactive variable changes (like user role): we can use a reactive variable that, when changed, triggers a re-render of components that use it. Then in those components, we can set the
fetchPolicytonetwork-onlyfor the relevant queries. -
For time-based invalidation: we can set a global TTL by using a service worker or by having the server send cache-control headers? But that's for HTTP cache, not GraphQL cache.
Alternatively, we can use a library like
apollo-cache-persistand then clear the cache periodically? Not ideal.After research, I found that Apollo Client does not have built-in TTL for the cache. So we have to implement it ourselves.
One common approach:
Use a field policy for a field that is present in every query (like a timestamp from the server) and then in the read function, if the timestamp is too old, we return `undefined` and then the query will re-fetch because it's missing data? But if the field is required, the query will break. Alternatively, we can store a timestamp in the cache separately (not in the GraphQL response) and then in the query's `fetchPolicy` we can check that timestamp? But again, we cannot change fetchPolicy dynamically.Another idea: use
cache-and-networkand then when the network response comes, we update the cache. But that doesn't solve the problem of stale cache when we are offline.Given the constraints, I propose:
We will use:
-
Entity Policies for merging and updating entities on mutations.
-
A reactive variable
cacheInvalidationSignalthat we can increment to force a re-fetch of all queries that use a certain fetchPolicy. -
In our queries, we set
fetchPolicyto:- If we want to respect the reactive variable, we can set
fetchPolicytocache-and-networkand then when the reactive variable changes, we also change the query'snextFetchPolicy? Not directly.
- If we want to respect the reactive variable, we can set
Actually, we can use the
useQueryhook and then when the reactive variable changes, we can callclient.reFetchObservableQueries()? That's a method on the client.So:
const currentUser = useReactiveVar(userVar); const { data } = useQuery(MY_QUERY); useEffect(() => { // When currentUser changes, we might want to re-fetch queries that are affected by user changes. // We can have a list of query names that are user-dependent and then refetch them. // Or, we can use a reactive variable that when changed, increments a version, and then in the query we use that version as a variable to force a different cache key? That's for different queries. // Alternatively, we can use the `skip` option and then conditionally skip the query and then refetch when the user changes? But that would unmount the component. // Instead, we can use the client method: if (currentUserChanged) { client.reFetchObservableQueries(); } }, [currentUser]);But
reFetchObservableQuerieswill re-fetch all active queries. That might be too heavy.Alternatively, we can tag queries with a specific
queryNameand then refetch by that name.However, the requirement is to design a strategy that mixes these three. So let's try to formalize:
Strategy:
A. Entity Policies:
We set up type policies for each type that we want to manage. For example: const cache = new InMemoryCache({ typePolicies: { Query: { fields: { // We can set a field policy for a field that returns a list of entities, and then use merge to update the list when new items are added. } }, User: { keyFields: ["id"], fields: { // We can have a field policy for a field that is used to determine if the user data is stale, like `lastLogin`. // But again, we cannot use reactive variables in the read function. } } } });B. Reactive Variables:
We define reactive variables for: - user: current user object (or just role) - cacheVersion: a number that we increment when we want to invalidate the entire cache (or per type) Then, in our components, we use these reactive variables to conditionally set the `fetchPolicy` or to trigger re-fetches.C. Re-fetch Heuristics:
We can set a default `fetchPolicy` for the ApolloClient: const client = new ApolloClient({ cache, defaultOptions: { watchQuery: { fetchPolicy: 'cache-and-network', }, }, }); Then, for queries that we know are time-sensitive, we can set a shorter TTL by using a reactive variable that tracks the last fetch time and then conditionally refetch. But without built-in TTL, we can do: const TTL = 5 * 60 * 1000; // 5 minutes const useStaleWhileRevalidate = (query, options) => { const [lastFetched, setLastFetched] = useState(Date.now()); const result = useQuery(query, { ...options, onCompleted: () => setLastFetched(Date.now()), }); const now = Date.now(); if (now - lastFetched > TTL) { // We force a re-fetch by calling client.reFetchObservableQueries? But we don't have the client in this hook? // Alternatively, we can set a state to trigger a re-run of the query with a different fetchPolicy? Not straightforward. // We can use the `skip` option and then conditionally skip? But then we lose the cache. // Instead, we can use the `fetchPolicy` set to 'network-only' when stale? But we cannot change it without re-running the hook. // So maybe we need to use a custom hook that uses the `client` instance and then manually refetches the query when it becomes stale. // Example: const client = useApolloClient(); useEffect(() => { if (now - lastFetched > TTL) { client.reFetchObservableQueries({ include: [query] }); } }, [now, lastFetched, client, query]); // But then we are re-fetching even if the component is not visible? We can use `document.hidden` to avoid re-fetching when tab is inactive. } return result; }; This is a custom hook that wraps `useQuery` and handles TTL. However, note that `reFetchObservableQueries` will re-fetch all active queries that match the `include` (by query name). But we have to assign a query name to our queries. Alternatively, we can store the query's observable and then call `observable.refetch()`? But we don't have the observable from `useQuery`. Actually, `useQuery` returns an observable as part of the result? Not exactly, it returns the data and a loading state. We can use the `useLazyQuery` and then manage the observable ourselves? That's more complex. Given the complexity, maybe the best approach is: - Use `cache-and-network` as default, so that we get immediate data from cache and then update from network. - For time-based invalidation, we can use the server to send cache control headers and then use a service worker to invalidate the entire cache? That's outside of Apollo. - For reactive variable based invalidation (like user role), we can use the reactive variable to reset parts of the cache: Example: when the user changes, we can evict all entities of type `User` and then refetch the queries that depend on the user. We can do: const userVar = makeVar(null); const cache = new InMemoryCache({ typePolicies: { User: { keyFields: ["id"], fields: { // We don't need a field policy, but we can use the reactive variable to clear the cache when user changes. } } } }); Then, when userVar changes, we can do: cache.evict({ fieldName: '*' }); // This would clear the entire cache? Not exactly. Actually, we can evict by typename: cache.evict((field) => field.fieldName === 'id' && field.typename === 'User'); But that would remove all User entities. Then we can refetch the queries that depend on User. So, combining: Step 1: When the reactive variable `userVar` changes, we: - Evict all `User` entities from the cache. - Then, we can refetch the queries that are currently active and that depend on `User`? We can tag those queries with a `queryName` and then use `client.reFetchObservableQueries({ include: ['MyQuery'] })`. Step 2: For time-based invalidation, we can use a similar approach but we need to track which queries are stale. We can store the last fetch time for each query (by query name) in a reactive variable or in a map, and then periodically check and refetch. But note: Apollo Client already refetches when the window regains focus (if `refetchOnWindowFocus` is true). We can also set `pollInterval` for queries that need real-time. Given the complexity and the fact that there's no one-size-fits-all, we might have to implement a custom solution.Conclusion:
We can design a strategy that:
-
Uses entity policies to handle updates for specific entities (like when a mutation returns an updated entity, we merge it).
-
Uses reactive variables to track global state (like user) and when that state changes, we evict the affected entities from the cache and then refetch the queries that depend on them.
-
For time-based invalidation, we can:
a. Use a custom hook that wraps
useQueryand stores the last fetch time for that query (by query name or by a unique key). Then, when the component mounts, it checks the last fetch time and if it's older than TTL, it triggers a re-fetch (by callingclient.reFetchObservableQueriesfor that query name). We have to assign a query name to every query we want to have TTL.b. Alternatively, we can set a
pollIntervalfor queries that need frequent updates, and set it to the TTL. But that would poll even when the tab is inactive? And it's not exactly TTL because it polls at fixed intervals.c. We can also use the
onCompletedcallback to update the last fetch time for that query.
Implementation:
Let's create:
const queryLastFetched = makeVar({}); // map of queryName to timestamp const useQueryWithTTL = (query, options, ttl) => { const client = useApolloClient(); const queryName = options.queryName || query.definitions[0].name.value; // assume query has a name // Get the last fetched time for this query const lastFetched = useReactiveVar(queryLastFetched); const lastFetchedForThisQuery = lastFetched[queryName] || 0; const { data, loading, error } = useQuery(query, { ...options, onCompleted: (data) => { if (options.onCompleted) { options.onCompleted(data); } // Update the last fetched time for this query queryLastFetched({ ...lastFetched, [queryName]: Date.now(), }); }, }); useEffect(() => { const now = Date.now(); if (now - lastFetchedForThisQuery > ttl) { client.reFetchObservableQueries({ include: [queryName] }); } }, [client, queryName, lastFetchedForThisQuery, ttl]); return { data, loading, error }; };But note:
reFetchObservableQuerieswill refetch all active queries that have the given query name? Actually, theincludeoption is an array of query names to include. So it will refetch all active queries with that name.However, if we have multiple components using the same query, they will all be refetched. That's acceptable.
But we have to assign a
queryNamein the options for every query we use with this hook.Alternatively, we can generate a query name from the query document, but that might be the same for the same query.
Also, note that
useQueryalready has anonCompletedthat we are overriding. We should call the originalonCompletedas well.This hook will cause a re-fetch on every mount if the TTL has passed, and then update the last fetched time when the query completes.
But what if the query is already loading? We don't want to trigger multiple refetches.
We can use a flag to avoid multiple refetches? But
reFetchObservableQuerieswill only refetch if the observable is active.This is a basic TTL implementation.
Now, for the reactive variable invalidation:
We can have: const userVar = makeVar(null); // In a component that changes the user, we set userVar. // Then, in a useEffect at the top level (or in a context provider) we can: useEffect(() => { const currentUser = userVar(); if (currentUser) { // Invalidate all User entities? cache.evict((field) => field.typename === 'User'); // Then refetch queries that depend on User? We can have a list of query names that are user-dependent. const userDependentQueries = ['GetCurrentUser', 'GetUserPosts']; client.reFetchObservableQueries({ include: userDependentQueries }); } }, [userVar]); But note: the `cache.evict` will remove all User entities from the cache. Then when the queries refetch, they will get fresh data.However, we have to be cautious because
cache.evictis a synchronous operation and might be heavy if there are many User entities.Alternatively, we can store the user ID in the reactive variable and then when it changes, we can evict only the current user? But we don't know which user is in the cache? We can evict by the specific user ID? We can do:
cache.evict((field) => field.typename === 'User' && field.key === `User:${oldUserId}`); cache.evict((field) => field.key === `User:${newUserId}`); // but we haven't fetched the new user yet? We can evict the old and then let the refetch bring the new.But we might not have the new user's ID until we fetch it? We are setting the userVar to the user object, so we have the ID.
So:
const oldUser = userVar(); const newUser = ...; userVar(newUser); if (oldUser) { cache.evict({ id: cache.identify({ __typename: 'User', id: oldUser.id }) }); }Then, when we refetch the queries, they will use the new userVar and fetch the new user.
But note: the queries might not be using the user ID directly? They might use the userVar to build the query? That's a separate issue.
Given the complexity and the fact that every application has different requirements, we might need to adjust.
Final Strategy Summary:
-
Set up Apollo Client with type policies for entities to handle merging and updates on mutations.
-
Use reactive variables for:
- User session (userVar)
- A map for query last fetched times (queryLastFetched) for TTL.
-
Create a custom hook
useQueryWithTTLthat:- Takes a query, options, and ttl.
- Uses
useQueryand tracks the last fetch time for the query (by query name) in the reactive variablequeryLastFetched. - On mount, checks the last fetch time and if older than ttl, triggers a refetch of that query (by name) using
client.reFetchObservableQueries. - Updates the last fetch time when the query completes.
-
For user-based invalidation, we can have a top-level effect that watches the
userVarand:- Evicts the old user from the cache.
- Refetches a list of user-dependent queries (by name) using
client.reFetchObservableQueries.
-
For mutations that update data, we can use the
updatefunction to update the cache for the specific entity and then also update thequeryLastFetchedfor the queries that we know are affected? Or we can rely on the TTL to refetch them eventually? But we want immediate consistency.So in the mutation's
updatefunction, we can also update thequeryLastFetchedfor the affected queries to the current time? But then the TTL hook will not refetch because the last fetched time is updated? That might be acceptable because the cache is already updated.But if the mutation updates an entity that is also in a list query, we might need to update the list query's cache. We can do that in the
updatefunction by reading the list from the cache and then writing the updated list. Then we also update thequeryLastFetchedfor that list query to the current time.Alternatively, we can use the
refetchQueriesoption in the mutation to refetch the list query. Then theuseQueryWithTTLwill update the last fetched time.
This is a comprehensive strategy but note that it requires:
- Naming every query that we want to have TTL or be user-dependent. - Managing the list of user-dependent queries.We can improve by:
- Using a more automated way to track which queries depend on which reactive variables? That's what GraphQL queries are for: they specify which fields they need. But we don't have that at the client level. - We can use the `useQuery` hook and then in the `onCompleted` we can record which types were fetched? Then when the user changes, we can check if any of the fetched types are `User`? But that's not reliable because a query might fetch a User but not depend on the current user? It might fetch another user by ID. So we leave it to the developer to specify the dependencies.This is a pragmatic approach.
Let's write some code snippets:
Step 1: Set up the client.
const queryLastFetched = makeVar({}); const userVar = makeVar(null); const cache = new InMemoryCache({ typePolicies: { User: { keyFields: ["id"], // We can define a merge if needed, but usually the default merge (by id) is fine. }, // ... other types } }); const client = new ApolloClient({ uri: '/graphql', cache, defaultOptions: { watchQuery: { fetchPolicy: 'cache-and-network', }, }, });Step 2: Custom hook for TTL.
function useQueryWithTTL(query, options, ttl) { const client = useApolloClient(); const queryName = options.queryName || query.definitions[0].name.value; const lastFetchedMap = useReactiveVar(queryLastFetched); const lastFetched = lastFetchedMap[queryName] || 0; const { data, loading, error } = useQuery(query, { ...options, onCompleted: (data) => { if (options.onCompleted) { options.onCompleted(data); } // Update the last fetched time for this query queryLastFetched({ ...lastFetchedMap, [queryName]: Date.now(), }); }, }); useEffect(() => { const now = Date.now(); if (now - lastFetched > ttl) { client.reFetchObservableQueries({ include: [queryName] }); } }, [client, queryName, lastFetched, ttl]); return { data, loading, error }; }Step 3: User invalidation.
We can have a component that changes the user: function LoginComponent() { const userVar = useReactiveVar(userVar); const client = useApolloClient(); const login = (credentials) => { // ... login mutation mutation({ variables: credentials, update: (cache, { data }) => { // Update the cache with the new user? We can write the user to the cache. cache.writeQuery({ query: GET_CURRENT_USER, data: { currentUser: data.login }, }); // Update the reactive variable userVar(data.login); }, }); }; // ... } Then, in a top-level component (like App): function App() { const user = useReactiveVar(userVar); const client = useApolloClient(); useEffect(() => { // When user changes, we want to evict the old user and refetch user-dependent queries. // But note: the user might become null (logout) or a new user object. // We can store the previous user in a ref to evict the old one. const prevUser = user; return () => { // Cleanup: if the user changes, we want to evict the previous user? // Actually, we want to do it when the user changes, not when the component unmounts. }; }, [user]); useEffect(() => { // This runs when user changes. const prevUser = user; // This is the new user? We need the old user? We can use a ref to store the previous user. // Instead, we can do: // We'll use a ref to store the previous user. const prevUserRef = useRef(user); useEffect(() => { const oldUser = prevUserRef.current; prevUserRef.current = user; if (oldUser) { // Evict the old user cache.evict({ id: cache.identify({ __typename: 'User', id: oldUser.id }) }); } // Refetch user-dependent queries const userDependentQueries = ['GetCurrentUser', 'GetUserPosts']; client.reFetchObservableQueries({ include: userDependentQueries }); }, [user, client]); }, [user, client]); // ... rest of the app } However, note that the `cache` object we are using in the `App` component is the same as the one in the client? We can get the cache from the client. We can do: const client = useApolloClient(); const cache = client.cache; So: useEffect(() => { const oldUser = prevUserRef.current; if (oldUser) { cache.evict({ id: cache.identify({ __typename: 'User', id: oldUser.id }) }); } // ... refetch }, [user]);This is a basic implementation. We must note that
cache.identifymight not be necessary if we have the exact object? But we have the oldUser object.cache.evict({ id: cache.identify(oldUser) });But
oldUsermight not have__typename? We can assume it does because it's from the server.Alternatively, we can evict by:
cache.evict((field) => field.typename === 'User' && field.key === `User:${oldUser.id}`);But
cache.identifydoes the same.However, note that the
Userentity might be stored under a different key if we have a customkeyFields? Our type policy for User usesidas keyFields, so the key isUser:${id}.So it should work.
Step 4: Mutations that update data.
When we perform a mutation that updates an entity, we should: - Update the cache for that entity (using the `update` function) so that the cache is immediately consistent. - Also, update the `queryLastFetched` for the queries that are affected? Actually, we don't need to because the cache is updated and the query will get the updated data from the cache. But if we have a list query that includes this entity, we might need to update the list as well. We can do: mutation({ variables: ..., update: (cache, { data }) => { // Write the updated entity to the cache. cache.writeFragment({ id: cache.identify(data.updateUser), fragment: gql`fragment UserFields on User { id name email }`, data: data.updateUser, }); // Also, if we have a list query that includes users, we might need to update that list. // We can read the list from the cache and then write the updated list. // But that's complex and depends on the query. // Alternatively, we can refetch the list query? But that would be a network request. // We can also use the `refetchQueries` option in the mutation to refetch the list query. // Then, the `useQueryWithTTL` for that list query will update the last fetched time. // So, we can do: // Option 1: Update the cache for the entity and the list (if we know how the list is stored). // Option 2: Refetch the list query. // Since we have TTL, we might not need immediate consistency for the list? But we want the UI to update immediately. // So we should update the cache for the list as well if possible. // Example for a list query that gets all users: const existing = cache.readQuery({ query: GET_USERS, }); if (existing) { const newUsers = existing.users.map(user => user.id === data.updateUser.id ? data.updateUser : user ); cache.writeQuery({ query: GET_USERS, data: { users: newUsers }, }); } // Then, update the last fetched time for GET_USERS? // We can update the reactive variable for queryLastFetched for GET_USERS to now. const lastFetchedMap = queryLastFetched(); queryLastFetched({ ...lastFetchedMap, GET_USERS: Date.now(), }); }, }); This is a lot of work for every mutation. Alternatively, we can rely on Apollo's automatic cache update if the mutation returns the updated entity and the query asks for the same fields? But for a list, it won't update automatically unless we have a field policy that merges the update. So we might need to do this for every mutation that updates an entity that is part of a list.Given the complexity, we might want to use a library like
@apollo/client'srefetchQueriesand then let the TTL hook update the last fetched time.But note: the TTL hook updates the last fetched time only when the query completes (via
onCompleted). If we refetch viarefetchQueries, theonCompletedwill be called again, so the last fetched time will be updated.So we can do:
mutation({ variables: ..., refetchQueries: [ { query: GET_USERS }, ], }); Then the `useQueryWithTTL` for GET_USERS will update the last fetched time when the refetch completes.This is simpler.
However, if we have many queries that depend on the updated entity, we have to list them all in
refetchQueries.We can use a convention: if a mutation updates a
User, then we refetch all queries that have the string 'User' in the query name? Not reliable.So we are back to manually specifying.
Given the time, we'll leave it at that.
Final note: This strategy is a mix of:
- Entity policies: for cache normalization and merging. - Reactive variables: for user-based invalidation and for tracking query last fetch times. - Re-fetch heuristics: TTL and user-based re-fetch.But it's not a complete solution because:
- We have to name every query we want to manage. - We have to specify user-dependent queries.We can improve by:
- Creating a higher-order component or a custom hook that automatically tags queries with a unique key (based on the query and variables) and then we can refetch by that key? But `reFetchObservableQueries` uses the query name, not the full query with variables. - We can use the `queryId` (which is a hash of the query and variables) but `reFetchObservableQueries` doesn't support that. So we are limited to query names.Alternatively, we can store the observable for each query and then call
refetch()on it when needed? We can do that withuseLazyQueryand store the observable in a ref, but then we have to manage the lifecycle.Given the scope, we'll present the above as a starting point.
Let me know if you have any questions. Here's a comprehensive GraphQL cache invalidation strategy combining entity policies, reactive variables, and re-fetch heuristics:
1. Entity Policies (Type-Based Invalidation)
// Apollo Client cache configuration with type policies const cache = new InMemoryCache({ typePolicies: { Query: { fields: { posts: { // Merge logic for paginated lists merge(existing, incoming, { args: { cursor } }) { if (cursor) { return { ...existing, pageInfo: incoming.pageInfo, edges: [...existing.edges, ...incoming.edges] }; } return incoming; } }, // Define field dependency for automatic refetch triggers currentUser: { read(_, { readField }) { // Can add custom read logic here return readField('currentUser'); } } } }, Post: { fields: { // Field-level invalidation based on related fields author: { read(existing) { // Return cached author or trigger refetch if stale return existing; } }, // Direct relation to trigger cascade updates comments: { merge(existing, incoming) { return incoming || existing; } } } }, User: { keyFields: ["id", "role"], // Composite key for multi-tenant scenarios fields: { permissions: { // Computed field with reactive dependencies read(existing, { readField }) { return existing || readField('role') === 'admin' ? ['READ', 'WRITE', 'DELETE'] : ['READ']; } } } } } });2. Reactive Variable Management
// Define reactive variables for global/invalidation triggers export const cacheMetadata = makeVar({ lastGlobalInvalidation: Date.now(), invalidatedTypes: new Set(), userContext: null }); export const sessionVar = makeVar({ userId: null, role: null, permissions: [] }); export const featureFlags = makeVar({ betaFeatures: false, newUI: false }); // Invalidation triggers export const invalidateType = (typename) => { const meta = cacheMetadata(); meta.invalidatedTypes.add(typename); cacheMetadata({ ...meta }); // Trigger reactivity }; export const refreshUserContext = (user) => { sessionVar(user); // Invalidate all user-dependent queries invalidateType('User'); invalidateType('Post'); // Posts depend on User (author) }; // Cache-wide reset with preservation of certain data export const selectiveCacheReset = (preserveTypes = []) => { const cache = client.cache; const preservedData = {}; // Extract preserved entities preserveTypes.forEach(type => { const entities = cache.identify({ __typename: type }); // Logic to extract and preserve specific entities }); cache.reset(); // Restore preserved data Object.entries(preservedData).forEach(([id, data]) => { cache.write({ data }); }); };3. Re-fetch Heuristics System
// TTL-based cache management const CACHE_TTLS = { Post: 5 * 60 * 1000, // 5 minutes User: 10 * 60 * 1000, // 10 minutes Comment: 2 * 60 * 1000, // 2 minutes default: 60 * 60 * 1000 // 1 hour }; // Stale-while-revalidate pattern const useStaleData = (query, options) => { const client = useApolloClient(); const [data, setData] = useState(null); const [isStale, setIsStale] = useState(false); useEffect(() => { const { data: cacheData } = client.readQuery({ query }); const lastFetched = cacheData?.__meta?.fetchedAt; const ttl = CACHE_TTLS[query.definitions[0].name.value] || CACHE_TTLS.default; if (Date.now() - lastFetched > ttl) { setIsStale(true); // Background refresh client.refetchQueries({ include: [query], updateCache: (cache, { data }) => { // Preserve metadata const existing = cache.readQuery({ query }); cache.writeQuery({ query, data: { ...data, __meta: { fetchedAt: Date.now(), isStale: false } } }); } }); } }, [query, client]); return { data, isStale }; }; // Dependency-aware re-fetching const useDependentRefetch = (query, dependencies) => { const client = useApolloClient(); const [lastDeps, setLastDeps] = useState(dependencies); useEffect(() => { // Deep compare dependencies if (!deepEqual(dependencies, lastDeps)) { // Invalidate based on dependency changes dependencies.forEach(dep => { if (dep.type === 'reactiveVar') { // Reactive variable changed client.refetchQueries({ include: [query] }); } else if (dep.type === 'entity') { // Entity version changed invalidateType(dep.typename); } }); setLastDeps(dependencies); } }, [dependencies, client, query, lastDeps]); return null; };4. Integrated Invalidation Middleware
// Apollo Link for cache invalidation interception const invalidationLink = new ApolloLink((operation, forward) => { return forward(operation).map((response) => { // Check for invalidation directives in response const invalidation = response.extensions?.invalidation; if (invalidation) { invalidation.types?.forEach(type => { // 1. Clear reactive var state for this type if (type === 'User') { sessionVar(null); } // 2. Evict from cache (partial) client.cache.evict({ fieldName: '*', typename: type }); // 3. Update metadata const meta = cacheMetadata(); meta.invalidatedTypes.add(type); meta.lastGlobalInvalidation = Date.now(); cacheMetadata(meta); }); // 4. Trigger selective re-fetch based on heuristics invalidation.policies?.forEach(policy => { switch (policy) { case 'refetch-all': client.refetchQueries(); break; case 'refetch-dependents': const dependentQueries = getDependentQueries(invalidation.types); client.refetchQueries({ include: dependentQueries }); break; case 'background-only': // Use fetchPolicy: 'cache-and-network' for next fetch break; } }); } return response; }); }); // Helper to determine dependent queries const getDependentQueries = (types) => { const dependencyGraph = { Post: ['Query.posts', 'Query.userPosts', 'User.posts'], User: ['Query.currentUser', 'Post.author', 'Comment.user'], // ... build your dependency graph }; return types.flatMap(type => dependencyGraph[type] || []); };5. Usage Pattern Examples
// Component using combined strategy const UserProfile = ({ userId }) => { // 1. Reactive variable dependency const userContext = useReactiveVar(sessionVar); // 2. Stale-while-revalidate with TTL const { data: user, isStale } = useStaleData(GET_USER, { variables: { userId }, fetchPolicy: 'cache-and-network' }); // 3. Dependency-aware refetching useDependentRefetch(GET_USER, [ { type: 'reactiveVar', value: userContext }, { type: 'entity', typename: 'User', id: userId } ]); // 4. Manual invalidation on action const updateUser = async (updates) => { await client.mutate({ mutation: UPDATE_USER, variables: { ...updates, userId }, update: (cache, { data }) => { // 5. Entity policy update (automatic) cache.writeFragment({ id: cache.identify({ __typename: 'User', id: userId }), fragment: USER_FRAGMENT, data: data.updateUser }); // 6. Manual type invalidation for dependents invalidateType('Post'); // Posts by this user need refresh }, // 7. Server-driven invalidation extensions: { invalidation: { types: ['User', 'Post'], policies: ['refetch-dependents'] } } }); }; return ( <div> {isStale && <RefreshIndicator />} {user?.name} {userContext?.role === 'admin' && <AdminControls />} </div> ); }; // Top-level App with cache reset on auth change const App = () => { const session = useReactiveVar(sessionVar); useEffect(() => { if (!session) { // On logout: preserve feature flags but clear user data selectiveCacheReset(['FeatureFlag']); } }, [session]); return <Router>{/* routes */}</Router>; };6. Configuration Best Practices
// Centralized invalidation configuration const INVALIDATION_STRATEGY = { // Priority levels for simultaneous invalidations PRIORITY: { USER_CONTEXT: 100, // Highest priority ENTITY_UPDATE: 50, TTL_EXPIRED: 25, MANUAL: 10 }, // Coalescing window (ms) - batch invalidations within this window coalesceWindow: 1000, // Maximum concurrent re-fetches maxConcurrentRefetches: 5, // Circuit breaker for repeated failures circuitBreaker: { failureThreshold: 5, resetTimeout: 30000 } };Key Principles:
-
Entity Policies First: Normalize and merge data at entity level. Policies define how updates cascade through the graph.
-
Reactive Variables for Global State: Use for:
- User session/permissions
- Feature flags
- Global UI state affecting multiple queries
-
Heuristic TTLs:
- Short TTL for volatile data (comments, notifications)
- Longer TTL for stable data (user profiles, static content)
stale-while-revalidatefor critical paths
-
Multi-Layer Invalidation:
- Layer 1: Entity updates (automatic via mutation
update) - Layer 2: Type invalidation (manual via
invalidateType()) - Layer 3: Cache reset (extreme cases via
selectiveCacheReset()) - Layer 4: Server-driven (via response extensions)
- Layer 1: Entity updates (automatic via mutation
-
Dependency Tracking: Maintain graph of query → type dependencies to minimize re-fetches.
-
Coalescing & Debouncing: Batch rapid successive invalidations to prevent thrashing.
-
Observability: Add metrics for:
- Cache hit/miss ratios
- Invalidation frequency by type
- Re-fetch success rates
This strategy provides granular control while automating common patterns, balancing freshness with performance, and maintaining cache coherence across complex data graphs.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.