Created
August 29, 2024 12:08
-
-
Save marufsiddiqui/021c3c1f254b81f0db2971637044d4c7 to your computer and use it in GitHub Desktop.
2 way to interact with Apollo Cache
This file contains hidden or 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
function clearWishlist2() { | |
const cache = apolloClient.cache | |
cache.modify({ | |
id: cache.identify({ __typename: 'GraphqlWishlistItems' }), | |
fields: { | |
items(existingItemRefs) { | |
existingItemRefs.forEach((itemRef: Reference) => { | |
cache.modify({ | |
id: cache.identify(itemRef), | |
fields: { | |
priceAlertSubscription() { | |
return false | |
}, | |
priceAlerts(refs: Reference[] | null) { | |
refs?.forEach(ref => { | |
cache.evict({ id: cache.identify(ref) }) | |
}) | |
return null | |
}, | |
}, | |
}) | |
}) | |
return existingItemRefs | |
}, | |
}, | |
}) | |
} | |
function clearWishlist1() { | |
const cache = apolloClient.cache | |
const storeObjects = Object.values(cache.extract()) | |
storeObjects | |
.filter((item: StoreObject) => item.__typename === 'GraphqlWishlistItem') | |
.map((item: StoreObject) => cache.identify(item)) | |
.forEach(id => { | |
cache.modify({ | |
id, | |
fields: { | |
priceAlertSubscription() { | |
return false | |
}, | |
priceAlerts() { | |
return null | |
}, | |
}, | |
}) | |
}) | |
storeObjects | |
.filter((item: StoreObject) => item.__typename === 'GraphqlPriceAlert') | |
.map((item: StoreObject) => cache.identify(item)) | |
.forEach(id => { | |
cache.evict({ | |
id, | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment