阿波罗客户端缓存与Redux的对比[英] Apollo Client Cache vs. Redux

本文是小编为大家收集整理的关于阿波罗客户端缓存与Redux的对比的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试从 redux商店迁移使用 apollo客户端缓存随附Apollo GraphQl客户端.

设置Apollo客户端与其他数据管理解决方案不同的关键功能之一是归一化缓存.仅通过设置Apollo客户端,您就可以从包装盒中获得一个智能缓存,而无需其他配置.

使用Redux,我们必须根据从副作用收到的响应来编写操作,类型和调度操作,并使用还原器在商店中设置数据,该数据由Apollo Client自动完成.

问题:

1)从redux到阿波罗客户端缓存有什么优点?

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

推荐答案

您正在将苹果与橙子进行比较.是的,redux和apollo-client在高级别上都提供了一种管理全局应用程序状态的方法.但是,redux允许您创建一个可预测的状态容器,该容器可根据您定义的操作进行更改.这意味着redux提供:

  • 可预测性.还原器是纯粹的功能 - 给定相同的状态和动作,还原器将始终产生相同的结果.
  • 可检验性.同样,由于还原器只是功能,因此单位测试它们很容易.
  • 可伸缩性.因为Redux迫使您以特定的方式组织代码,所以它使您的代码更加可维护.即使您随着代码库的增长,您的代码仍然可以通过其他开发人员进行讨论和理解.

dan Abromov指出了其他一些好处/p>

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

Apollo客户端对管理本地状态的指令的使用非常方便,尤其是如果您已经使用库查询GraphQl API的情况.能够轻松地使用派生字段装饰查询结果是整洁的.能够利用相同的API查询服务器并查询本地状态可提供良好的DX.但是apollo-client不能 替换 redux,因为在一天结束时,两个库的原因非常不同.

其他推荐答案

我认为您在这里提出了一个很好的观点:"使用Redux,我们必须根据从副作用收到的响应来编写操作,类型和调度操作,并使用还原器在商店中设置数据,由Apollo客户端自动完成."

对于副作用,redux势在必行,阿波罗是声明性的.声明代码通常较短,因为您将逻辑委派到库/框架.


丹尼尔·雷登(Daniel Readden)很好地说,比较redux和阿波罗客户端缓存就像苹果和橙色.这里的苹果和橘子是状态的不同类型,特别是远程和 local 状态.不幸的是,Redux鼓励我们对所有状态进行相同的状态.

我将利用需要检索,更新和在服务器上进行突变的状态的Apollo缓存.我将找到更轻的工具,例如React的上下文API,以防止Prop钻孔,全球应用程序状态和业务逻辑挂钩(例如,用户使用/USESTATE).

棘手的部分是远程状态和本地/全球应用状态混合.因此,我会谨慎地定义围绕它们如何相互作用的模式

其他推荐答案

迁移到阿波罗只有在您的后端允许进行GraphQl调用时才有意义,我们将项目从Redux迁移到Apollo,并且非常好.

阅读此博客是来自这个博客,我们决定进行迁移

本文地址:https://www.itbaoku.cn/post/1938122.html

问题描述

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

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.

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.

Questions:

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

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

推荐答案

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:

  • Predictability. Reducers are pure functions -- given the same state and actions, a reducer will always produce the same result.
  • Testability. Again, because reducers are just functions, it's simple to unit test them.
  • Scalability. Because redux forces you to organize your code in a specific way, it makes your code more maintainable. Even your as your code base grows, your code is still debuggable and understandable by other developers.

Dan Abromov points out several other benefits:

  • 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.

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'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.

其他推荐答案

I think you made a good point here: "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."

For side effects, Redux is imperative, and Apollo is declarative. Declarative code is usually shorter, since you're delegating logic to the library/framework.


Daniel Rearden made a good point that comparing Redux and the Apollo client cache is like apples and oranges. The apples and oranges here are the different types of state, specifically remote and local state. Unfortunately, Redux encourages us to treat all state the same.

I would leverage Apollo cache for state that needs to be retrieved, updated, and mutated on the server. I would reach for lighter tools like React's Context API for preventing prop drilling, global app state, and hooks for business logic (e.g. useReducer/useState).

The tricky part is when remote state and local/global app state mix. So I would be careful to define patterns around how they interact

其他推荐答案

migrating to apollo makes sense only if your backend allows to make graphql call, we have migrated our project from redux to apollo and it has come out very good.

Read this blog too, it was from this blog we decided for the migration