将 redux 客户端从 REST 转换为 GraphQL Apollo?
Converting redux client from REST to GraphQL Apollo?
我有这个 rest 应用程序可以像这样获取数据,带有用于 redux 的 thunk + promise 中间件:
export const fetchVenues = () => ({
type: 'FETCH_VENUES',
payload: axios.get('http://localhost:3000/mock/venues'),
});
来自这里的数据进入商店,组件使用从那里获取的数据。
我可以将 "payload" 中的其余请求替换为 graphql 查询或其他吗?
让我感到困惑的是,Redux 说:"Data lives in the store and can only be changed by actions",但 Apollo 将数据直接绑定到组件。
我已经制作了一个 GraphQL API,在 graphiql devtool 中运行良好,但在阅读 this article.
后,我对它在客户端中的处理方式感到有点困惑
我的理解是apollo在内部使用redux。基本上它使您将查询与组件并置并注入(连接)相关数据+函数作为道具。它还通过在请求完成时更新缓存/商店来为您管理商店。
您可以看到 ApolloProvider 是一个加强版的 redux 提供程序:https://github.com/apollostack/react-apollo/blob/01425b3df5f05e7f389b36f3385433c89a39977b/src/ApolloProvider.tsx
希望对您的理解有所帮助。
我有这个 rest 应用程序可以像这样获取数据,带有用于 redux 的 thunk + promise 中间件:
export const fetchVenues = () => ({
type: 'FETCH_VENUES',
payload: axios.get('http://localhost:3000/mock/venues'),
});
来自这里的数据进入商店,组件使用从那里获取的数据。
我可以将 "payload" 中的其余请求替换为 graphql 查询或其他吗?
让我感到困惑的是,Redux 说:"Data lives in the store and can only be changed by actions",但 Apollo 将数据直接绑定到组件。 我已经制作了一个 GraphQL API,在 graphiql devtool 中运行良好,但在阅读 this article.
后,我对它在客户端中的处理方式感到有点困惑我的理解是apollo在内部使用redux。基本上它使您将查询与组件并置并注入(连接)相关数据+函数作为道具。它还通过在请求完成时更新缓存/商店来为您管理商店。
您可以看到 ApolloProvider 是一个加强版的 redux 提供程序:https://github.com/apollostack/react-apollo/blob/01425b3df5f05e7f389b36f3385433c89a39977b/src/ApolloProvider.tsx
希望对您的理解有所帮助。