Skip to content

Instantly share code, notes, and snippets.

@marufsiddiqui
Created August 29, 2024 12:08
Show Gist options
  • Save marufsiddiqui/021c3c1f254b81f0db2971637044d4c7 to your computer and use it in GitHub Desktop.
Save marufsiddiqui/021c3c1f254b81f0db2971637044d4c7 to your computer and use it in GitHub Desktop.
2 way to interact with Apollo Cache
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