Apollo客户端缓存与Redux [英] Apollo Client Cache vs. Redux

查看:130
本文介绍了Apollo客户端缓存与Redux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Redux Store 迁移,以使用Apollo Graphql Client随附的 Apollo Client Cache .

I'm trying to migrate from Redux Store to use Apollo Client Cache that comes with Apollo Graphql Client.

使Apollo Client与其他数据管理解决方案区分开的关键功能之一是其标准化缓存.只需设置Apollo客户端,便可以立即使用智能缓存,而无需进行其他配置.

One of the key features that sets Apollo Client apart from other data management solutions is its normalized cache. Just by setting up Apollo Client, you get an intelligent cache out of the box with no additional configuration required.

使用Redux,我们必须根据从副作用收到的响应来编写动作,类型和调度动作,并使用reducers设置存储中的数据,这是由Apollo Client自动完成的.

With Redux we have to write actions, types and dispatch actions based on the response received from the side-effect and set the data in the store using reducers, which is done by Apollo Client automatically.

问题:

1)从Redux迁移到Apollo Client Cache有什么优势?

1) What are the advantages of moving from Redux to Apollo Client Cache?

2)在迁移到Apollo客户端缓存之前,我应该担心什么吗?

2) Is there anything that I should be worrying about before migrating to Apollo Client Cache?

推荐答案

您正在将苹果与桔子进行比较.是的,reduxapollo-client都可以为您提供一种管理全局应用程序状态的方式.但是,redux允许您创建一个可预测的状态容器,该容器会根据您定义的操作而更改.这意味着redux提供:

You're comparing apples to oranges. Yes, at a high level both redux and apollo-client provide a way for you to manage global application state. However, redux allows you to create a predictable state container that changes in response to the actions you define. That means redux offers:

  • 可预测性.精简器是纯函数-给定相同的状态和动作,精简器将始终产生相同的结果.
  • 可测试性.同样,由于reducer只是函数,因此对它们进行单元测试很简单.
  • 可扩展性.由于redux迫使您以特定方式组织代码,因此使您的代码更具可维护性.即使随着您的代码库的增长,您的代码仍可被其他开发人员调试和理解.

Dan Abromov指出了其他一些好处:

  • 序列化用户操作,并将其与状态快照一起附加到自动错误报告,以便产品开发人员可以重播它们以重现错误.
  • 在网络上传递动作对象以实现协作环境,而无需对代码的编写方式进行重大更改.
  • 保留撤消历史记录或实现乐观的变异,而无需对代码的编写方式进行重大更改.
  • 在开发中的状态历史记录之间移动,并在代码更改时从动作历史记录中重新评估当前状态,例如TDD.
  • 为开发工具提供全面的检查和控制功能,以便产品开发人员可以为其应用构建自定义工具.
  • 在重用大多数业务逻辑的同时提供备用UI.
  • Serialize user actions and attach them, together with a state snapshot, to automated bug reports, so that the product developers can replay them to reproduce the errors.
  • Pass action objects over the network to implement collaborative environments without dramatic changes to how the code is written.
  • Maintain an undo history or implement optimistic mutations without dramatic changes to how the code is written.
  • Travel between the state history in development, and re-evaluate the current state from the action history when the code changes, a la TDD.
  • Provide full inspection and control capabilities to the development tooling so that product developers can build custom tools for their apps.
  • Provide alternative UIs while reusing most of the business logic.

是的,redux带有很多样板.但是,您,您的应用程序和您的团队都可以通过使用它获得许多好处,而不仅仅是拥有一种管理全局状态的方法.另一方面,如果您没有看到redux提供的功能的价值,或者认为它们不值得在代码中使用间接性和复杂性redux,那么请不要使用它.如果您只需要一种管理全局应用程序状态的方法,则可以使用apollo-client或其他一些库,也可以只使用Context API和useReducer挂钩来推出自己的解决方案.

Yes, redux comes with a lot of boilerplate. However, both you, your application and your team can potentially reap a lot of benefits from using it beyond just having a way to manage global state. On the other hand, if you don't see value in the features provided by redux, or don't think they're worth the indirection and complexity redux adds to your code, then don't use it. If all you need is a way to manage global application state, then you can utilize apollo-client or some other library or just utilize the Context API and the useReducer hook to roll your own solution.

Apollo Client使用@client指令来管理本地状态非常方便,尤其是如果您已经在使用该库查询GraphQL API的话.能够轻松地用派生字段修饰查询结果是很巧妙的.能够利用相同的API来查询您的服务器和查询本地状态使DX具有良好的性能.但是apollo-client不能替换 redux,因为在一天结束时,这两个库由于非常不同的原因而做了非常不同的事情.

Apollo Client's use of the @client directive to manage local state is very convenient, especially if you'r already using the library for querying a GraphQL API. Being able to easily decorate query results with derived fields is neat. Being able to utilize the same API for querying your server and querying local state makes for good DX. But apollo-client cannot replace redux because at the end of the day the two libraries do very different things for very different reasons.

这篇关于Apollo客户端缓存与Redux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆