refactor(web): reduce magic in identifying apollo cache item

This commit is contained in:
Pujit Mehrotra
2024-11-05 09:21:03 -05:00
parent 11c160835a
commit 9d3397a687
2 changed files with 8 additions and 3 deletions

View File

@@ -2,4 +2,5 @@
* The magic 'Notification' prefix comes from inspecting the apollo cache.
* I think it comes from the __typename when apollo caches an object (by default)
*/
export const NOTIFICATION_CACHE_PREFIX = "Notification";
export const NotificationType = "Notification";

View File

@@ -1,6 +1,6 @@
import { InMemoryCache, type InMemoryCacheConfig } from "@apollo/client/core";
import { mergeAndDedup } from "./merge";
import { NOTIFICATION_CACHE_PREFIX } from "./prefixes";
import { NotificationType } from "../../composables/gql/typename";
/**------------------------------------------------------------------------
* ! Understanding Cache Type Policies
@@ -90,7 +90,11 @@ const defaultCacheConfig: InMemoryCacheConfig = {
*/
merge(_, incoming, { cache, args }) {
if (args?.id) {
cache.evict({ id: `${NOTIFICATION_CACHE_PREFIX}:${args.id}` });
const id = cache.identify({
id: args.id,
__typename: NotificationType,
});
cache.evict({ id });
}
// Removes references to evicted notification, preventing dangling references
cache.gc();