question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is it possible to call multiple queries with useQuery?

See original GitHub issue

Hi, In advance thank you great library.

In our product, there is the a page which needs data from 2 or 3 GraphQL queries and we want to call multiple queries with one useQuery().

export const Curriculums = ({ children }) => {

  const { data, error } = useQuery(QUERY_CURRICULUMS)

  /* We want to call useQuery as below 
  const { data, error } = useQuery([
     QUERY_CURRICULUMS, 
     QUERY_COURSES, 
     QUERY_LESSONS
     ]
  ) 
  */

  if (error) {
    throw error
  }

  return children(data)
}

Is it possible to do the same things as compose() method defined in react-apollo?

https://www.apollographql.com/docs/react/react-apollo-migration#compose-to-render-composition

Or should we call useQuery with each queries?

Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:22

github_iconTop GitHub Comments

330reactions
MarioKrstevskicommented, Jul 22, 2019

I have a simple solution that worked for me.

const { data: dataR, error: errorR, loading: landingR } = useQuery(GET_RESTAURANTS);
const { data, error, loading } = useQuery(GET_DAILY_MENU);

I am simply renaming the fields useQuery is giving me in the first call, so that they can live in the same scope, otherwise the second query will overwrite the first.

57reactions
FezVrastacommented, Mar 28, 2019

compose just calls multiple HOCs together, but it doesn’t anything else, in fact you have to map the result of the queries manually to avoid them from being overridden by the next query.

What you want to do is compose multiple useQuery into your own custom hook:

const queryMultiple = () => {
  const res1 = useQuery(...);
  const res2 = useQuery(...);
  return [res1, res2];
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple queries in a single component with useQuery hook
Is it possible to run more than one query with the useQuery hook a single component? Independent queries, in fact. useQuery seems to...
Read more >
multiple usequeries in a single component - Stack Overflow
Indeed you can use multiple queries const queryMultiple = () => { const res1 = useQuery(Get_Doctor); const res2 = useQuery(GET_Branch); ...
Read more >
How to use Apollo useQuery with multiple queries
How to handle multiple queries in a single query via the Apollo useQuery hook ... Thanks to the way we create queries in...
Read more >
How to handle multiple queries with React-Query - JS-HowTo
1- Multiple independent queries. React-Query comes with a hook called useQueries, This hook can be used to fetch a variable number of queries....
Read more >
How to use Apollo useQuery with multiple queries - Reddit
Hi Everyone! I have just finished writing my latest blog post about how to use Apollo useQuery with multiple queries. It includes multiple...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found