Created
November 3, 2020 18:46
-
-
Save andycarrell/c7f99016a25dc7e48605a969e8b9fa38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback } from "react"; | |
import { useApolloClient } from "@apollo/client"; | |
import { defaultDataIdFromObject } from "@apollo/client/core"; | |
const useApolloCacheEvict = () => { | |
const { cache } = useApolloClient(); | |
/** | |
* Pass entire objects as queried from the cache. | |
* Use Apollo's default function to determine id. | |
*/ | |
const evictObjectsFromCache = useCallback( | |
(...objects) => { | |
objects.map(defaultDataIdFromObject).forEach((id) => { | |
cache.evict({ id }); | |
}); | |
/** | |
* Evicting an object often makes other cached objects unreachable. | |
* Call cache.gc after evicting object(s) from the cache. | |
*/ | |
cache.gc(); | |
}, | |
[cache], | |
); | |
return evictObjectsFromCache; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment