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.

v3.5: Wrong TS types for `useLazyQuery`

See original GitHub issue

Intended outcome: Successfully upgrade Apollo Client from v3.4 to v3.5.

Actual outcome: TypeScript ESLint warning on this code:

  useEffect(() => {
    if (!called && inView) {
      void runQuery();
    }
  }, [called, inView, runQuery]);

The !called check is getting Unnecessary conditional, value is always falsy. That’s because according to the types, called is always true, even for a lazy query. That’s obviously incorrect.

There was no such type mismatch in v3.4.

How to reproduce the issue: Check called in any lazy query in a TypeScript codebase. You’ll see it permanently equals true, according to the types:

I see multiple problems in the existing types:

  • useQuery shouldn’t return called at all, according to the docs:

    This field is only present on the result object returned by useLazyQuery.

  • Per the above note, then, the result object returned when calling the lazy query shouldn’t include called, either.

  • The result object returned by useLazyQuery should include called: boolean.

Versions

  System:
    OS: Linux 4.19 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
  Binaries:
    Node: 16.13.0 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/bin/yarn
    npm: 8.1.0 - /usr/local/bin/npm

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
benmoshercommented, Feb 9, 2022

FWIW – IIRC (from ages ago! 😅) I was trying to make QueryResult<...> the type of non-lazy queries, in which case called would always be true because it would be called immediately; only for lazy queries could it be either boolean value.

but it sounds like either I didn’t succeed or some type changes in the intervening time lost track of that. either way, sorry for the bug! glad it’s fixed.

1reaction
aaronadamsCAcommented, Jan 11, 2022

Oops, yes, this is definitely a duplicate of #9102.

But I’m going to passive-aggressively leave it to a maintainer to close this, so we can actually get acknowledgment of the issue first 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript with Apollo Client - Apollo GraphQL Docs
ts file). This function enables us to type the variables that go into our React hooks, along with the results from those hooks....
Read more >
GraphQL useLazyQuery typescript fetchPolicy problem
Intellisense finds fetchPolicy and its value list while typing. But compiler generates below error; Argument of type '{ fetchPolicy: string; }' ...
Read more >
The React + Apollo Tutorial for 2020 (Real-World Examples)
Like the useLazyQuery hook, it returns an array which we can destructure into its two elements. In the first element, we get back...
Read more >
@apollo/client | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
@apollo/react-hooks - npm
TypeScript icon, indicating that this package has built-in type declarations ... React Apollo useQuery , useLazyQuery , useMutation ...
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