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

fix(typescript-vue-urql): variable typing #801

Merged
merged 1 commit into from
Aug 21, 2024
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/smooth-timers-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-vue-urql': minor
---

Fix types for variables used in a query/subscription
8 changes: 4 additions & 4 deletions packages/plugins/typescript/vue-urql/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ export function use${operationName}() {

if (operationType === 'Subscription') {
return `
export function use${operationName}<R = ${operationResultType}>(options: Omit<Urql.Use${operationType}Args<never, ${operationVariablesTypes}>, 'query'>, handler?: Urql.SubscriptionHandlerArg<${operationResultType}, R>) {
return Urql.use${operationType}<${operationResultType}, R, ${operationVariablesTypes}>({ query: ${documentVariableName}, ...options }, handler);
export function use${operationName}<R = ${operationResultType}>(options?: Omit<Urql.Use${operationType}Args<never, ${operationVariablesTypes} | undefined>, 'query'>, handler?: Urql.SubscriptionHandlerArg<${operationResultType}, R>) {
return Urql.use${operationType}<${operationResultType}, R, ${operationVariablesTypes} | undefined>({ query: ${documentVariableName}, variables: undefined, ...options }, handler);
};`;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export const ListenToCommentsDocument = gql\`
}
\`;

export function useListenToCommentsSubscription<R = ListenToCommentsSubscription>(options: Omit<Urql.UseSubscriptionArgs<never, ListenToCommentsSubscriptionVariables>, 'query'>, handler?: Urql.SubscriptionHandlerArg<ListenToCommentsSubscription, R>) {
return Urql.useSubscription<ListenToCommentsSubscription, R, ListenToCommentsSubscriptionVariables>({ query: ListenToCommentsDocument, ...options }, handler);
export function useListenToCommentsSubscription<R = ListenToCommentsSubscription>(options?: Omit<Urql.UseSubscriptionArgs<never, ListenToCommentsSubscriptionVariables | undefined>, 'query'>, handler?: Urql.SubscriptionHandlerArg<ListenToCommentsSubscription, R>) {
return Urql.useSubscription<ListenToCommentsSubscription, R, ListenToCommentsSubscriptionVariables | undefined>({ query: ListenToCommentsDocument, variables: undefined, ...options }, handler);
};"
`;

Expand All @@ -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, Operations.TestQueryVariables>({ query: Operations.TestDocument, ...options });
export function useTestQuery(options?: Omit<Urql.UseQueryArgs<never, Operations.TestQueryVariables | undefined>, 'query'>) {
return Urql.useQuery<Operations.TestQuery, Operations.TestQueryVariables | undefined>({ query: Operations.TestDocument, variables: undefined, ...options });
};"
`;
8 changes: 4 additions & 4 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, FeedQueryVariables>({ query: FeedDocument, ...options });
export function useFeedQuery(options?: Omit<Urql.UseQueryArgs<never, FeedQueryVariables | undefined>, 'query'>) {
return Urql.useQuery<FeedQuery, FeedQueryVariables | undefined>({ query: FeedDocument, variables: undefined, ...options });
};`);

expect(content.content).toBeSimilarStringTo(`
Expand Down Expand Up @@ -524,8 +524,8 @@ export function useSubmitRepositoryMutation() {
)) as Types.ComplexPluginOutput;

expect(content.content).toBeSimilarStringTo(`
export function useListenToCommentsSubscription<R = ListenToCommentsSubscription>(options: Omit<Urql.UseSubscriptionArgs<never, ListenToCommentsSubscriptionVariables>, 'query'>, handler?: Urql.SubscriptionHandlerArg<ListenToCommentsSubscription, R>) {
return Urql.useSubscription<ListenToCommentsSubscription, R, ListenToCommentsSubscriptionVariables>({ query: ListenToCommentsDocument, ...options }, handler);
export function useListenToCommentsSubscription<R = ListenToCommentsSubscription>(options?: Omit<Urql.UseSubscriptionArgs<never, ListenToCommentsSubscriptionVariables | undefined>, 'query'>, handler?: Urql.SubscriptionHandlerArg<ListenToCommentsSubscription, R>) {
return Urql.useSubscription<ListenToCommentsSubscription, R, ListenToCommentsSubscriptionVariables | undefined>({ query: ListenToCommentsDocument, variables: undefined, ...options }, handler);
};`);
await validateTypeScript(content, schema, docs, {});
expect(mergeOutputs([content])).toMatchSnapshot();
Expand Down