Answer the question
In order to leave comments, you need to log in
What is the correct way to update the cache in Apollo?
I update the cache after the mutation as follows:
<Mutation
mutation={ADD_PRICE_CATEGORY}
update={(cache, payload) => {
let data = cache.readQuery({
query: GET_PRICE_CATEGORY_TEMPLATES
});
const currentPriceCategoryTemplate = data.priceCategoryTemplate.getList.list.find(
item => item.id === props.currentItem.id
);
currentPriceCategoryTemplate.priceCategories = [
...currentPriceCategoryTemplate.priceCategories,
path(['data', 'priceCategory', 'add'], payload)
];
data = {
...data,
priceCategoryTemplate: {
...data.priceCategoryTemplate,
getList: {
...data.priceCategoryTemplate.getList,
list: [
...data.priceCategoryTemplate.getList.list.filter(
item =>
item.id !==
currentPriceCategoryTemplate.id
),
currentPriceCategoryTemplate
]
}
}
};
cache.writeQuery({
query: GET_PRICE_CATEGORY_TEMPLATES,
data
});
props.setIsNewPriceCategory(false);
}}
>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question