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.

Not able to execute `useLazyQuery` multiple times with different variables

See original GitHub issue

I’m not 100% sure whether this is a valid use case for useLazyQuery(), but I still think this new behaviour is not intended because of the following reasons:

  • The behaviour has changed between v3.5.10 and v3.6.x.
  • The new behaviour does not seem to be right, since the promise that gets resolved after executing a query with some variables corresponds to the response from another query executed with different variables.

Intended outcome:

When executing the same lazy query twice on the same tick with different variables, Apollo should execute two network requests each one with the corresponding variables and return the correct results

const [fetch] = useLazyQuery(CountriesList);

useEffect(() => {
  Promise.all([
    fetch({variables: {continent: "EU"}}),
    fetch({variables: {continent: "NA"}})
  ]).then(([result1, result2]) => {
    // result1 should contain the results of executing CountriesList query with continent=EU
    // result2 should contain the results of executing CountriesList query with continent=NA
  });
}, [fetch]);

Actual outcome:

When executing the same lazy query twice on the same tick with different variables, Apollo instead executes a single network request and returns the same results for the two requests.

So, on the code snipped pasted above, both result1 and result2 contains the results of executing CountriesList query with continent=NA.

How to reproduce the issue:

The expected behaviour is the two fetch calls on L27 and L28 to return different results, and therefore the UI should show 2 columns with different sets of countries.

Versions

@apollo/client >= 3.6.0 (the issue is also present in the currently latest available alpha (v3.7.0-alpha.7).

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:21
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
louleocommented, Aug 23, 2022

Got this issue as well. Another work around would be useApolloClient directly.

  const client = useApolloClient();
  const promises = Array.map((item) => {
    return client.query({
     query: YOUR_QUERY_DOCUMENT,
     variables: {
     YOUR VARIABLES  
   }  
  })
  });
 Promise.all(promises).then(...)
4reactions
jerelmillercommented, Dec 19, 2022

@rafeca thanks so much for this detail. This is extremely helpful understanding the context surrounding this issue.

I’ll try and respond to bits of this over time, but I’d love to take this back to the Apollo team and throw some ideas/questions around. FWIW, I agree that there are a few quirky things with useLazyQuery (like the loading flag in the resolved promise you pointed out). We’ll discuss and figure out if we deem this buggy or not.

Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to call one query multiple times with different variable ...
Then I would implement an endpoint that can handle multiple IDs, like so: type Query { users(ids: [ID!]!): [User!]! } If that is...
Read more >
Multiple useLazyQuery hooks (Apollo Client) in React ...
I am trying to include two Apollo-Client useLazyQuery hooks in my function component. Either works fine alone with the other one commented ...
Read more >
Calling Queries Manually with useLazyQuery - Thinkster.io
We can do this using the useLazyQuery hook that Apollo provides. This hook lets us run a query in response to an event...
Read more >
The React + Apollo Tutorial for 2020 (Real-World Examples)
Apollo is a library that brings together two incredibly useful ... executing queries client .query({ query: GET_POSTS, variables: { limit: 5 } ...
Read more >
Use useLazyQuery to manually execute a query with Apollo ...
When using useQuery from Apollo React Hooks, the request will be sent automatically after the component has been mounted. This might not be ......
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