使用Apollo是否可以同时运行多个fetchMores?
我有一个相对复杂的钩子(Hook),该钩子(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/

10-11 10:20