[typescript-urql] improve query hooks argument types
See original GitHub issueIs 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:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hello, I don’t believe 3.5.0 has fixed this problem. It appears to have made the outer
optionsrequired now, but not thevariablesobject, which is the actual important piece for the required query variables.useMySubscriptionQuery()<-- Now fails in 3.5.0useMySubscriptionQuery({})<-- Compiles, but is still incorrectuseMySubscriptionQuery({variables: {userId: "123"}})<-- The correct requirementReleased in
@graphql-codegen/typescript-urql@3.5.0📦