-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Only generate Exact utility type if documents have operations
#10571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b5643b
Remove extraneous test setup
eddeee888 83963a9
Do not generate Exact utility type of not used
eddeee888 a1885b5
Add default test case to check that Exact is used for Mutation and Su…
eddeee888 8d9c889
Update dev-tests
eddeee888 74c30a4
Add changeset
eddeee888 42d82df
Remove extraneous config for Exact test
eddeee888 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@graphql-codegen/typescript-operations': patch | ||
| --- | ||
|
|
||
| Only generate `Exact` utility type at the top if it is used | ||
|
|
||
| `Exact` utility is only used to wrap variables types for operations (queries, mutations and subscriptions) if they exist in the document. `Exact` is never used when there are _only_ fragments. | ||
|
|
||
| This is important to conditionally generate as users may use very strict tsconfig that will fail compiling if there are unused types. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] }; | ||
| export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,14 @@ describe('TypeScript Operations Plugin - Standalone', () => { | |
| users(input: UsersInput!): UsersResponse! | ||
| } | ||
|
|
||
| type Mutation { | ||
| makeUserAdmin(id: ID!): User! | ||
| } | ||
|
|
||
| type Subscription { | ||
| userChanges(id: ID!): User! | ||
| } | ||
|
|
||
| type ResponseError { | ||
| error: ResponseErrorType! | ||
| } | ||
|
|
@@ -90,6 +98,23 @@ describe('TypeScript Operations Plugin - Standalone', () => { | |
| } | ||
| } | ||
| } | ||
|
|
||
| mutation MakeAdmin { | ||
| makeUserAdmin(id: "100") { | ||
| ...UserFragment | ||
| } | ||
| } | ||
|
|
||
| subscription UserChanges { | ||
| makeUserAdmin(id: "100") { | ||
| ...UserFragment | ||
| } | ||
| } | ||
|
|
||
| fragment UserFragment on User { | ||
| id | ||
| role | ||
| } | ||
| `); | ||
|
|
||
| const result = mergeOutputs([await plugin(schema, [{ document }], {}, { outputFile: '' })]); | ||
|
|
@@ -147,6 +172,18 @@ describe('TypeScript Operations Plugin - Standalone', () => { | |
| | { result: Array<{ __typename: 'User' }> } | ||
| | { __typename: 'ResponseError' } | ||
| }; | ||
|
|
||
| export type MakeAdminMutationVariables = Exact<{ [key: string]: never; }>; | ||
|
|
||
|
|
||
| export type MakeAdminMutation = { makeUserAdmin: { id: string, role: UserRole } }; | ||
|
|
||
| export type UserChangesSubscriptionVariables = Exact<{ [key: string]: never; }>; | ||
|
|
||
|
|
||
| export type UserChangesSubscription = Record<PropertyKey, never>; | ||
|
|
||
| export type UserFragmentFragment = { id: string, role: UserRole }; | ||
| " | ||
| `); | ||
|
|
||
|
|
@@ -712,17 +749,6 @@ describe('TypeScript Operations Plugin - Standalone', () => { | |
| user(id: ID!): User | ||
| } | ||
|
|
||
| type ResponseError { | ||
| error: ResponseErrorType! | ||
| } | ||
|
|
||
| enum ResponseErrorType { | ||
| NOT_FOUND | ||
| INPUT_VALIDATION_ERROR | ||
| FORBIDDEN_ERROR | ||
| UNEXPECTED_ERROR | ||
| } | ||
|
Comment on lines
-715
to
-724
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extraneous and never used in this test, so removed. |
||
|
|
||
| type User { | ||
| id: ID! | ||
| name: String! | ||
|
|
@@ -782,4 +808,45 @@ describe('TypeScript Operations Plugin - Standalone', () => { | |
|
|
||
| validateTs(result, undefined, undefined, undefined, undefined, true); | ||
| }); | ||
|
|
||
| it('does not generate Exact utility type if there are only fragments', async () => { | ||
| const schema = buildSchema(/* GraphQL */ ` | ||
| type Query { | ||
| user(id: ID!): User | ||
| } | ||
|
|
||
| type User { | ||
| id: ID! | ||
| name: String! | ||
| role: UserRole! | ||
| createdAt: DateTime! | ||
| bestFriend: User | ||
| goodFriends: [User!]! | ||
| } | ||
|
|
||
| enum UserRole { | ||
| ADMIN | ||
| CUSTOMER | ||
| } | ||
|
|
||
| scalar DateTime | ||
| `); | ||
| const document = parse(/* GraphQL */ ` | ||
| fragment UserPart on User { | ||
| id | ||
| name | ||
| } | ||
| `); | ||
|
|
||
| const result = mergeOutputs([await plugin(schema, [{ document }], {}, { outputFile: '' })]); | ||
|
|
||
| expect(result).toMatchInlineSnapshot(` | ||
| " | ||
| export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }; | ||
| export type UserPartFragment = { id: string, name: string }; | ||
| " | ||
| `); | ||
|
|
||
| validateTs(result, undefined, undefined, undefined, undefined, true); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding these to make sure we test many things in the default test.
This gives more confidence overall.
e.g. in this PR I want to make sure
Exactis used for mutation and subscription variables.