Skip to content

Commit

Permalink
Generate explicit type for variables used in a subscription to work w…
Browse files Browse the repository at this point in the history
…ith urql v4 (#494)

Otherwise it doesn't work if subscription have a required variable
Possibly a breaking change - now it requires to define empty object for variables argument if no variables are used in subscription

Co-authored-by: Mikhail Lesin <mlesin@mrpost.ru>
  • Loading branch information
mlesin and Mikhail Lesin committed Mar 6, 2024
1 parent 34d9c70 commit b79e1a0
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/kind-pens-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/typescript-vue-urql': minor
---

Generate explicit type for variables used in a subscription to work with urql v4
Possibly a breaking change - now it requires to define empty object for variables argument if no variables are used in subscription
2 changes: 1 addition & 1 deletion packages/plugins/typescript/vue-urql/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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>) {
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);
};`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const ListenToCommentsDocument = gql\`
}
\`;
export function useListenToCommentsSubscription<R = ListenToCommentsSubscription>(options: Omit<Urql.UseSubscriptionArgs<never, ListenToCommentsSubscriptionVariables>, 'query'> = {}, handler?: Urql.SubscriptionHandlerArg<ListenToCommentsSubscription, R>) {
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);
};"
`;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/typescript/vue-urql/tests/urql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ 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>) {
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);
};`);
await validateTypeScript(content, schema, docs, {});
Expand Down

0 comments on commit b79e1a0

Please sign in to comment.