如何解决如何在Apollo中组合多个fetchMore函数?
是否可以使用Apollo同时运行多个fetchMores?
我有一个相对复杂的钩子,它可以运行两个查询,并在单个数组中返回这些查询的结果,如下所示:
export const useDashboardState = (collection: string) => {
// Get various parameters from query string
const [filter,setFilter] = useQueryParam("filter",StringParam);
const [minDate,setMinDate] = useQueryParam("minDate",StringParam);
const [maxDate,setMaxDate] = useQueryParam("maxDate",StringParam);
const [subcollections,setSubcollections] = useQueryParam(
"subcollections",ArrayParam
);
......the business logic of the hook....
// Conduct Apollo query #1
const { loading,error,data,fetchMore: fetchMoreOne } = useQuery(gqlQueryOne,{
variables: {
minDate: minDate,maxDate: maxDate,},notifyOnNetworkStatusChange: true,});
// Conduct Apollo query #2
const { loading,fetchMore: fetchMoreTwo } = useQuery(gqlQueryTwo,});
return {
// If either result is still loading,return loading
loading: senateCommitteesLoading || houseCommitteesLoading,// Once both data are non-null,concatenate them and return
data:
houseCommittees && senateCommittees
? [...houseCommittees,...senateCommittees]
: null,// How can we implement a re-run of this complicated hook that makes multiple queries?
fetchMoreOne,fetchMoreTwo,};
};
例如,是否可以将fetchMoreOne
和fetchMoreTwo
组合成一个会触发刷新的函数?如果是这样,那将如何工作?
解决方法
如果我正确理解了您的问题,则可以执行以下操作:
let fetchAll = useCallback(() => {
if (fetchMoreOne) fetchMoreOne();
if (fetchMoreTwo) fetchMoreTwo();
},[fetchMoreOne,fetchMoreTwo]);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。