Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urql 4 now have more strict types for variables #329

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/kind-pens-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-vue-urql': minor
---

Generate explicit type for variables used in a query to work with urql v4
4 changes: 2 additions & 2 deletions packages/plugins/typescript/vue-urql/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export function use${operationName}<R = ${operationResultType}>(options: Omit<Ur
}

return `
export function use${operationName}(options: Omit<Urql.Use${operationType}Args<never, ${operationVariablesTypes}>, 'query'> = {}) {

Choose a reason for hiding this comment

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

Even leaving the default in place works for me in all cases, no matter if I

  • have required variables,
  • have only optional variables,
  • have no variables at all.

With @urql/vue ^1.1.1, the only required change is your change in line 102.

I would prefer to leave the default (line 101) in place, to make it a non-breaking change for current users.

Choose a reason for hiding this comment

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

I use typescript version 5.

Choose a reason for hiding this comment

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

I'M SORRY: I WAS WRONG. My variables were marked as optional without my knowledge. Your version is right and also working for required variables!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we merge and publish these changes?

Choose a reason for hiding this comment

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

This PR is stale for more than a month now, can we get this merged or provide reasons for not merging it?

Copy link

Choose a reason for hiding this comment

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

do you know when this PR can be merged ? ... since 12 april ...

Copy link

@Dodobibi Dodobibi Aug 19, 2023

Choose a reason for hiding this comment

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

Variables & variables fields maybe ref :

-export function use${operationName}(options: Omit<Urql.Use${operationType}Args<never, ${operationVariablesTypes}>, 'query'> = {}) {
-  return Urql.use${operationType}<${operationResultType}>({ query: ${documentVariableName}, ...options });
+type MaybeRef<T> = T | Ref<T>; 
+export function use${operationName}(options: Omit<Urql.Use${operationType}Args<never, MaybeRef<{[K in keyof ${operationVariablesTypes}]: MaybeRef<${operationVariablesTypes}[K]>}>>, 'query'>) {
+  return Urql.use${operationType}<${operationResultType}, ${operationVariablesTypes}>({ query: ${documentVariableName}, ...options });

return Urql.use${operationType}<${operationResultType}>({ query: ${documentVariableName}, ...options });
export function use${operationName}(options: Omit<Urql.Use${operationType}Args<never, ${operationVariablesTypes}>, 'query'>) {
return Urql.use${operationType}<${operationResultType}, ${operationVariablesTypes}>({ query: ${documentVariableName}, ...options });
};`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as Urql from '@urql/vue';
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;


export function useTestQuery(options: Omit<Urql.UseQueryArgs<never, Operations.TestQueryVariables>, 'query'> = {}) {
return Urql.useQuery<Operations.TestQuery>({ query: Operations.TestDocument, ...options });
export function useTestQuery(options: Omit<Urql.UseQueryArgs<never, Operations.TestQueryVariables>, 'query'>) {
return Urql.useQuery<Operations.TestQuery, Operations.TestQueryVariables>({ query: Operations.TestDocument, ...options });
};"
`;
4 changes: 2 additions & 2 deletions packages/plugins/typescript/vue-urql/tests/urql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ query MyFeed {
)) as Types.ComplexPluginOutput;

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

expect(content.content).toBeSimilarStringTo(`
Expand Down