Skip to content

Commit 0b26199

Browse files
authored
[typescript-operations] No optional Result fields, unless deferred or conditional (#10548)
* Fix types issues and put comments * Handle avoidOptionals * Update integration tests * Add FIXME to pass test for now * Add changeset
1 parent c8b8955 commit 0b26199

File tree

52 files changed

+838
-1200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+838
-1200
lines changed

.changeset/breezy-games-enter.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': major
3+
'@graphql-codegen/typescript-operations': major
4+
'@graphql-codegen/client-preset': major
5+
---
6+
7+
Fix nullable field optionality in operations
8+
9+
Previously, a nullable Result field is generated as optional (marked by `?` TypeScript modifier) by default. This is not correct, because generally at runtime such field can only be `null`, and not `undefined` (both missing from the object OR `undefined`). The only exceptions are when fields are deferred (using `@defer` directive) or marked as conditional (using `@skip` or `@include`).
10+
11+
Now, a nullable Result field cannot be optional unless the exceptions are met. This also limits `avoidOptionals` to only target Variables input, since some users may want to force explicit `null` when providing operation variables.

dev-test/githunt/typed-document-nodes.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
77

88
export type OnCommentAddedSubscription = {
99
__typename?: 'Subscription';
10-
commentAdded?: {
10+
commentAdded: {
1111
__typename?: 'Comment';
1212
id: number;
1313
createdAt: number;
@@ -24,8 +24,8 @@ export type CommentQueryVariables = Exact<{
2424

2525
export type CommentQuery = {
2626
__typename?: 'Query';
27-
currentUser?: { __typename?: 'User'; login: string; html_url: string } | null;
28-
entry?: {
27+
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
28+
entry: {
2929
__typename?: 'Entry';
3030
id: number;
3131
createdAt: number;
@@ -40,8 +40,8 @@ export type CommentQuery = {
4040
} | null>;
4141
repository: {
4242
__typename?: 'Repository';
43-
description?: string | null;
44-
open_issues_count?: number | null;
43+
description: string | null;
44+
open_issues_count: number | null;
4545
stargazers_count: number;
4646
full_name: string;
4747
html_url: string;
@@ -61,7 +61,7 @@ export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }
6161

6262
export type CurrentUserForProfileQuery = {
6363
__typename?: 'Query';
64-
currentUser?: { __typename?: 'User'; login: string; avatar_url: string } | null;
64+
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
6565
};
6666

6767
export type FeedEntryFragment = {
@@ -74,10 +74,10 @@ export type FeedEntryFragment = {
7474
__typename?: 'Repository';
7575
full_name: string;
7676
html_url: string;
77-
description?: string | null;
77+
description: string | null;
7878
stargazers_count: number;
79-
open_issues_count?: number | null;
80-
owner?: { __typename?: 'User'; avatar_url: string } | null;
79+
open_issues_count: number | null;
80+
owner: { __typename?: 'User'; avatar_url: string } | null;
8181
};
8282
vote: { __typename?: 'Vote'; vote_value: number };
8383
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -91,8 +91,8 @@ export type FeedQueryVariables = Exact<{
9191

9292
export type FeedQuery = {
9393
__typename?: 'Query';
94-
currentUser?: { __typename?: 'User'; login: string } | null;
95-
feed?: Array<{
94+
currentUser: { __typename?: 'User'; login: string } | null;
95+
feed: Array<{
9696
__typename?: 'Entry';
9797
id: number;
9898
commentCount: number;
@@ -102,10 +102,10 @@ export type FeedQuery = {
102102
__typename?: 'Repository';
103103
full_name: string;
104104
html_url: string;
105-
description?: string | null;
105+
description: string | null;
106106
stargazers_count: number;
107-
open_issues_count?: number | null;
108-
owner?: { __typename?: 'User'; avatar_url: string } | null;
107+
open_issues_count: number | null;
108+
owner: { __typename?: 'User'; avatar_url: string } | null;
109109
};
110110
vote: { __typename?: 'Vote'; vote_value: number };
111111
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -118,17 +118,17 @@ export type SubmitRepositoryMutationVariables = Exact<{
118118

119119
export type SubmitRepositoryMutation = {
120120
__typename?: 'Mutation';
121-
submitRepository?: { __typename?: 'Entry'; createdAt: number } | null;
121+
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
122122
};
123123

124124
export type RepoInfoFragment = {
125125
__typename?: 'Entry';
126126
createdAt: number;
127127
repository: {
128128
__typename?: 'Repository';
129-
description?: string | null;
129+
description: string | null;
130130
stargazers_count: number;
131-
open_issues_count?: number | null;
131+
open_issues_count: number | null;
132132
};
133133
postedBy: { __typename?: 'User'; html_url: string; login: string };
134134
};
@@ -140,7 +140,7 @@ export type SubmitCommentMutationVariables = Exact<{
140140

141141
export type SubmitCommentMutation = {
142142
__typename?: 'Mutation';
143-
submitComment?: {
143+
submitComment: {
144144
__typename?: 'Comment';
145145
id: number;
146146
createdAt: number;
@@ -162,7 +162,7 @@ export type VoteMutationVariables = Exact<{
162162

163163
export type VoteMutation = {
164164
__typename?: 'Mutation';
165-
vote?: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
165+
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
166166
};
167167

168168
export const CommentsPageCommentFragmentDoc = {

dev-test/githunt/types.d.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
66

77
export type OnCommentAddedSubscription = {
88
__typename?: 'Subscription';
9-
commentAdded?: {
9+
commentAdded: {
1010
__typename?: 'Comment';
1111
id: number;
1212
createdAt: number;
@@ -23,8 +23,8 @@ export type CommentQueryVariables = Exact<{
2323

2424
export type CommentQuery = {
2525
__typename?: 'Query';
26-
currentUser?: { __typename?: 'User'; login: string; html_url: string } | null;
27-
entry?: {
26+
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
27+
entry: {
2828
__typename?: 'Entry';
2929
id: number;
3030
createdAt: number;
@@ -39,8 +39,8 @@ export type CommentQuery = {
3939
} | null>;
4040
repository: {
4141
__typename?: 'Repository';
42-
description?: string | null;
43-
open_issues_count?: number | null;
42+
description: string | null;
43+
open_issues_count: number | null;
4444
stargazers_count: number;
4545
full_name: string;
4646
html_url: string;
@@ -60,7 +60,7 @@ export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }
6060

6161
export type CurrentUserForProfileQuery = {
6262
__typename?: 'Query';
63-
currentUser?: { __typename?: 'User'; login: string; avatar_url: string } | null;
63+
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
6464
};
6565

6666
export type FeedEntryFragment = {
@@ -73,10 +73,10 @@ export type FeedEntryFragment = {
7373
__typename?: 'Repository';
7474
full_name: string;
7575
html_url: string;
76-
description?: string | null;
76+
description: string | null;
7777
stargazers_count: number;
78-
open_issues_count?: number | null;
79-
owner?: { __typename?: 'User'; avatar_url: string } | null;
78+
open_issues_count: number | null;
79+
owner: { __typename?: 'User'; avatar_url: string } | null;
8080
};
8181
vote: { __typename?: 'Vote'; vote_value: number };
8282
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -90,8 +90,8 @@ export type FeedQueryVariables = Exact<{
9090

9191
export type FeedQuery = {
9292
__typename?: 'Query';
93-
currentUser?: { __typename?: 'User'; login: string } | null;
94-
feed?: Array<{
93+
currentUser: { __typename?: 'User'; login: string } | null;
94+
feed: Array<{
9595
__typename?: 'Entry';
9696
id: number;
9797
commentCount: number;
@@ -101,10 +101,10 @@ export type FeedQuery = {
101101
__typename?: 'Repository';
102102
full_name: string;
103103
html_url: string;
104-
description?: string | null;
104+
description: string | null;
105105
stargazers_count: number;
106-
open_issues_count?: number | null;
107-
owner?: { __typename?: 'User'; avatar_url: string } | null;
106+
open_issues_count: number | null;
107+
owner: { __typename?: 'User'; avatar_url: string } | null;
108108
};
109109
vote: { __typename?: 'Vote'; vote_value: number };
110110
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -117,17 +117,17 @@ export type SubmitRepositoryMutationVariables = Exact<{
117117

118118
export type SubmitRepositoryMutation = {
119119
__typename?: 'Mutation';
120-
submitRepository?: { __typename?: 'Entry'; createdAt: number } | null;
120+
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
121121
};
122122

123123
export type RepoInfoFragment = {
124124
__typename?: 'Entry';
125125
createdAt: number;
126126
repository: {
127127
__typename?: 'Repository';
128-
description?: string | null;
128+
description: string | null;
129129
stargazers_count: number;
130-
open_issues_count?: number | null;
130+
open_issues_count: number | null;
131131
};
132132
postedBy: { __typename?: 'User'; html_url: string; login: string };
133133
};
@@ -139,7 +139,7 @@ export type SubmitCommentMutationVariables = Exact<{
139139

140140
export type SubmitCommentMutation = {
141141
__typename?: 'Mutation';
142-
submitComment?: {
142+
submitComment: {
143143
__typename?: 'Comment';
144144
id: number;
145145
createdAt: number;
@@ -161,5 +161,5 @@ export type VoteMutationVariables = Exact<{
161161

162162
export type VoteMutation = {
163163
__typename?: 'Mutation';
164-
vote?: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
164+
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
165165
};

dev-test/githunt/types.enumsAsTypes.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
66

77
export type OnCommentAddedSubscription = {
88
__typename?: 'Subscription';
9-
commentAdded?: {
9+
commentAdded: {
1010
__typename?: 'Comment';
1111
id: number;
1212
createdAt: number;
@@ -23,8 +23,8 @@ export type CommentQueryVariables = Exact<{
2323

2424
export type CommentQuery = {
2525
__typename?: 'Query';
26-
currentUser?: { __typename?: 'User'; login: string; html_url: string } | null;
27-
entry?: {
26+
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
27+
entry: {
2828
__typename?: 'Entry';
2929
id: number;
3030
createdAt: number;
@@ -39,8 +39,8 @@ export type CommentQuery = {
3939
} | null>;
4040
repository: {
4141
__typename?: 'Repository';
42-
description?: string | null;
43-
open_issues_count?: number | null;
42+
description: string | null;
43+
open_issues_count: number | null;
4444
stargazers_count: number;
4545
full_name: string;
4646
html_url: string;
@@ -60,7 +60,7 @@ export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }
6060

6161
export type CurrentUserForProfileQuery = {
6262
__typename?: 'Query';
63-
currentUser?: { __typename?: 'User'; login: string; avatar_url: string } | null;
63+
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
6464
};
6565

6666
export type FeedEntryFragment = {
@@ -73,10 +73,10 @@ export type FeedEntryFragment = {
7373
__typename?: 'Repository';
7474
full_name: string;
7575
html_url: string;
76-
description?: string | null;
76+
description: string | null;
7777
stargazers_count: number;
78-
open_issues_count?: number | null;
79-
owner?: { __typename?: 'User'; avatar_url: string } | null;
78+
open_issues_count: number | null;
79+
owner: { __typename?: 'User'; avatar_url: string } | null;
8080
};
8181
vote: { __typename?: 'Vote'; vote_value: number };
8282
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -90,8 +90,8 @@ export type FeedQueryVariables = Exact<{
9090

9191
export type FeedQuery = {
9292
__typename?: 'Query';
93-
currentUser?: { __typename?: 'User'; login: string } | null;
94-
feed?: Array<{
93+
currentUser: { __typename?: 'User'; login: string } | null;
94+
feed: Array<{
9595
__typename?: 'Entry';
9696
id: number;
9797
commentCount: number;
@@ -101,10 +101,10 @@ export type FeedQuery = {
101101
__typename?: 'Repository';
102102
full_name: string;
103103
html_url: string;
104-
description?: string | null;
104+
description: string | null;
105105
stargazers_count: number;
106-
open_issues_count?: number | null;
107-
owner?: { __typename?: 'User'; avatar_url: string } | null;
106+
open_issues_count: number | null;
107+
owner: { __typename?: 'User'; avatar_url: string } | null;
108108
};
109109
vote: { __typename?: 'Vote'; vote_value: number };
110110
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -117,17 +117,17 @@ export type SubmitRepositoryMutationVariables = Exact<{
117117

118118
export type SubmitRepositoryMutation = {
119119
__typename?: 'Mutation';
120-
submitRepository?: { __typename?: 'Entry'; createdAt: number } | null;
120+
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
121121
};
122122

123123
export type RepoInfoFragment = {
124124
__typename?: 'Entry';
125125
createdAt: number;
126126
repository: {
127127
__typename?: 'Repository';
128-
description?: string | null;
128+
description: string | null;
129129
stargazers_count: number;
130-
open_issues_count?: number | null;
130+
open_issues_count: number | null;
131131
};
132132
postedBy: { __typename?: 'User'; html_url: string; login: string };
133133
};
@@ -139,7 +139,7 @@ export type SubmitCommentMutationVariables = Exact<{
139139

140140
export type SubmitCommentMutation = {
141141
__typename?: 'Mutation';
142-
submitComment?: {
142+
submitComment: {
143143
__typename?: 'Comment';
144144
id: number;
145145
createdAt: number;
@@ -161,5 +161,5 @@ export type VoteMutationVariables = Exact<{
161161

162162
export type VoteMutation = {
163163
__typename?: 'Mutation';
164-
vote?: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
164+
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
165165
};

0 commit comments

Comments
 (0)