Skip to content

Commit

Permalink
Add useQuery argument generic type (#7977)
Browse files Browse the repository at this point in the history
* Add useQuery argument generic type

* Update test snapshot

* Update tests and examples

* Create spotty-cherries-drop.md

Co-authored-by: Charly POLY <1252066+charlypoly@users.noreply.github.com>
  • Loading branch information
CreativeTechGuy and charlypoly committed Jul 27, 2022
1 parent 9480fd4 commit ab66ba1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-cherries-drop.md
@@ -0,0 +1,5 @@
---
"@graphql-codegen/typescript-urql": patch
---

Add useQuery argument generic type
9 changes: 6 additions & 3 deletions dev-test/githunt/types.urql.tsx
Expand Up @@ -445,7 +445,7 @@ export const CommentDocument = gql`
`;

export function useCommentQuery(options: Omit<Urql.UseQueryArgs<CommentQueryVariables>, 'query'>) {
return Urql.useQuery<CommentQuery>({ query: CommentDocument, ...options });
return Urql.useQuery<CommentQuery, CommentQueryVariables>({ query: CommentDocument, ...options });
}
export const CurrentUserForProfileDocument = gql`
query CurrentUserForProfile {
Expand All @@ -459,7 +459,10 @@ export const CurrentUserForProfileDocument = gql`
export function useCurrentUserForProfileQuery(
options?: Omit<Urql.UseQueryArgs<CurrentUserForProfileQueryVariables>, 'query'>
) {
return Urql.useQuery<CurrentUserForProfileQuery>({ query: CurrentUserForProfileDocument, ...options });
return Urql.useQuery<CurrentUserForProfileQuery, CurrentUserForProfileQueryVariables>({
query: CurrentUserForProfileDocument,
...options,
});
}
export const FeedDocument = gql`
query Feed($type: FeedType!, $offset: Int, $limit: Int) {
Expand All @@ -474,7 +477,7 @@ export const FeedDocument = gql`
`;

export function useFeedQuery(options: Omit<Urql.UseQueryArgs<FeedQueryVariables>, 'query'>) {
return Urql.useQuery<FeedQuery>({ query: FeedDocument, ...options });
return Urql.useQuery<FeedQuery, FeedQueryVariables>({ query: FeedDocument, ...options });
}
export const SubmitRepositoryDocument = gql`
mutation submitRepository($repoFullName: String!) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/typescript/urql/src/visitor.ts
Expand Up @@ -131,7 +131,7 @@ export function use${operationName}<TData = ${operationResultType}>(options: Omi
export function use${operationName}(options${
isVariablesRequired ? '' : '?'
}: Omit<Urql.Use${operationType}Args<${operationVariablesTypes}>, 'query'>) {
return Urql.use${operationType}<${operationResultType}>({ query: ${documentVariableName}, ...options });
return Urql.use${operationType}<${operationResultType}, ${operationVariablesTypes}>({ query: ${documentVariableName}, ...options });
};`;
}

Expand Down
Expand Up @@ -25,6 +25,6 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export function useTestQuery(options?: Omit<Urql.UseQueryArgs<Operations.TestQueryVariables>, 'query'>) {
return Urql.useQuery<Operations.TestQuery>({ query: Operations.TestDocument, ...options });
return Urql.useQuery<Operations.TestQuery, Operations.TestQueryVariables>({ query: Operations.TestDocument, ...options });
};"
`;
6 changes: 3 additions & 3 deletions packages/plugins/typescript/urql/tests/urql.spec.ts
Expand Up @@ -553,7 +553,7 @@ query MyFeed {

expect(content.content).toBeSimilarStringTo(`
export function useFeedQuery(options?: Omit<Urql.UseQueryArgs<FeedQueryVariables>, 'query'>) {
return Urql.useQuery<FeedQuery>({ query: FeedDocument, ...options });
return Urql.useQuery<FeedQuery, FeedQueryVariables>({ query: FeedDocument, ...options });
};`);

expect(content.content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -582,7 +582,7 @@ export function useSubmitRepositoryMutation() {

expect(content.content).toBeSimilarStringTo(`
export function useRequiredArgQuery(options: Omit<Urql.UseQueryArgs<RequiredArgQueryVariables>, 'query'>) {
return Urql.useQuery<RequiredArgQuery>({ query: RequiredArgDocument, ...options });
return Urql.useQuery<RequiredArgQuery, RequiredArgQueryVariables>({ query: RequiredArgDocument, ...options });
};`);
await validateTypeScript(content, schema, docs, {});
});
Expand All @@ -606,7 +606,7 @@ export function useRequiredArgQuery(options: Omit<Urql.UseQueryArgs<RequiredArgQ

expect(content.content).toBeSimilarStringTo(`
export function useDefaultValueArgQuery(options?: Omit<Urql.UseQueryArgs<DefaultValueArgQueryVariables>, 'query'>) {
return Urql.useQuery<DefaultValueArgQuery>({ query: DefaultValueArgDocument, ...options });
return Urql.useQuery<DefaultValueArgQuery, DefaultValueArgQueryVariables>({ query: DefaultValueArgDocument, ...options });
};`);
await validateTypeScript(content, schema, docs, {});
});
Expand Down

1 comment on commit ab66ba1

@vercel
Copy link

@vercel vercel bot commented on ab66ba1 Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.