javascript - 如何在 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, 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,
};
};
例如,是否可以将fetchMoreOne
and组合fetchMoreTwo
成一个触发刷新的函数?如果是这样,那将如何运作?
解决方案
如果我正确理解了您的问题,您可以简单地执行此操作:
let fetchAll = useCallback(() => {
if (fetchMoreOne) fetchMoreOne();
if (fetchMoreTwo) fetchMoreTwo();
}, [fetchMoreOne, fetchMoreTwo]);
推荐阅读
- gremlin - Tinkerpop 选择多个按顶点分组的邻居,它们是具有范围步长的邻居
- node.js - 授予节点 js 中 google drive api v3 上现有文件的权限
- apache-spark - 当行展平逻辑中存在错误/异常时,使用 Flatmap 函数跳过 Spark 数据集中的特定行
- pandas - 从 datetimeobject 中提取毫秒
- css - \@ 媒体查询上的符号及其解释方式
- python - Python - 使用 Selenium 下载 PDF 并保存到磁盘
- reactjs - 索引没有更新,同时过滤掉表数据
- netlify - Netlify Lambda 函数日志永远挂起
- android - 动态查询房间数据库不支持 LiveData 吗?
- python - 如何对 Django REST Framework 中嵌套序列化器字段的源参数进行排序?