Skip to content

Commit 8fd4558

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 e17d768 commit 8fd4558

File tree

58 files changed

+2340
-3191
lines changed

Some content is hidden

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

58 files changed

+2340
-3191
lines changed

.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/githunt/typed-document-nodes.ts

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
66
}>;
77

88
export type OnCommentAddedSubscription = {
9-
__typename?: 'Subscription';
109
commentAdded: {
11-
__typename?: 'Comment';
1210
id: number;
1311
createdAt: number;
1412
content: string;
15-
postedBy: { __typename?: 'User'; login: string; html_url: string };
13+
postedBy: { login: string; html_url: string };
1614
} | null;
1715
};
1816

@@ -23,23 +21,19 @@ export type CommentQueryVariables = Exact<{
2321
}>;
2422

2523
export type CommentQuery = {
26-
__typename?: 'Query';
27-
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
24+
currentUser: { login: string; html_url: string } | null;
2825
entry: {
29-
__typename?: 'Entry';
3026
id: number;
3127
createdAt: number;
3228
commentCount: number;
33-
postedBy: { __typename?: 'User'; login: string; html_url: string };
29+
postedBy: { login: string; html_url: string };
3430
comments: Array<{
35-
__typename?: 'Comment';
3631
id: number;
3732
createdAt: number;
3833
content: string;
39-
postedBy: { __typename?: 'User'; login: string; html_url: string };
34+
postedBy: { login: string; html_url: string };
4035
} | null>;
4136
repository: {
42-
__typename?: 'Repository';
4337
description: string | null;
4438
open_issues_count: number | null;
4539
stargazers_count: number;
@@ -50,37 +44,31 @@ export type CommentQuery = {
5044
};
5145

5246
export type CommentsPageCommentFragment = {
53-
__typename?: 'Comment';
5447
id: number;
5548
createdAt: number;
5649
content: string;
57-
postedBy: { __typename?: 'User'; login: string; html_url: string };
50+
postedBy: { login: string; html_url: string };
5851
};
5952

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

62-
export type CurrentUserForProfileQuery = {
63-
__typename?: 'Query';
64-
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
65-
};
55+
export type CurrentUserForProfileQuery = { currentUser: { login: string; avatar_url: string } | null };
6656

6757
export type FeedEntryFragment = {
68-
__typename?: 'Entry';
6958
id: number;
7059
commentCount: number;
7160
score: number;
7261
createdAt: number;
7362
repository: {
74-
__typename?: 'Repository';
7563
full_name: string;
7664
html_url: string;
7765
description: string | null;
7866
stargazers_count: number;
7967
open_issues_count: number | null;
80-
owner: { __typename?: 'User'; avatar_url: string } | null;
68+
owner: { avatar_url: string } | null;
8169
};
82-
vote: { __typename?: 'Vote'; vote_value: number };
83-
postedBy: { __typename?: 'User'; html_url: string; login: string };
70+
vote: { vote_value: number };
71+
postedBy: { html_url: string; login: string };
8472
};
8573

8674
export type FeedQueryVariables = Exact<{
@@ -90,47 +78,35 @@ export type FeedQueryVariables = Exact<{
9078
}>;
9179

9280
export type FeedQuery = {
93-
__typename?: 'Query';
94-
currentUser: { __typename?: 'User'; login: string } | null;
81+
currentUser: { login: string } | null;
9582
feed: Array<{
96-
__typename?: 'Entry';
9783
id: number;
9884
commentCount: number;
9985
score: number;
10086
createdAt: number;
10187
repository: {
102-
__typename?: 'Repository';
10388
full_name: string;
10489
html_url: string;
10590
description: string | null;
10691
stargazers_count: number;
10792
open_issues_count: number | null;
108-
owner: { __typename?: 'User'; avatar_url: string } | null;
93+
owner: { avatar_url: string } | null;
10994
};
110-
vote: { __typename?: 'Vote'; vote_value: number };
111-
postedBy: { __typename?: 'User'; html_url: string; login: string };
95+
vote: { vote_value: number };
96+
postedBy: { html_url: string; login: string };
11297
} | null> | null;
11398
};
11499

115100
export type SubmitRepositoryMutationVariables = Exact<{
116101
repoFullName: string;
117102
}>;
118103

119-
export type SubmitRepositoryMutation = {
120-
__typename?: 'Mutation';
121-
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
122-
};
104+
export type SubmitRepositoryMutation = { submitRepository: { createdAt: number } | null };
123105

124106
export type RepoInfoFragment = {
125-
__typename?: 'Entry';
126107
createdAt: number;
127-
repository: {
128-
__typename?: 'Repository';
129-
description: string | null;
130-
stargazers_count: number;
131-
open_issues_count: number | null;
132-
};
133-
postedBy: { __typename?: 'User'; html_url: string; login: string };
108+
repository: { description: string | null; stargazers_count: number; open_issues_count: number | null };
109+
postedBy: { html_url: string; login: string };
134110
};
135111

136112
export type SubmitCommentMutationVariables = Exact<{
@@ -139,31 +115,22 @@ export type SubmitCommentMutationVariables = Exact<{
139115
}>;
140116

141117
export type SubmitCommentMutation = {
142-
__typename?: 'Mutation';
143118
submitComment: {
144-
__typename?: 'Comment';
145119
id: number;
146120
createdAt: number;
147121
content: string;
148-
postedBy: { __typename?: 'User'; login: string; html_url: string };
122+
postedBy: { login: string; html_url: string };
149123
} | null;
150124
};
151125

152-
export type VoteButtonsFragment = {
153-
__typename?: 'Entry';
154-
score: number;
155-
vote: { __typename?: 'Vote'; vote_value: number };
156-
};
126+
export type VoteButtonsFragment = { score: number; vote: { vote_value: number } };
157127

158128
export type VoteMutationVariables = Exact<{
159129
repoFullName: string;
160130
type: VoteType;
161131
}>;
162132

163-
export type VoteMutation = {
164-
__typename?: 'Mutation';
165-
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
166-
};
133+
export type VoteMutation = { vote: { score: number; id: number; vote: { vote_value: number } } | null };
167134

168135
export const CommentsPageCommentFragmentDoc = {
169136
kind: 'Document',

dev-test/githunt/types.avoidOptionals.ts

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ export type OnCommentAddedSubscriptionVariables = Exact<{
55
}>;
66

77
export type OnCommentAddedSubscription = {
8-
__typename?: 'Subscription';
98
commentAdded: {
10-
__typename?: 'Comment';
119
id: number;
1210
createdAt: number;
1311
content: string;
14-
postedBy: { __typename?: 'User'; login: string; html_url: string };
12+
postedBy: { login: string; html_url: string };
1513
} | null;
1614
};
1715

@@ -22,23 +20,19 @@ export type CommentQueryVariables = Exact<{
2220
}>;
2321

2422
export type CommentQuery = {
25-
__typename?: 'Query';
26-
currentUser: { __typename?: 'User'; login: string; html_url: string } | null;
23+
currentUser: { login: string; html_url: string } | null;
2724
entry: {
28-
__typename?: 'Entry';
2925
id: number;
3026
createdAt: number;
3127
commentCount: number;
32-
postedBy: { __typename?: 'User'; login: string; html_url: string };
28+
postedBy: { login: string; html_url: string };
3329
comments: Array<{
34-
__typename?: 'Comment';
3530
id: number;
3631
createdAt: number;
3732
content: string;
38-
postedBy: { __typename?: 'User'; login: string; html_url: string };
33+
postedBy: { login: string; html_url: string };
3934
} | null>;
4035
repository: {
41-
__typename?: 'Repository';
4236
description: string | null;
4337
open_issues_count: number | null;
4438
stargazers_count: number;
@@ -49,37 +43,31 @@ export type CommentQuery = {
4943
};
5044

5145
export type CommentsPageCommentFragment = {
52-
__typename?: 'Comment';
5346
id: number;
5447
createdAt: number;
5548
content: string;
56-
postedBy: { __typename?: 'User'; login: string; html_url: string };
49+
postedBy: { login: string; html_url: string };
5750
};
5851

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

61-
export type CurrentUserForProfileQuery = {
62-
__typename?: 'Query';
63-
currentUser: { __typename?: 'User'; login: string; avatar_url: string } | null;
64-
};
54+
export type CurrentUserForProfileQuery = { currentUser: { login: string; avatar_url: string } | null };
6555

6656
export type FeedEntryFragment = {
67-
__typename?: 'Entry';
6857
id: number;
6958
commentCount: number;
7059
score: number;
7160
createdAt: number;
7261
repository: {
73-
__typename?: 'Repository';
7462
full_name: string;
7563
html_url: string;
7664
description: string | null;
7765
stargazers_count: number;
7866
open_issues_count: number | null;
79-
owner: { __typename?: 'User'; avatar_url: string } | null;
67+
owner: { avatar_url: string } | null;
8068
};
81-
vote: { __typename?: 'Vote'; vote_value: number };
82-
postedBy: { __typename?: 'User'; html_url: string; login: string };
69+
vote: { vote_value: number };
70+
postedBy: { html_url: string; login: string };
8371
};
8472

8573
export type FeedQueryVariables = Exact<{
@@ -89,47 +77,35 @@ export type FeedQueryVariables = Exact<{
8977
}>;
9078

9179
export type FeedQuery = {
92-
__typename?: 'Query';
93-
currentUser: { __typename?: 'User'; login: string } | null;
80+
currentUser: { login: string } | null;
9481
feed: Array<{
95-
__typename?: 'Entry';
9682
id: number;
9783
commentCount: number;
9884
score: number;
9985
createdAt: number;
10086
repository: {
101-
__typename?: 'Repository';
10287
full_name: string;
10388
html_url: string;
10489
description: string | null;
10590
stargazers_count: number;
10691
open_issues_count: number | null;
107-
owner: { __typename?: 'User'; avatar_url: string } | null;
92+
owner: { avatar_url: string } | null;
10893
};
109-
vote: { __typename?: 'Vote'; vote_value: number };
110-
postedBy: { __typename?: 'User'; html_url: string; login: string };
94+
vote: { vote_value: number };
95+
postedBy: { html_url: string; login: string };
11196
} | null> | null;
11297
};
11398

11499
export type SubmitRepositoryMutationVariables = Exact<{
115100
repoFullName: string;
116101
}>;
117102

118-
export type SubmitRepositoryMutation = {
119-
__typename?: 'Mutation';
120-
submitRepository: { __typename?: 'Entry'; createdAt: number } | null;
121-
};
103+
export type SubmitRepositoryMutation = { submitRepository: { createdAt: number } | null };
122104

123105
export type RepoInfoFragment = {
124-
__typename?: 'Entry';
125106
createdAt: number;
126-
repository: {
127-
__typename?: 'Repository';
128-
description: string | null;
129-
stargazers_count: number;
130-
open_issues_count: number | null;
131-
};
132-
postedBy: { __typename?: 'User'; html_url: string; login: string };
107+
repository: { description: string | null; stargazers_count: number; open_issues_count: number | null };
108+
postedBy: { html_url: string; login: string };
133109
};
134110

135111
export type SubmitCommentMutationVariables = Exact<{
@@ -138,28 +114,19 @@ export type SubmitCommentMutationVariables = Exact<{
138114
}>;
139115

140116
export type SubmitCommentMutation = {
141-
__typename?: 'Mutation';
142117
submitComment: {
143-
__typename?: 'Comment';
144118
id: number;
145119
createdAt: number;
146120
content: string;
147-
postedBy: { __typename?: 'User'; login: string; html_url: string };
121+
postedBy: { login: string; html_url: string };
148122
} | null;
149123
};
150124

151-
export type VoteButtonsFragment = {
152-
__typename?: 'Entry';
153-
score: number;
154-
vote: { __typename?: 'Vote'; vote_value: number };
155-
};
125+
export type VoteButtonsFragment = { score: number; vote: { vote_value: number } };
156126

157127
export type VoteMutationVariables = Exact<{
158128
repoFullName: string;
159129
type: VoteType;
160130
}>;
161131

162-
export type VoteMutation = {
163-
__typename?: 'Mutation';
164-
vote: { __typename?: 'Entry'; score: number; id: number; vote: { __typename?: 'Vote'; vote_value: number } } | null;
165-
};
132+
export type VoteMutation = { vote: { score: number; id: number; vote: { vote_value: number } } | null };

0 commit comments

Comments
 (0)