Skip to content

Commit

Permalink
improved type safety for generated React hooks, by making sure that v…
Browse files Browse the repository at this point in the history
…ariables exists when required (#5037)
  • Loading branch information
dotansimha committed Nov 4, 2020
1 parent eaf45d1 commit 9f2a4e2
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-gorillas-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-react-apollo': minor
---

Improved type-safety: in generated React Hooks, `baseOptions` will be non-optional in case there when there is a required variables with no default value
5 changes: 5 additions & 0 deletions .changeset/green-turkeys-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Expose `_hasRequiredVariables` to `buildOperation` in order to allow better type-safety for plugins that deals with `variables`
12 changes: 6 additions & 6 deletions dev-test/githunt/types.reactApollo.customSuffix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export const OnCommentAddedDocument = gql`
* });
*/
export function useOnCommentAddedSubscription(
baseOptions?: Apollo.SubscriptionHookOptions<
baseOptions: Apollo.SubscriptionHookOptions<
OnCommentAddedSubscriptionMyOperation,
OnCommentAddedSubscriptionVariables
>
Expand Down Expand Up @@ -409,7 +409,7 @@ export const CommentDocument = gql`
* },
* });
*/
export function useCommentQuery(baseOptions?: Apollo.QueryHookOptions<CommentQueryMyOperation, CommentQueryVariables>) {
export function useCommentQuery(baseOptions: Apollo.QueryHookOptions<CommentQueryMyOperation, CommentQueryVariables>) {
return Apollo.useQuery<CommentQueryMyOperation, CommentQueryVariables>(CommentDocument, baseOptions);
}
export function useCommentLazyQuery(
Expand Down Expand Up @@ -496,7 +496,7 @@ export const FeedDocument = gql`
* },
* });
*/
export function useFeedQuery(baseOptions?: Apollo.QueryHookOptions<FeedQueryMyOperation, FeedQueryVariables>) {
export function useFeedQuery(baseOptions: Apollo.QueryHookOptions<FeedQueryMyOperation, FeedQueryVariables>) {
return Apollo.useQuery<FeedQueryMyOperation, FeedQueryVariables>(FeedDocument, baseOptions);
}
export function useFeedLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<FeedQueryMyOperation, FeedQueryVariables>) {
Expand Down Expand Up @@ -535,7 +535,7 @@ export type SubmitRepositoryMutationFn = Apollo.MutationFunction<
* });
*/
export function useSubmitRepositoryMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitRepositoryMutationMyOperation, SubmitRepositoryMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitRepositoryMutationMyOperation, SubmitRepositoryMutationVariables>
) {
return Apollo.useMutation<SubmitRepositoryMutationMyOperation, SubmitRepositoryMutationVariables>(
SubmitRepositoryDocument,
Expand Down Expand Up @@ -580,7 +580,7 @@ export type SubmitCommentMutationFn = Apollo.MutationFunction<
* });
*/
export function useSubmitCommentMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitCommentMutationMyOperation, SubmitCommentMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitCommentMutationMyOperation, SubmitCommentMutationVariables>
) {
return Apollo.useMutation<SubmitCommentMutationMyOperation, SubmitCommentMutationVariables>(
SubmitCommentDocument,
Expand Down Expand Up @@ -625,7 +625,7 @@ export type VoteMutationFn = Apollo.MutationFunction<VoteMutationMyOperation, Vo
* });
*/
export function useVoteMutation(
baseOptions?: Apollo.MutationHookOptions<VoteMutationMyOperation, VoteMutationVariables>
baseOptions: Apollo.MutationHookOptions<VoteMutationMyOperation, VoteMutationVariables>
) {
return Apollo.useMutation<VoteMutationMyOperation, VoteMutationVariables>(VoteDocument, baseOptions);
}
Expand Down
12 changes: 6 additions & 6 deletions dev-test/githunt/types.reactApollo.hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export const OnCommentAddedDocument = gql`
* });
*/
export function useOnCommentAddedSubscription(
baseOptions?: Apollo.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
baseOptions: Apollo.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
) {
return Apollo.useSubscription<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>(
OnCommentAddedDocument,
Expand Down Expand Up @@ -406,7 +406,7 @@ export const CommentDocument = gql`
* },
* });
*/
export function useCommentQuery(baseOptions?: Apollo.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
export function useCommentQuery(baseOptions: Apollo.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
return Apollo.useQuery<CommentQuery, CommentQueryVariables>(CommentDocument, baseOptions);
}
export function useCommentLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<CommentQuery, CommentQueryVariables>) {
Expand Down Expand Up @@ -491,7 +491,7 @@ export const FeedDocument = gql`
* },
* });
*/
export function useFeedQuery(baseOptions?: Apollo.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
export function useFeedQuery(baseOptions: Apollo.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
return Apollo.useQuery<FeedQuery, FeedQueryVariables>(FeedDocument, baseOptions);
}
export function useFeedLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<FeedQuery, FeedQueryVariables>) {
Expand Down Expand Up @@ -530,7 +530,7 @@ export type SubmitRepositoryMutationFn = Apollo.MutationFunction<
* });
*/
export function useSubmitRepositoryMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
) {
return Apollo.useMutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(
SubmitRepositoryDocument,
Expand Down Expand Up @@ -572,7 +572,7 @@ export type SubmitCommentMutationFn = Apollo.MutationFunction<SubmitCommentMutat
* });
*/
export function useSubmitCommentMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
) {
return Apollo.useMutation<SubmitCommentMutation, SubmitCommentMutationVariables>(SubmitCommentDocument, baseOptions);
}
Expand Down Expand Up @@ -613,7 +613,7 @@ export type VoteMutationFn = Apollo.MutationFunction<VoteMutation, VoteMutationV
* },
* });
*/
export function useVoteMutation(baseOptions?: Apollo.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
export function useVoteMutation(baseOptions: Apollo.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
return Apollo.useMutation<VoteMutation, VoteMutationVariables>(VoteDocument, baseOptions);
}
export type VoteMutationHookResult = ReturnType<typeof useVoteMutation>;
Expand Down
12 changes: 6 additions & 6 deletions dev-test/githunt/types.reactApollo.preResolveTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export const OnCommentAddedDocument = gql`
* });
*/
export function useOnCommentAddedSubscription(
baseOptions?: Apollo.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
baseOptions: Apollo.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
) {
return Apollo.useSubscription<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>(
OnCommentAddedDocument,
Expand Down Expand Up @@ -433,7 +433,7 @@ export const CommentDocument = gql`
* },
* });
*/
export function useCommentQuery(baseOptions?: Apollo.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
export function useCommentQuery(baseOptions: Apollo.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
return Apollo.useQuery<CommentQuery, CommentQueryVariables>(CommentDocument, baseOptions);
}
export function useCommentLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<CommentQuery, CommentQueryVariables>) {
Expand Down Expand Up @@ -518,7 +518,7 @@ export const FeedDocument = gql`
* },
* });
*/
export function useFeedQuery(baseOptions?: Apollo.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
export function useFeedQuery(baseOptions: Apollo.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
return Apollo.useQuery<FeedQuery, FeedQueryVariables>(FeedDocument, baseOptions);
}
export function useFeedLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<FeedQuery, FeedQueryVariables>) {
Expand Down Expand Up @@ -557,7 +557,7 @@ export type SubmitRepositoryMutationFn = Apollo.MutationFunction<
* });
*/
export function useSubmitRepositoryMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
) {
return Apollo.useMutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(
SubmitRepositoryDocument,
Expand Down Expand Up @@ -599,7 +599,7 @@ export type SubmitCommentMutationFn = Apollo.MutationFunction<SubmitCommentMutat
* });
*/
export function useSubmitCommentMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
) {
return Apollo.useMutation<SubmitCommentMutation, SubmitCommentMutationVariables>(SubmitCommentDocument, baseOptions);
}
Expand Down Expand Up @@ -640,7 +640,7 @@ export type VoteMutationFn = Apollo.MutationFunction<VoteMutation, VoteMutationV
* },
* });
*/
export function useVoteMutation(baseOptions?: Apollo.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
export function useVoteMutation(baseOptions: Apollo.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
return Apollo.useMutation<VoteMutation, VoteMutationVariables>(VoteDocument, baseOptions);
}
export type VoteMutationHookResult = ReturnType<typeof useVoteMutation>;
Expand Down
12 changes: 6 additions & 6 deletions dev-test/githunt/types.reactApollo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ export const OnCommentAddedDocument = gql`
* });
*/
export function useOnCommentAddedSubscription(
baseOptions?: Apollo.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
baseOptions: Apollo.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
) {
return Apollo.useSubscription<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>(
OnCommentAddedDocument,
Expand Down Expand Up @@ -406,7 +406,7 @@ export const CommentDocument = gql`
* },
* });
*/
export function useCommentQuery(baseOptions?: Apollo.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
export function useCommentQuery(baseOptions: Apollo.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
return Apollo.useQuery<CommentQuery, CommentQueryVariables>(CommentDocument, baseOptions);
}
export function useCommentLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<CommentQuery, CommentQueryVariables>) {
Expand Down Expand Up @@ -491,7 +491,7 @@ export const FeedDocument = gql`
* },
* });
*/
export function useFeedQuery(baseOptions?: Apollo.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
export function useFeedQuery(baseOptions: Apollo.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
return Apollo.useQuery<FeedQuery, FeedQueryVariables>(FeedDocument, baseOptions);
}
export function useFeedLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<FeedQuery, FeedQueryVariables>) {
Expand Down Expand Up @@ -530,7 +530,7 @@ export type SubmitRepositoryMutationFn = Apollo.MutationFunction<
* });
*/
export function useSubmitRepositoryMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
) {
return Apollo.useMutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(
SubmitRepositoryDocument,
Expand Down Expand Up @@ -572,7 +572,7 @@ export type SubmitCommentMutationFn = Apollo.MutationFunction<SubmitCommentMutat
* });
*/
export function useSubmitCommentMutation(
baseOptions?: Apollo.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
baseOptions: Apollo.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
) {
return Apollo.useMutation<SubmitCommentMutation, SubmitCommentMutationVariables>(SubmitCommentDocument, baseOptions);
}
Expand Down Expand Up @@ -613,7 +613,7 @@ export type VoteMutationFn = Apollo.MutationFunction<VoteMutation, VoteMutationV
* },
* });
*/
export function useVoteMutation(baseOptions?: Apollo.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
export function useVoteMutation(baseOptions: Apollo.MutationHookOptions<VoteMutation, VoteMutationVariables>) {
return Apollo.useMutation<VoteMutation, VoteMutationVariables>(VoteDocument, baseOptions);
}
export type VoteMutationHookResult = ReturnType<typeof useVoteMutation>;
Expand Down
15 changes: 6 additions & 9 deletions dev-test/githunt/types.reactApollo.v2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,7 @@ export const OnCommentAddedDocument = gql`
* });
*/
export function useOnCommentAddedSubscription(
baseOptions?: ApolloReactHooks.SubscriptionHookOptions<
OnCommentAddedSubscription,
OnCommentAddedSubscriptionVariables
>
baseOptions: ApolloReactHooks.SubscriptionHookOptions<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>
) {
return ApolloReactHooks.useSubscription<OnCommentAddedSubscription, OnCommentAddedSubscriptionVariables>(
OnCommentAddedDocument,
Expand Down Expand Up @@ -410,7 +407,7 @@ export const CommentDocument = gql`
* },
* });
*/
export function useCommentQuery(baseOptions?: ApolloReactHooks.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
export function useCommentQuery(baseOptions: ApolloReactHooks.QueryHookOptions<CommentQuery, CommentQueryVariables>) {
return ApolloReactHooks.useQuery<CommentQuery, CommentQueryVariables>(CommentDocument, baseOptions);
}
export function useCommentLazyQuery(
Expand Down Expand Up @@ -497,7 +494,7 @@ export const FeedDocument = gql`
* },
* });
*/
export function useFeedQuery(baseOptions?: ApolloReactHooks.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
export function useFeedQuery(baseOptions: ApolloReactHooks.QueryHookOptions<FeedQuery, FeedQueryVariables>) {
return ApolloReactHooks.useQuery<FeedQuery, FeedQueryVariables>(FeedDocument, baseOptions);
}
export function useFeedLazyQuery(baseOptions?: ApolloReactHooks.LazyQueryHookOptions<FeedQuery, FeedQueryVariables>) {
Expand Down Expand Up @@ -536,7 +533,7 @@ export type SubmitRepositoryMutationFn = ApolloReactCommon.MutationFunction<
* });
*/
export function useSubmitRepositoryMutation(
baseOptions?: ApolloReactHooks.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
baseOptions: ApolloReactHooks.MutationHookOptions<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>
) {
return ApolloReactHooks.useMutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>(
SubmitRepositoryDocument,
Expand Down Expand Up @@ -581,7 +578,7 @@ export type SubmitCommentMutationFn = ApolloReactCommon.MutationFunction<
* });
*/
export function useSubmitCommentMutation(
baseOptions?: ApolloReactHooks.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
baseOptions: ApolloReactHooks.MutationHookOptions<SubmitCommentMutation, SubmitCommentMutationVariables>
) {
return ApolloReactHooks.useMutation<SubmitCommentMutation, SubmitCommentMutationVariables>(
SubmitCommentDocument,
Expand Down Expand Up @@ -626,7 +623,7 @@ export type VoteMutationFn = ApolloReactCommon.MutationFunction<VoteMutation, Vo
* });
*/
export function useVoteMutation(
baseOptions?: ApolloReactHooks.MutationHookOptions<VoteMutation, VoteMutationVariables>
baseOptions: ApolloReactHooks.MutationHookOptions<VoteMutation, VoteMutationVariables>
) {
return ApolloReactHooks.useMutation<VoteMutation, VoteMutationVariables>(VoteDocument, baseOptions);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dev-test/star-wars/__generated__/HeroNameConditional.tsx

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions dev-test/star-wars/types.refetchFn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export type CreateReviewForEpisodeMutationFn = Apollo.MutationFunction<
* });
*/
export function useCreateReviewForEpisodeMutation(
baseOptions?: Apollo.MutationHookOptions<CreateReviewForEpisodeMutation, CreateReviewForEpisodeMutationVariables>
baseOptions: Apollo.MutationHookOptions<CreateReviewForEpisodeMutation, CreateReviewForEpisodeMutationVariables>
) {
return Apollo.useMutation<CreateReviewForEpisodeMutation, CreateReviewForEpisodeMutationVariables>(
CreateReviewForEpisodeDocument,
Expand Down Expand Up @@ -549,7 +549,7 @@ export const HeroNameConditionalInclusionDocument = gql`
* });
*/
export function useHeroNameConditionalInclusionQuery(
baseOptions?: Apollo.QueryHookOptions<HeroNameConditionalInclusionQuery, HeroNameConditionalInclusionQueryVariables>
baseOptions: Apollo.QueryHookOptions<HeroNameConditionalInclusionQuery, HeroNameConditionalInclusionQueryVariables>
) {
return Apollo.useQuery<HeroNameConditionalInclusionQuery, HeroNameConditionalInclusionQueryVariables>(
HeroNameConditionalInclusionDocument,
Expand Down Expand Up @@ -604,7 +604,7 @@ export const HeroNameConditionalExclusionDocument = gql`
* });
*/
export function useHeroNameConditionalExclusionQuery(
baseOptions?: Apollo.QueryHookOptions<HeroNameConditionalExclusionQuery, HeroNameConditionalExclusionQueryVariables>
baseOptions: Apollo.QueryHookOptions<HeroNameConditionalExclusionQuery, HeroNameConditionalExclusionQueryVariables>
) {
return Apollo.useQuery<HeroNameConditionalExclusionQuery, HeroNameConditionalExclusionQueryVariables>(
HeroNameConditionalExclusionDocument,
Expand Down

0 comments on commit 9f2a4e2

Please sign in to comment.