Skip to content

Commit

Permalink
fix(typescript-vue-apollo): make lazy query's variables optional (#557)
Browse files Browse the repository at this point in the history
* fix(typescript-vue-apollo): make lazy query's variables optional

* Add changeset

* fix(typescript-vue-apollo): don't use default for non-nullable variables

the whole parameter must be optional instead
  • Loading branch information
FloEdelmann authored Feb 2, 2024
1 parent 4205f09 commit c2b4a1b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-cars-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-codegen/typescript-vue-apollo": patch
---

Make lazy query's variables optional
4 changes: 2 additions & 2 deletions dev-test/githunt/types.vueApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export function useCommentQuery(
);
}
export function useCommentLazyQuery(
variables:
variables?:
| CommentQueryVariables
| VueCompositionApi.Ref<CommentQueryVariables>
| ReactiveFunction<CommentQueryVariables>,
Expand Down Expand Up @@ -664,7 +664,7 @@ export function useFeedQuery(
);
}
export function useFeedLazyQuery(
variables:
variables?:
| FeedQueryVariables
| VueCompositionApi.Ref<FeedQueryVariables>
| ReactiveFunction<FeedQueryVariables>,
Expand Down
8 changes: 4 additions & 4 deletions packages/plugins/typescript/vue-apollo/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,10 @@ export class VueApolloVisitor extends ClientSideBaseVisitor<
useTypesPrefix: false,
});

const lazyOperationType = 'LazyQuery';

compositionFunctions.push(
this.buildCompositionFunction({
operationName: lazyOperationName,
operationType: lazyOperationType,
operationType: 'LazyQuery',
operationResultType,
operationVariablesTypes,
operationHasNonNullableVariable,
Expand Down Expand Up @@ -278,8 +276,10 @@ export class VueApolloVisitor extends ClientSideBaseVisitor<
operationHasVariables,
documentNodeVariable,
}: BuildCompositionFunctions): string {
const operationVariablesOptionalModifier =
operationType === 'LazyQuery' && operationHasNonNullableVariable ? '?' : '';
const variables = operationHasVariables
? `variables: ${operationVariablesTypes} | VueCompositionApi.Ref<${operationVariablesTypes}> | ReactiveFunction<${operationVariablesTypes}>${
? `variables${operationVariablesOptionalModifier}: ${operationVariablesTypes} | VueCompositionApi.Ref<${operationVariablesTypes}> | ReactiveFunction<${operationVariablesTypes}>${
operationHasNonNullableVariable ? '' : ' = {}'
}, `
: '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,9 @@ query MyFeed {
}`,
);

// lazy query with required variables
// lazy query with optional variables, see https://github.com/dotansimha/graphql-code-generator-community/issues/438
expect(content.content).toBeSimilarStringTo(
`export function useFeedLazyQuery(variables: FeedQueryVariables | VueCompositionApi.Ref<FeedQueryVariables> | ReactiveFunction<FeedQueryVariables>, options: VueApolloComposable.UseQueryOptions<FeedQuery, FeedQueryVariables> | VueCompositionApi.Ref<VueApolloComposable.UseQueryOptions<FeedQuery, FeedQueryVariables>> | ReactiveFunction<VueApolloComposable.UseQueryOptions<FeedQuery, FeedQueryVariables>> = {}) {
`export function useFeedLazyQuery(variables?: FeedQueryVariables | VueCompositionApi.Ref<FeedQueryVariables> | ReactiveFunction<FeedQueryVariables>, options: VueApolloComposable.UseQueryOptions<FeedQuery, FeedQueryVariables> | VueCompositionApi.Ref<VueApolloComposable.UseQueryOptions<FeedQuery, FeedQueryVariables>> | ReactiveFunction<VueApolloComposable.UseQueryOptions<FeedQuery, FeedQueryVariables>> = {}) {
return VueApolloComposable.useLazyQuery<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options);
}`,
);
Expand Down

0 comments on commit c2b4a1b

Please sign in to comment.