【发布时间】:2018-01-12 15:50:16
【问题描述】:
我正在使用很棒的 https://github.com/apollographql/react-apollo 库,我正在尝试看看是否有比我现在这样做更好的惯例来将数据加载到组件中。
我已经将我的组件设置为使用 apollo HOC 将数据加载到我的组件中,如下所示:
const mainQuery = gql`
query currentProfileData($userId:String, $communityId:String!){
created: communities(id:$communityId) {
opportunities{
submittedDate
approvalDate
status
opportunity{
id
}
}
}
}
`;
const mainQueryOptions = {
options: {
variables: { userId: '_', communityId: '_' },
},
};
const ComponentWithData = compose(
graphql(mainQuery, mainQueryOptions),
)(Component);
此设置效果很好,但存在一些问题。
我最终得到总是运行两次的查询,因为我需要将 props 传递给 apollo refetch 以进行查询。我还必须传入一些虚拟数据(也称为“_”)以防止获取无用的数据。
-
我最终不得不在 componentWillReceiveProps 中进行一些花哨的检查,以防止多次加载查询。
- 我不能在查询中使用跳过选项,因为这会阻止重新获取函数被传入。
我没有完全避开 HOC 并直接通过 apollo 手动运行查询,我该如何解决这个问题?
【问题讨论】:
标签: javascript reactjs graphql apollo