我想 运行 Graphql Apollo 查询尽可能多的 id

I want to run Graphql Apollo query with as many ids as possible

使用反应,阿波罗。
我想对状态中存储的 ID 数量执行 useUserQuery。 目前只执行一次,因为ids[0]写在变量中。另外,我只能获取 ids[0].
的数据 我想执行 useUserQuery 的次数与状态中的 id 一样多。

   const [ids, setIds] = React.useState<string[]>([]);

  const {data: {userData = null} = {}} = useUserQuery({
    variables: {id: ids[0]},
    skip: !ids,
  });

不确定你所说的 useUserQuery 是什么意思,我假设它是使用原始 useQuery

制作的自定义挂钩

一个可能的解决方案是使用手动触发的useLazyQuery,否则你可以这样做:

  let fetchId = useRef(0);
  
  const {data: {userData = null} = {}, refetch} = useUserQuery({
     variables: {id: ids[0]},
     skip: !ids,
     onComplete: () => {
         if (++fetchId.current < ids.length)
             refetch({id: fetchId.current})
     }
   });

在这种情况下,您还可以使用 useState,它会在更改时自动重新获取。