简体   繁体   English

如何为 apollo-angular 中的突变配置缓存?

[英]how to configure the cache for mutations in apollo-angular?

After a mutation (create, update and delete) I need to update the list, so far the documentation is only for React, but BUT I need help to implement it in Angular 12, and the official documentation for angular is poor and seems almost a copy of the documentation for React.在发生突变(创建、更新和删除)之后,我需要更新列表,到目前为止,文档仅适用于 React,但我需要帮助才能在 Angular 12 中实现它,并且 angular 的官方文档很差,看起来几乎是React 文档的副本。

the idea is that I have a list of things, and I go to another route to create or edit, after saving, I need to return to the updated list我的想法是我有一个东西列表,我go到另一个路由创建或编辑,保存后,我需要返回更新列表

I totally agree with you regarding the documentation for apollo-angular.关于 apollo-angular 的文档,我完全同意你的看法。 I don't know if this is still relevant to you, but maybe it can help someone else.我不知道这是否仍然与您有关,但也许它可以帮助其他人。

If I understand your question correctly, I had as similar issue;如果我正确理解您的问题,我也有类似的问题; a mutate that added an object to a list on another object.将 object 添加到另一个 object 的列表中的突变。 After quite a lot of reading I understood that you have to manually update the GraphQL cache to immediately reflect the changes to the list in the UI.经过大量阅读后,我了解到您必须手动更新 GraphQL 缓存以立即反映 UI 中列表的更改。 In my case, the backend returns the whole list as a response to the mutation, so I could simply update the cache with the new list directly.在我的例子中,后端返回整个列表作为对突变的响应,所以我可以直接用新列表更新缓存。

I ended up using writeFragment , as this allows you to write"random-access" data to the GQL cache, which was simpler than writeQuery .我最终使用了writeFragment ,因为这允许您将“随机访问”数据写入 GQL 缓存,这比writeQuery更简单。

    this.apollo
            .mutate({
                mutation: YOUR_MUTATION,
                update: (cache, result) => {
                    cache.writeFragment({
                        id: `[OBJECT_NAME_IN_GQL_CACHE]:[OBJECT_ID_IN_GQL_CACHE]`,
                        fragment: gql`
                           fragment [FRAGMENT_NAME] on [OBJECT_NAME_IN_GQL_CACHE] {
                              [LIST_TO_UPDATE] { [GQL_QUERY] }
                           }
                        `,
                        data: { [LIST_TO_UPDATE]: result.data.[UPDATED_LIST]},
                    });
                },
            })

The bottom part of the Apollo Angular doc (about the update function) helped me to understand how it works. Apollo Angular 文档的底部(关于update功能)帮助我理解了它是如何工作的。 I also used the Apollo Client doc aboutreading and writing data to the cache .我还使用了 Apollo Client 文档关于读取和写入数据到缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM