Skip to content

Commit d59d970

Browse files
committed
[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 dafe7b1 commit d59d970

52 files changed

Lines changed: 1078 additions & 1086 deletions

File tree

Some content is hidden

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

.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: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
1010

1111
export type OnCommentAddedSubscription = {
1212
__typename?: 'Subscription';
13-
commentAdded?: {
13+
commentAdded: {
1414
__typename?: 'Comment';
1515
id: number;
1616
createdAt: number;
@@ -27,8 +27,8 @@ export type CommentQueryVariables = Exact<{
2727

2828
export type CommentQuery = {
2929
__typename?: 'Query';
30-
currentUser?: { __typename?: 'User'; login: string; html_url: string } | null;
31-
entry?: {
30+
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
31+
entry: {
3232
__typename?: 'Entry';
3333
id: number;
3434
createdAt: number;
@@ -43,8 +43,8 @@ export type CommentQuery = {
4343
} | null>;
4444
repository: {
4545
__typename?: 'Repository';
46-
description?: string | null;
47-
open_issues_count?: number | null;
46+
description: string | null;
47+
open_issues_count: number | null;
4848
stargazers_count: number;
4949
full_name: string;
5050
html_url: string;
@@ -64,7 +64,7 @@ export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }
6464

6565
export type CurrentUserForProfileQuery = {
6666
__typename?: 'Query';
67-
currentUser?: { __typename?: 'User'; login: string; avatar_url: string } | null;
67+
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
6868
};
6969

7070
export type FeedEntryFragment = {
@@ -77,10 +77,10 @@ export type FeedEntryFragment = {
7777
__typename?: 'Repository';
7878
full_name: string;
7979
html_url: string;
80-
description?: string | null;
80+
description: string | null;
8181
stargazers_count: number;
82-
open_issues_count?: number | null;
83-
owner?: { __typename?: 'User'; avatar_url: string } | null;
82+
open_issues_count: number | null;
83+
owner: { __typename?: 'User'; avatar_url: string } | null;
8484
};
8585
vote: { __typename?: 'Vote'; vote_value: number };
8686
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -94,8 +94,8 @@ export type FeedQueryVariables = Exact<{
9494

9595
export type FeedQuery = {
9696
__typename?: 'Query';
97-
currentUser?: { __typename?: 'User'; login: string } | null;
98-
feed?: Array<{
97+
currentUser: { __typename?: 'User'; login: string } | null;
98+
feed: Array<{
9999
__typename?: 'Entry';
100100
id: number;
101101
commentCount: number;
@@ -105,10 +105,10 @@ export type FeedQuery = {
105105
__typename?: 'Repository';
106106
full_name: string;
107107
html_url: string;
108-
description?: string | null;
108+
description: string | null;
109109
stargazers_count: number;
110-
open_issues_count?: number | null;
111-
owner?: { __typename?: 'User'; avatar_url: string } | null;
110+
open_issues_count: number | null;
111+
owner: { __typename?: 'User'; avatar_url: string } | null;
112112
};
113113
vote: { __typename?: 'Vote'; vote_value: number };
114114
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -121,17 +121,17 @@ export type SubmitRepositoryMutationVariables = Exact<{
121121

122122
export type SubmitRepositoryMutation = {
123123
__typename?: 'Mutation';
124-
submitRepository?: { __typename?: 'Entry'; createdAt: number } | null;
124+
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
125125
};
126126

127127
export type RepoInfoFragment = {
128128
__typename?: 'Entry';
129129
createdAt: number;
130130
repository: {
131131
__typename?: 'Repository';
132-
description?: string | null;
132+
description: string | null;
133133
stargazers_count: number;
134-
open_issues_count?: number | null;
134+
open_issues_count: number | null;
135135
};
136136
postedBy: { __typename?: 'User'; html_url: string; login: string };
137137
};
@@ -143,7 +143,7 @@ export type SubmitCommentMutationVariables = Exact<{
143143

144144
export type SubmitCommentMutation = {
145145
__typename?: 'Mutation';
146-
submitComment?: {
146+
submitComment: {
147147
__typename?: 'Comment';
148148
id: number;
149149
createdAt: number;
@@ -165,12 +165,16 @@ export type VoteMutationVariables = Exact<{
165165

166166
export type VoteMutation = {
167167
__typename?: 'Mutation';
168+
<<<<<<< HEAD
168169
vote?: {
169170
__typename?: 'Entry';
170171
score: number;
171172
id: number;
172173
vote: { __typename?: 'Vote'; vote_value: number };
173174
} | null;
175+
=======
176+
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
177+
>>>>>>> caa1c98e0 ([typescript-operations] No optional Result fields, unless deferred or conditional (#10548))
174178
};
175179

176180
export const CommentsPageCommentFragmentDoc = {

dev-test/githunt/types.d.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
88

99
export type OnCommentAddedSubscription = {
1010
__typename?: 'Subscription';
11-
commentAdded?: {
11+
commentAdded: {
1212
__typename?: 'Comment';
1313
id: number;
1414
createdAt: number;
@@ -25,8 +25,8 @@ export type CommentQueryVariables = Exact<{
2525

2626
export type CommentQuery = {
2727
__typename?: 'Query';
28-
currentUser?: { __typename?: 'User'; login: string; html_url: string } | null;
29-
entry?: {
28+
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
29+
entry: {
3030
__typename?: 'Entry';
3131
id: number;
3232
createdAt: number;
@@ -41,8 +41,8 @@ export type CommentQuery = {
4141
} | null>;
4242
repository: {
4343
__typename?: 'Repository';
44-
description?: string | null;
45-
open_issues_count?: number | null;
44+
description: string | null;
45+
open_issues_count: number | null;
4646
stargazers_count: number;
4747
full_name: string;
4848
html_url: string;
@@ -62,7 +62,7 @@ export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }
6262

6363
export type CurrentUserForProfileQuery = {
6464
__typename?: 'Query';
65-
currentUser?: { __typename?: 'User'; login: string; avatar_url: string } | null;
65+
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
6666
};
6767

6868
export type FeedEntryFragment = {
@@ -75,10 +75,10 @@ export type FeedEntryFragment = {
7575
__typename?: 'Repository';
7676
full_name: string;
7777
html_url: string;
78-
description?: string | null;
78+
description: string | null;
7979
stargazers_count: number;
80-
open_issues_count?: number | null;
81-
owner?: { __typename?: 'User'; avatar_url: string } | null;
80+
open_issues_count: number | null;
81+
owner: { __typename?: 'User'; avatar_url: string } | null;
8282
};
8383
vote: { __typename?: 'Vote'; vote_value: number };
8484
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -92,8 +92,8 @@ export type FeedQueryVariables = Exact<{
9292

9393
export type FeedQuery = {
9494
__typename?: 'Query';
95-
currentUser?: { __typename?: 'User'; login: string } | null;
96-
feed?: Array<{
95+
currentUser: { __typename?: 'User'; login: string } | null;
96+
feed: Array<{
9797
__typename?: 'Entry';
9898
id: number;
9999
commentCount: number;
@@ -103,10 +103,10 @@ export type FeedQuery = {
103103
__typename?: 'Repository';
104104
full_name: string;
105105
html_url: string;
106-
description?: string | null;
106+
description: string | null;
107107
stargazers_count: number;
108-
open_issues_count?: number | null;
109-
owner?: { __typename?: 'User'; avatar_url: string } | null;
108+
open_issues_count: number | null;
109+
owner: { __typename?: 'User'; avatar_url: string } | null;
110110
};
111111
vote: { __typename?: 'Vote'; vote_value: number };
112112
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -119,17 +119,17 @@ export type SubmitRepositoryMutationVariables = Exact<{
119119

120120
export type SubmitRepositoryMutation = {
121121
__typename?: 'Mutation';
122-
submitRepository?: { __typename?: 'Entry'; createdAt: number } | null;
122+
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
123123
};
124124

125125
export type RepoInfoFragment = {
126126
__typename?: 'Entry';
127127
createdAt: number;
128128
repository: {
129129
__typename?: 'Repository';
130-
description?: string | null;
130+
description: string | null;
131131
stargazers_count: number;
132-
open_issues_count?: number | null;
132+
open_issues_count: number | null;
133133
};
134134
postedBy: { __typename?: 'User'; html_url: string; login: string };
135135
};
@@ -141,7 +141,7 @@ export type SubmitCommentMutationVariables = Exact<{
141141

142142
export type SubmitCommentMutation = {
143143
__typename?: 'Mutation';
144-
submitComment?: {
144+
submitComment: {
145145
__typename?: 'Comment';
146146
id: number;
147147
createdAt: number;
@@ -163,10 +163,14 @@ export type VoteMutationVariables = Exact<{
163163

164164
export type VoteMutation = {
165165
__typename?: 'Mutation';
166+
<<<<<<< HEAD
166167
vote?: {
167168
__typename?: 'Entry';
168169
score: number;
169170
id: number;
170171
vote: { __typename?: 'Vote'; vote_value: number };
171172
} | null;
173+
=======
174+
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
175+
>>>>>>> caa1c98e0 ([typescript-operations] No optional Result fields, unless deferred or conditional (#10548))
172176
};

dev-test/githunt/types.enumsAsTypes.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
88

99
export type OnCommentAddedSubscription = {
1010
__typename?: 'Subscription';
11-
commentAdded?: {
11+
commentAdded: {
1212
__typename?: 'Comment';
1313
id: number;
1414
createdAt: number;
@@ -25,8 +25,8 @@ export type CommentQueryVariables = Exact<{
2525

2626
export type CommentQuery = {
2727
__typename?: 'Query';
28-
currentUser?: { __typename?: 'User'; login: string; html_url: string } | null;
29-
entry?: {
28+
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
29+
entry: {
3030
__typename?: 'Entry';
3131
id: number;
3232
createdAt: number;
@@ -41,8 +41,8 @@ export type CommentQuery = {
4141
} | null>;
4242
repository: {
4343
__typename?: 'Repository';
44-
description?: string | null;
45-
open_issues_count?: number | null;
44+
description: string | null;
45+
open_issues_count: number | null;
4646
stargazers_count: number;
4747
full_name: string;
4848
html_url: string;
@@ -62,7 +62,7 @@ export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }
6262

6363
export type CurrentUserForProfileQuery = {
6464
__typename?: 'Query';
65-
currentUser?: { __typename?: 'User'; login: string; avatar_url: string } | null;
65+
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
6666
};
6767

6868
export type FeedEntryFragment = {
@@ -75,10 +75,10 @@ export type FeedEntryFragment = {
7575
__typename?: 'Repository';
7676
full_name: string;
7777
html_url: string;
78-
description?: string | null;
78+
description: string | null;
7979
stargazers_count: number;
80-
open_issues_count?: number | null;
81-
owner?: { __typename?: 'User'; avatar_url: string } | null;
80+
open_issues_count: number | null;
81+
owner: { __typename?: 'User'; avatar_url: string } | null;
8282
};
8383
vote: { __typename?: 'Vote'; vote_value: number };
8484
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -92,8 +92,8 @@ export type FeedQueryVariables = Exact<{
9292

9393
export type FeedQuery = {
9494
__typename?: 'Query';
95-
currentUser?: { __typename?: 'User'; login: string } | null;
96-
feed?: Array<{
95+
currentUser: { __typename?: 'User'; login: string } | null;
96+
feed: Array<{
9797
__typename?: 'Entry';
9898
id: number;
9999
commentCount: number;
@@ -103,10 +103,10 @@ export type FeedQuery = {
103103
__typename?: 'Repository';
104104
full_name: string;
105105
html_url: string;
106-
description?: string | null;
106+
description: string | null;
107107
stargazers_count: number;
108-
open_issues_count?: number | null;
109-
owner?: { __typename?: 'User'; avatar_url: string } | null;
108+
open_issues_count: number | null;
109+
owner: { __typename?: 'User'; avatar_url: string } | null;
110110
};
111111
vote: { __typename?: 'Vote'; vote_value: number };
112112
postedBy: { __typename?: 'User'; html_url: string; login: string };
@@ -119,17 +119,17 @@ export type SubmitRepositoryMutationVariables = Exact<{
119119

120120
export type SubmitRepositoryMutation = {
121121
__typename?: 'Mutation';
122-
submitRepository?: { __typename?: 'Entry'; createdAt: number } | null;
122+
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
123123
};
124124

125125
export type RepoInfoFragment = {
126126
__typename?: 'Entry';
127127
createdAt: number;
128128
repository: {
129129
__typename?: 'Repository';
130-
description?: string | null;
130+
description: string | null;
131131
stargazers_count: number;
132-
open_issues_count?: number | null;
132+
open_issues_count: number | null;
133133
};
134134
postedBy: { __typename?: 'User'; html_url: string; login: string };
135135
};
@@ -141,7 +141,7 @@ export type SubmitCommentMutationVariables = Exact<{
141141

142142
export type SubmitCommentMutation = {
143143
__typename?: 'Mutation';
144-
submitComment?: {
144+
submitComment: {
145145
__typename?: 'Comment';
146146
id: number;
147147
createdAt: number;
@@ -163,10 +163,14 @@ export type VoteMutationVariables = Exact<{
163163

164164
export type VoteMutation = {
165165
__typename?: 'Mutation';
166+
<<<<<<< HEAD
166167
vote?: {
167168
__typename?: 'Entry';
168169
score: number;
169170
id: number;
170171
vote: { __typename?: 'Vote'; vote_value: number };
171172
} | null;
173+
=======
174+
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
175+
>>>>>>> caa1c98e0 ([typescript-operations] No optional Result fields, unless deferred or conditional (#10548))
172176
};

0 commit comments

Comments
 (0)