gpt4 book ai didi

javascript - 如何在 Apollo 中组合多个 fetchMore 函数?

转载 作者:行者123 更新时间:2023-12-01 16:24:35 25 4
gpt4 key购买 nike

是否可以使用 Apollo 同时运行多个 fetchMore?

我有一个相对复杂的钩子(Hook),它运行两个查询,并在一个数组中返回这些查询的结果,如下所示:

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, error, data, fetchMore: fetchMoreTwo } = useQuery(gqlQueryTwo, {
variables: {
minDate: minDate,
maxDate: maxDate,
},
notifyOnNetworkStatusChange: true,
});


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,
};
};

例如,是否可以将 fetchMoreOnefetchMoreTwo 组合成一个触发刷新的函数?如果是这样,那将如何运作?

最佳答案

如果我正确理解你的问题,你可以简单地这样做:

let fetchAll = useCallback(() => {
if (fetchMoreOne) fetchMoreOne();
if (fetchMoreTwo) fetchMoreTwo();
}, [fetchMoreOne, fetchMoreTwo]);

关于javascript - 如何在 Apollo 中组合多个 fetchMore 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63975001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com