Skip to content

Commit 5a1e550

Browse files
committed
[typescript-operations] Fix __typename optionality and nullability (#10552)
* Fix typename optionality and existence at Result root and types * Update tests * Update tests * Stop allowing skipTypename into Client Preset and typescript-operations, and update tests * Remove dependency on typescript plugin * Add changeset
1 parent 5b9de85 commit 5a1e550

60 files changed

Lines changed: 2296 additions & 3518 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/real-numbers-fall.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': major
3+
'@graphql-codegen/typescript-operations': major
4+
'@graphql-codegen/client-preset': major
5+
---
6+
7+
BREAKING CHANGE: Operation plugin and Client Preset no longer generates optional `__typename` for result type
8+
9+
`__typenam` should not be in the request unless:
10+
11+
- explicitly requested by the user
12+
- automatically injected into the request by clients, such as Apollo Clients.
13+
14+
Note: Apollo Client users can still use `nonOptionalTypename: true` and `skipTypeNameForRoot: true` to ensure generated types match the runtime behaviour.

dev-test/external-documents/app/types.generated.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
22
export type Incremental<T> =
33
| T
44
| { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
5+
export type UserRole = 'ADMIN' | 'CUSTOMER';
6+
57
export type UserQueryVariables = Exact<{
68
id: string;
79
}>;
810

9-
export type UserQuery = {
10-
__typename?: 'Query';
11-
user?: { __typename?: 'User'; id: string; name: string; role: UserRole } | null;
12-
};
11+
export type UserQuery = { user: { id: string; name: string; role: UserRole } | null };

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

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
99
}>;
1010

1111
export type OnCommentAddedSubscription = {
12-
__typename?: 'Subscription';
1312
commentAdded: {
14-
__typename?: 'Comment';
1513
id: number;
1614
createdAt: number;
1715
content: string;
18-
postedBy: { __typename?: 'User'; login: string; html_url: string };
16+
postedBy: { login: string; html_url: string };
1917
} | null;
2018
};
2119

@@ -26,23 +24,19 @@ export type CommentQueryVariables = Exact<{
2624
}>;
2725

2826
export type CommentQuery = {
29-
__typename?: 'Query';
30-
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
27+
currentUser: { login: string; html_url: string } | null;
3128
entry: {
32-
__typename?: 'Entry';
3329
id: number;
3430
createdAt: number;
3531
commentCount: number;
36-
postedBy: { __typename?: 'User'; login: string; html_url: string };
32+
postedBy: { login: string; html_url: string };
3733
comments: Array<{
38-
__typename?: 'Comment';
3934
id: number;
4035
createdAt: number;
4136
content: string;
42-
postedBy: { __typename?: 'User'; login: string; html_url: string };
37+
postedBy: { login: string; html_url: string };
4338
} | null>;
4439
repository: {
45-
__typename?: 'Repository';
4640
description: string | null;
4741
open_issues_count: number | null;
4842
stargazers_count: number;
@@ -53,37 +47,33 @@ export type CommentQuery = {
5347
};
5448

5549
export type CommentsPageCommentFragment = {
56-
__typename?: 'Comment';
5750
id: number;
5851
createdAt: number;
5952
content: string;
60-
postedBy: { __typename?: 'User'; login: string; html_url: string };
53+
postedBy: { login: string; html_url: string };
6154
};
6255

6356
export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }>;
6457

6558
export type CurrentUserForProfileQuery = {
66-
__typename?: 'Query';
67-
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
59+
currentUser: { login: string; avatar_url: string } | null;
6860
};
6961

7062
export type FeedEntryFragment = {
71-
__typename?: 'Entry';
7263
id: number;
7364
commentCount: number;
7465
score: number;
7566
createdAt: number;
7667
repository: {
77-
__typename?: 'Repository';
7868
full_name: string;
7969
html_url: string;
8070
description: string | null;
8171
stargazers_count: number;
8272
open_issues_count: number | null;
83-
owner: { __typename?: 'User'; avatar_url: string } | null;
73+
owner: { avatar_url: string } | null;
8474
};
85-
vote: { __typename?: 'Vote'; vote_value: number };
86-
postedBy: { __typename?: 'User'; html_url: string; login: string };
75+
vote: { vote_value: number };
76+
postedBy: { html_url: string; login: string };
8777
};
8878

8979
export type FeedQueryVariables = Exact<{
@@ -93,47 +83,39 @@ export type FeedQueryVariables = Exact<{
9383
}>;
9484

9585
export type FeedQuery = {
96-
__typename?: 'Query';
97-
currentUser: { __typename?: 'User'; login: string } | null;
86+
currentUser: { login: string } | null;
9887
feed: Array<{
99-
__typename?: 'Entry';
10088
id: number;
10189
commentCount: number;
10290
score: number;
10391
createdAt: number;
10492
repository: {
105-
__typename?: 'Repository';
10693
full_name: string;
10794
html_url: string;
10895
description: string | null;
10996
stargazers_count: number;
11097
open_issues_count: number | null;
111-
owner: { __typename?: 'User'; avatar_url: string } | null;
98+
owner: { avatar_url: string } | null;
11299
};
113-
vote: { __typename?: 'Vote'; vote_value: number };
114-
postedBy: { __typename?: 'User'; html_url: string; login: string };
100+
vote: { vote_value: number };
101+
postedBy: { html_url: string; login: string };
115102
} | null> | null;
116103
};
117104

118105
export type SubmitRepositoryMutationVariables = Exact<{
119106
repoFullName: string;
120107
}>;
121108

122-
export type SubmitRepositoryMutation = {
123-
__typename?: 'Mutation';
124-
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
125-
};
109+
export type SubmitRepositoryMutation = { submitRepository: { createdAt: number } | null };
126110

127111
export type RepoInfoFragment = {
128-
__typename?: 'Entry';
129112
createdAt: number;
130113
repository: {
131-
__typename?: 'Repository';
132114
description: string | null;
133115
stargazers_count: number;
134116
open_issues_count: number | null;
135117
};
136-
postedBy: { __typename?: 'User'; html_url: string; login: string };
118+
postedBy: { html_url: string; login: string };
137119
};
138120

139121
export type SubmitCommentMutationVariables = Exact<{
@@ -142,39 +124,23 @@ export type SubmitCommentMutationVariables = Exact<{
142124
}>;
143125

144126
export type SubmitCommentMutation = {
145-
__typename?: 'Mutation';
146127
submitComment: {
147-
__typename?: 'Comment';
148128
id: number;
149129
createdAt: number;
150130
content: string;
151-
postedBy: { __typename?: 'User'; login: string; html_url: string };
131+
postedBy: { login: string; html_url: string };
152132
} | null;
153133
};
154134

155-
export type VoteButtonsFragment = {
156-
__typename?: 'Entry';
157-
score: number;
158-
vote: { __typename?: 'Vote'; vote_value: number };
159-
};
135+
export type VoteButtonsFragment = { score: number; vote: { vote_value: number } };
160136

161137
export type VoteMutationVariables = Exact<{
162138
repoFullName: string;
163139
type: VoteType;
164140
}>;
165141

166142
export type VoteMutation = {
167-
__typename?: 'Mutation';
168-
<<<<<<< HEAD
169-
vote?: {
170-
__typename?: 'Entry';
171-
score: number;
172-
id: number;
173-
vote: { __typename?: 'Vote'; vote_value: number };
174-
} | 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))
143+
vote: { score: number; id: number; vote: { vote_value: number } } | null;
178144
};
179145

180146
export const CommentsPageCommentFragmentDoc = {

dev-test/githunt/types.avoidOptionals.ts

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
77
}>;
88

99
export type OnCommentAddedSubscription = {
10-
__typename?: 'Subscription';
1110
commentAdded: {
12-
__typename?: 'Comment';
1311
id: number;
1412
createdAt: number;
1513
content: string;
16-
postedBy: { __typename?: 'User'; login: string; html_url: string };
14+
postedBy: { login: string; html_url: string };
1715
} | null;
1816
};
1917

@@ -24,23 +22,19 @@ export type CommentQueryVariables = Exact<{
2422
}>;
2523

2624
export type CommentQuery = {
27-
__typename?: 'Query';
28-
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
25+
currentUser: { login: string; html_url: string } | null;
2926
entry: {
30-
__typename?: 'Entry';
3127
id: number;
3228
createdAt: number;
3329
commentCount: number;
34-
postedBy: { __typename?: 'User'; login: string; html_url: string };
30+
postedBy: { login: string; html_url: string };
3531
comments: Array<{
36-
__typename?: 'Comment';
3732
id: number;
3833
createdAt: number;
3934
content: string;
40-
postedBy: { __typename?: 'User'; login: string; html_url: string };
35+
postedBy: { login: string; html_url: string };
4136
} | null>;
4237
repository: {
43-
__typename?: 'Repository';
4438
description: string | null;
4539
open_issues_count: number | null;
4640
stargazers_count: number;
@@ -51,37 +45,33 @@ export type CommentQuery = {
5145
};
5246

5347
export type CommentsPageCommentFragment = {
54-
__typename?: 'Comment';
5548
id: number;
5649
createdAt: number;
5750
content: string;
58-
postedBy: { __typename?: 'User'; login: string; html_url: string };
51+
postedBy: { login: string; html_url: string };
5952
};
6053

6154
export type CurrentUserForProfileQueryVariables = Exact<{ [key: string]: never }>;
6255

6356
export type CurrentUserForProfileQuery = {
64-
__typename?: 'Query';
65-
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
57+
currentUser: { login: string; avatar_url: string } | null;
6658
};
6759

6860
export type FeedEntryFragment = {
69-
__typename?: 'Entry';
7061
id: number;
7162
commentCount: number;
7263
score: number;
7364
createdAt: number;
7465
repository: {
75-
__typename?: 'Repository';
7666
full_name: string;
7767
html_url: string;
7868
description: string | null;
7969
stargazers_count: number;
8070
open_issues_count: number | null;
81-
owner: { __typename?: 'User'; avatar_url: string } | null;
71+
owner: { avatar_url: string } | null;
8272
};
83-
vote: { __typename?: 'Vote'; vote_value: number };
84-
postedBy: { __typename?: 'User'; html_url: string; login: string };
73+
vote: { vote_value: number };
74+
postedBy: { html_url: string; login: string };
8575
};
8676

8777
export type FeedQueryVariables = Exact<{
@@ -91,47 +81,39 @@ export type FeedQueryVariables = Exact<{
9181
}>;
9282

9383
export type FeedQuery = {
94-
__typename?: 'Query';
95-
currentUser: { __typename?: 'User'; login: string } | null;
84+
currentUser: { login: string } | null;
9685
feed: Array<{
97-
__typename?: 'Entry';
9886
id: number;
9987
commentCount: number;
10088
score: number;
10189
createdAt: number;
10290
repository: {
103-
__typename?: 'Repository';
10491
full_name: string;
10592
html_url: string;
10693
description: string | null;
10794
stargazers_count: number;
10895
open_issues_count: number | null;
109-
owner: { __typename?: 'User'; avatar_url: string } | null;
96+
owner: { avatar_url: string } | null;
11097
};
111-
vote: { __typename?: 'Vote'; vote_value: number };
112-
postedBy: { __typename?: 'User'; html_url: string; login: string };
98+
vote: { vote_value: number };
99+
postedBy: { html_url: string; login: string };
113100
} | null> | null;
114101
};
115102

116103
export type SubmitRepositoryMutationVariables = Exact<{
117104
repoFullName: string;
118105
}>;
119106

120-
export type SubmitRepositoryMutation = {
121-
__typename?: 'Mutation';
122-
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
123-
};
107+
export type SubmitRepositoryMutation = { submitRepository: { createdAt: number } | null };
124108

125109
export type RepoInfoFragment = {
126-
__typename?: 'Entry';
127110
createdAt: number;
128111
repository: {
129-
__typename?: 'Repository';
130112
description: string | null;
131113
stargazers_count: number;
132114
open_issues_count: number | null;
133115
};
134-
postedBy: { __typename?: 'User'; html_url: string; login: string };
116+
postedBy: { html_url: string; login: string };
135117
};
136118

137119
export type SubmitCommentMutationVariables = Exact<{
@@ -140,33 +122,21 @@ export type SubmitCommentMutationVariables = Exact<{
140122
}>;
141123

142124
export type SubmitCommentMutation = {
143-
__typename?: 'Mutation';
144125
submitComment: {
145-
__typename?: 'Comment';
146126
id: number;
147127
createdAt: number;
148128
content: string;
149-
postedBy: { __typename?: 'User'; login: string; html_url: string };
129+
postedBy: { login: string; html_url: string };
150130
} | null;
151131
};
152132

153-
export type VoteButtonsFragment = {
154-
__typename?: 'Entry';
155-
score: number;
156-
vote: { __typename?: 'Vote'; vote_value: number };
157-
};
133+
export type VoteButtonsFragment = { score: number; vote: { vote_value: number } };
158134

159135
export type VoteMutationVariables = Exact<{
160136
repoFullName: string;
161137
type: VoteType;
162138
}>;
163139

164140
export type VoteMutation = {
165-
__typename?: 'Mutation';
166-
vote: {
167-
__typename?: 'Entry';
168-
score: number;
169-
id: number;
170-
vote: { __typename?: 'Vote'; vote_value: number };
171-
} | null;
141+
vote: { score: number; id: number; vote: { vote_value: number } } | null;
172142
};

0 commit comments

Comments
 (0)