Apollo 客户端缓存与 Redux
Apollo Client Cache vs. Redux
我正在尝试从 Redux Store 迁移到使用 Apollo Graphql Client 附带的 Apollo Client Cache。
Apollo Client 有别于其他数据管理解决方案的关键特性之一是其 规范化缓存 。只需设置 Apollo Client,您就可以获得开箱即用的智能缓存,无需额外配置。
使用 Redux,我们必须根据从副作用接收到的响应编写操作、类型和调度操作,并使用 reducer 设置存储中的数据,这是由 Apollo Client 自动完成的。
问题:
1) 从 Redux 迁移到 Apollo Client Cache 有什么优势?
2) 在迁移到 Apollo Client Cache 之前,我应该担心什么吗?
只有当你的后端允许进行 graphql 调用时,迁移到 apollo 才有意义,我们已经将我们的项目从 redux 迁移到 apollo,结果非常好。
也请阅读此 blog,我们正是从这个博客决定进行迁移的
您是在将苹果与橙子进行比较。是的,在较高级别上,redux
和 apollo-client
都为您提供了一种管理全局应用程序状态的方法。但是,redux
允许您创建一个可预测的状态容器,该容器会根据您定义的操作而发生变化。这意味着 redux
提供:
- 可预测性。 Reducer 是纯函数——给定相同的状态和操作,reducer 将始终产生相同的结果。
- 可测试性。同样,因为 reducer 只是函数,所以对它们进行单元测试很简单。
- 可扩展性。因为 redux 强制您以特定方式组织代码,所以它使您的代码更易于维护。即使您的代码库在增长,您的代码仍然可以被其他开发人员调试和理解。
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.
是的,redux
附带了很多样板文件。但是,您、您的应用程序和您的团队都可能从使用它中获得很多好处,而不仅仅是拥有一种管理全局状态的方法。另一方面,如果您看不到 redux 提供的功能的价值,或者认为它们不值得间接和复杂性 redux
添加到您的代码中,那么请不要使用它。如果您只需要一种管理全局应用程序状态的方法,那么您可以利用 apollo-client
或其他一些库,或者只利用上下文 API 和 useReducer
挂钩来推出您自己的解决方案。
Apollo Client 使用 @client
指令来管理本地状态非常方便,特别是如果您已经在使用库来查询 GraphQL API。能够使用派生字段轻松地修饰查询结果是很巧妙的。能够利用相同的 API 来查询您的服务器和查询本地状态有助于实现良好的 DX。但是 apollo-client
不能 取代 redux
因为最终这两个库会出于不同的原因做不同的事情。
我觉得你说得很好:"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."
对于副作用,Redux 是命令式的,Apollo 是声明式的。声明性代码通常更短,因为您将逻辑委托给 library/framework.
Daniel Rearden 提出了一个很好的观点,即比较 Redux 和 Apollo 客户端缓存就像苹果和橘子。这里的苹果和橘子是不同的 类型的状态 ,特别是 远程 和 本地 状态。不幸的是,Redux 鼓励我们对所有状态一视同仁。
我会利用 Apollo 缓存来获取需要在服务器上检索、更新和更改的状态。我会使用更轻量级的工具,例如 React 的 Context API,用于防止 prop drilling、全局应用程序状态和业务逻辑挂钩(例如 useReducer/useState)。
棘手的部分是当远程状态和 local/global 应用状态混合时。所以我会小心定义围绕它们如何交互的模式
我正在尝试从 Redux Store 迁移到使用 Apollo Graphql Client 附带的 Apollo Client Cache。
Apollo Client 有别于其他数据管理解决方案的关键特性之一是其 规范化缓存 。只需设置 Apollo Client,您就可以获得开箱即用的智能缓存,无需额外配置。
使用 Redux,我们必须根据从副作用接收到的响应编写操作、类型和调度操作,并使用 reducer 设置存储中的数据,这是由 Apollo Client 自动完成的。
问题:
1) 从 Redux 迁移到 Apollo Client Cache 有什么优势?
2) 在迁移到 Apollo Client Cache 之前,我应该担心什么吗?
只有当你的后端允许进行 graphql 调用时,迁移到 apollo 才有意义,我们已经将我们的项目从 redux 迁移到 apollo,结果非常好。
也请阅读此 blog,我们正是从这个博客决定进行迁移的
您是在将苹果与橙子进行比较。是的,在较高级别上,redux
和 apollo-client
都为您提供了一种管理全局应用程序状态的方法。但是,redux
允许您创建一个可预测的状态容器,该容器会根据您定义的操作而发生变化。这意味着 redux
提供:
- 可预测性。 Reducer 是纯函数——给定相同的状态和操作,reducer 将始终产生相同的结果。
- 可测试性。同样,因为 reducer 只是函数,所以对它们进行单元测试很简单。
- 可扩展性。因为 redux 强制您以特定方式组织代码,所以它使您的代码更易于维护。即使您的代码库在增长,您的代码仍然可以被其他开发人员调试和理解。
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.
是的,redux
附带了很多样板文件。但是,您、您的应用程序和您的团队都可能从使用它中获得很多好处,而不仅仅是拥有一种管理全局状态的方法。另一方面,如果您看不到 redux 提供的功能的价值,或者认为它们不值得间接和复杂性 redux
添加到您的代码中,那么请不要使用它。如果您只需要一种管理全局应用程序状态的方法,那么您可以利用 apollo-client
或其他一些库,或者只利用上下文 API 和 useReducer
挂钩来推出您自己的解决方案。
Apollo Client 使用 @client
指令来管理本地状态非常方便,特别是如果您已经在使用库来查询 GraphQL API。能够使用派生字段轻松地修饰查询结果是很巧妙的。能够利用相同的 API 来查询您的服务器和查询本地状态有助于实现良好的 DX。但是 apollo-client
不能 取代 redux
因为最终这两个库会出于不同的原因做不同的事情。
我觉得你说得很好:"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."
对于副作用,Redux 是命令式的,Apollo 是声明式的。声明性代码通常更短,因为您将逻辑委托给 library/framework.
Daniel Rearden 提出了一个很好的观点,即比较 Redux 和 Apollo 客户端缓存就像苹果和橘子。这里的苹果和橘子是不同的 类型的状态 ,特别是 远程 和 本地 状态。不幸的是,Redux 鼓励我们对所有状态一视同仁。
我会利用 Apollo 缓存来获取需要在服务器上检索、更新和更改的状态。我会使用更轻量级的工具,例如 React 的 Context API,用于防止 prop drilling、全局应用程序状态和业务逻辑挂钩(例如 useReducer/useState)。
棘手的部分是当远程状态和 local/global 应用状态混合时。所以我会小心定义围绕它们如何交互的模式