E
E
Egor Zholnin2019-06-06 13:43:14
React
Egor Zholnin, 2019-06-06 13:43:14

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);
                            }}
                          >

What's in data inside update:
5cf8ed702600e211505336.png
What's in data before mutation inside Query:
5cf8edabb448a540976159.png
What did data look like after mutation:
5cf8ee068f118223818957.png
What could go wrong here?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question