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.

[typescript-urql] improve query hooks argument types

See original GitHub issue

Is your feature request related to a problem? Please describe.

Even if you generate code with a query with required arguments, you can still make a no-argument call because the function arguments are set to default values. Therefore, it is not type-safe.

example

query Foo($bar: String!) {
  foo(bar: $bar) {
    id
  }
}

generated code

export function useFooQuery(options: Omit<Urql.UseQueryArgs<FooQueryVariables>, 'query'> = {}) {
//                                                                                       ^^^^ set default value
  return Urql.useQuery<FooQuery>({ query: FooDocument, ...options });                                         
};

// The following code will not result in a compilation error
useFooQuery();

Describe the solution you’d like

For documents with required arguments, generate code with required arguments.

export function useFooQuery(options: Omit<Urql.UseQueryArgs<FooQueryVariables>, 'query'>) {
  return Urql.useQuery<FooQuery>({ query: FooDocument, ...options });                                         
};

Also, if the arguments are optional, the arguments should also be optional of generated code.

query Foo2($bar: String) {
  foo2(bar: $bar) {
    id
  }
}
export function useFoo2Query(options?: Omit<Urql.UseQueryArgs<Foo2QueryVariables>, 'query'>) {
//                                  ^ optional
  return Urql.useQuery<Foo2Query>({ query: Foo2Document, ...options });                                         
};

Additional context I have fixed this issue. I can submit a PR if you need it.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
edkimmelcommented, Apr 14, 2022

Hello, I don’t believe 3.5.0 has fixed this problem. It appears to have made the outer options required now, but not the variables object, which is the actual important piece for the required query variables.

query MySubscription($userId: String!) {
  ...
}

useMySubscriptionQuery() <-- Now fails in 3.5.0 useMySubscriptionQuery({}) <-- Compiles, but is still incorrect useMySubscriptionQuery({variables: {userId: "123"}}) <-- The correct requirement

1reaction
charlypolycommented, Jan 31, 2022

Released in @graphql-codegen/typescript-urql@3.5.0 📦

Read more comments on GitHub >

github_iconTop Results From Across the Web

The useQuery hook - with variables | Lift-off III - Apollo GraphQL
The useQuery hook returns an object with three useful properties that we use in our app: indicates whether the query has completed and...
Read more >
How do I make a query hook return type depend on parameter ...
If I use createApi, and have an endpoint called getPersistentFileValue, how do I do the typing so that the hooks figure out the...
Read more >
React Query and TypeScript - TkDodo's blog
Over time, React Query has added more Generics to the useQuery hook (there are now four of them), mainly because more functionality was ......
Read more >
How did TypeScript 4.1.0 save my React Query project? - Hokla
We ended up having the same batch of articles in all categories, all because of a missing parameter in the query key. So...
Read more >
Hooks | Objection.js
These hooks are executed in different stages of each query type (find, update, ... Each instance hook is passed the context object as...
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