简体   繁体   English

如何使Redux Thunk和Apollo GraphQL协同工作

[英]How to make Redux Thunk and Apollo GraphQL work together

In my projects, Redux Thunk are used to keep all the async functions in action creators. 在我的项目中, Redux Thunk用于将所有异步函数保留在动作创建器中。

Now I am trying to add Apollo GraphQL into my project, everything works well except that when adding mutations into my functional Component and call them there, it breaks the redux thunk architecture. 现在我正在尝试将Apollo GraphQL添加到我的项目中,除了将突变添加到我的功能组件并在那里调用它之外,一切都运行良好,它打破了redux thunk架构。

How to solve it? 怎么解决? I suppose I can create new action creator methods and pass the mutations into them, but it would soon become boilerplate and that just doesn't seem to be a good solution to me. 我想我可以创建新的动作创建器方法并将突变传递给它们,但它很快就会变成样板,而这对我来说似乎不是一个好的解决方案。

Apollo mutations don't break redux thunk architecture. 阿波罗突变不会破坏redux thunk架构。 If you need to update your own reducers after a mutation, then dispatch those reducer's actions from within the then or catch of the mutation. 如果您需要在突变后更新自己的Reducer,那么从thencatch突变中调度这些reducer的操作。 Apollo's graphql container is designed to replace async redux middleware (thunks, sagas, epics), not break it. Apollo的graphql容器旨在取代异步redux中间件(thunk,sagas,epics),而不是破坏它。

For Apollo Client 1.0 对于Apollo Client 1.0

If you want, you could also wire up your own reducers to the same actions that Apollo dispatches (APOLLO_MUTATION_INIT, APOLLO_MUTATION_RESULT, etc.). 如果需要,您还可以将自己的Reducer连接到Apollo调度的相同操作(APOLLO_MUTATION_INIT,APOLLO_MUTATION_RESULT等)。 Just makes sure that you have the apollo store integrated with your own. 只需确保您的apollo商店与您自己的商店集成。 See http://dev.apollodata.com/react/redux.html . 请参阅http://dev.apollodata.com/react/redux.html

Update: Migrating to Apollo Client 2.0 更新:迁移到Apollo Client 2.0

Apollo Client 2.0 removed the dependency on using redux as its cache (store). Apollo Client 2.0删除了使用redux作为缓存(存储)的依赖性。 If you still want to be able to use 1.0 style actions in your reducers, you can use this link https://www.npmjs.com/package/apollo-link-redux (example usage in the readme). 如果您仍希望能够在Reducer中使用1.0样式操作,则可以使用此链接https://www.npmjs.com/package/apollo-link-redux (自述文件中的示例用法)。

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

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