rahul  Kushwaha
rahul Kushwaha

Reputation: 2829

How to wait for multiple useLazyQuery in react-apollo

I have a gql query MY_Q and have a list of the data items, like data_list = [data1, data2]

how to group multiple useLazyQuery() on each data item and wait until all the query has finished before processing the data.

similar to Promise.all().

example:-

Promise.all (data_list.map((e) => useLazyQuery(MY_Q , { variables : { data : 
e}}))).then ((res) {
 //process result.
})

Upvotes: 2

Views: 1925

Answers (1)

user1849962
user1849962

Reputation: 1363

I wouldn't use promises because you will want what the hooks returns in your React component won't you?

Just define the hook for each separate query and then render based on what is returned so far. When everything is fetched render that, if not, render a loading view in the component.


function SomeComponent () {
  const [fetchInitial, {data, fetchMore}] = useLazyQuery(QUERY, {
    variables: {
      dataSets: [
        dataSet1,
        dataSet2
      ]
    }
  });

}

Upvotes: 1

Related Questions