diff --git a/.changeset/smooth-timers-peel.md b/.changeset/smooth-timers-peel.md new file mode 100644 index 000000000..8b702c2fb --- /dev/null +++ b/.changeset/smooth-timers-peel.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/typescript-vue-urql': minor +--- + +Fix types for variables used in a query/subscription diff --git a/packages/plugins/typescript/vue-urql/src/visitor.ts b/packages/plugins/typescript/vue-urql/src/visitor.ts index 408292694..d3723d416 100644 --- a/packages/plugins/typescript/vue-urql/src/visitor.ts +++ b/packages/plugins/typescript/vue-urql/src/visitor.ts @@ -92,14 +92,14 @@ export function use${operationName}() { if (operationType === 'Subscription') { return ` -export function use${operationName}(options: Omit, 'query'>, handler?: Urql.SubscriptionHandlerArg<${operationResultType}, R>) { - return Urql.use${operationType}<${operationResultType}, R, ${operationVariablesTypes}>({ query: ${documentVariableName}, ...options }, handler); +export function use${operationName}(options?: Omit, '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, 'query'>) { - return Urql.use${operationType}<${operationResultType}, ${operationVariablesTypes}>({ query: ${documentVariableName}, ...options }); +export function use${operationName}(options?: Omit, 'query'>) { + return Urql.use${operationType}<${operationResultType}, ${operationVariablesTypes} | undefined>({ query: ${documentVariableName}, variables: undefined, ...options }); };`; } diff --git a/packages/plugins/typescript/vue-urql/tests/__snapshots__/urql.spec.ts.snap b/packages/plugins/typescript/vue-urql/tests/__snapshots__/urql.spec.ts.snap index e897e4af9..092eb5c80 100644 --- a/packages/plugins/typescript/vue-urql/tests/__snapshots__/urql.spec.ts.snap +++ b/packages/plugins/typescript/vue-urql/tests/__snapshots__/urql.spec.ts.snap @@ -13,8 +13,8 @@ export const ListenToCommentsDocument = gql\` } \`; -export function useListenToCommentsSubscription(options: Omit, 'query'>, handler?: Urql.SubscriptionHandlerArg) { - return Urql.useSubscription({ query: ListenToCommentsDocument, ...options }, handler); +export function useListenToCommentsSubscription(options?: Omit, 'query'>, handler?: Urql.SubscriptionHandlerArg) { + return Urql.useSubscription({ query: ListenToCommentsDocument, variables: undefined, ...options }, handler); };" `; @@ -24,7 +24,7 @@ import * as Urql from '@urql/vue'; export type Omit = Pick>; -export function useTestQuery(options: Omit, 'query'>) { - return Urql.useQuery({ query: Operations.TestDocument, ...options }); +export function useTestQuery(options?: Omit, 'query'>) { + return Urql.useQuery({ query: Operations.TestDocument, variables: undefined, ...options }); };" `; diff --git a/packages/plugins/typescript/vue-urql/tests/urql.spec.ts b/packages/plugins/typescript/vue-urql/tests/urql.spec.ts index 8e34bfda5..35ba34d1a 100644 --- a/packages/plugins/typescript/vue-urql/tests/urql.spec.ts +++ b/packages/plugins/typescript/vue-urql/tests/urql.spec.ts @@ -477,8 +477,8 @@ query MyFeed { )) as Types.ComplexPluginOutput; expect(content.content).toBeSimilarStringTo(` -export function useFeedQuery(options: Omit, 'query'>) { - return Urql.useQuery({ query: FeedDocument, ...options }); +export function useFeedQuery(options?: Omit, 'query'>) { + return Urql.useQuery({ query: FeedDocument, variables: undefined, ...options }); };`); expect(content.content).toBeSimilarStringTo(` @@ -524,8 +524,8 @@ export function useSubmitRepositoryMutation() { )) as Types.ComplexPluginOutput; expect(content.content).toBeSimilarStringTo(` - export function useListenToCommentsSubscription(options: Omit, 'query'>, handler?: Urql.SubscriptionHandlerArg) { - return Urql.useSubscription({ query: ListenToCommentsDocument, ...options }, handler); + export function useListenToCommentsSubscription(options?: Omit, 'query'>, handler?: Urql.SubscriptionHandlerArg) { + return Urql.useSubscription({ query: ListenToCommentsDocument, variables: undefined, ...options }, handler); };`); await validateTypeScript(content, schema, docs, {}); expect(mergeOutputs([content])).toMatchSnapshot();