Skip to content

Commit 97570c2

Browse files
committed
[typescript-operations] Fix Input generation when using importSchemaTypesFrom (#10575)
* Fix importing issue when using importSchemaTypesFrom * Add dev-tests for usage with typescript plugin * Update unit test * Add changeset
1 parent c31b126 commit 97570c2

15 files changed

Lines changed: 162 additions & 40 deletions

.changeset/twenty-buckets-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-operations': patch
3+
---
4+
5+
Fix importing issue of Input when importSchemaTypesFrom is used

dev-test/codegen.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ const config: CodegenConfig = {
256256
},
257257
},
258258

259-
// standalone-operations
259+
// standalone-operations/import-schema-types
260260
'./dev-test/standalone-operations/import-schema-types/_base.generated.ts': {
261261
schema: './dev-test/standalone-operations/schema.graphql',
262262
documents: ['./dev-test/standalone-operations/import-schema-types/*.graphql'],
@@ -274,6 +274,21 @@ const config: CodegenConfig = {
274274
namespacedImportName: 'Types',
275275
},
276276
},
277+
278+
// standalone-operations/with-typescript-plugin
279+
'./dev-test/standalone-operations/with-typescript-plugin/_base.generated.ts': {
280+
schema: './dev-test/standalone-operations/schema.graphql',
281+
plugins: ['typescript'],
282+
},
283+
'./dev-test/standalone-operations/with-typescript-plugin/_types.generated.ts': {
284+
schema: './dev-test/standalone-operations/schema.graphql',
285+
documents: ['./dev-test/standalone-operations/with-typescript-plugin/*.graphql'],
286+
plugins: ['typescript-operations'],
287+
config: {
288+
importSchemaTypesFrom: './dev-test/standalone-operations/with-typescript-plugin/_base.generated.ts',
289+
namespacedImportName: 'Types',
290+
},
291+
},
277292
},
278293
};
279294

dev-test/standalone-operations/import-schema-types/_base.generated.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ export type UserRole =
44
| 'ADMIN'
55
/** UserRole CUSTOMER */
66
| 'CUSTOMER';
7+
8+
export type UsersInput = {
9+
name?: string | null | undefined;
10+
nestedInput?: UsersInput | null | undefined;
11+
role?: UserRole | null | undefined;
12+
};

dev-test/standalone-operations/import-schema-types/_types.generated.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ export type WithVariablesQueryVariables = Exact<{
77
}>;
88

99
export type WithVariablesQuery = { user: { id: string; name: string } | null };
10+
11+
export type UsersQueryVariables = Exact<{
12+
input: Types.UsersInput;
13+
}>;
14+
15+
export type UsersQuery = { users: Array<{ id: string }> };

dev-test/standalone-operations/import-schema-types/query.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ query WithVariables($role: UserRole) {
44
name
55
}
66
}
7+
8+
query Users($input: UsersInput!) {
9+
users(input: $input) {
10+
id
11+
}
12+
}

dev-test/standalone-operations/schema.graphql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
type Query {
22
user(id: ID!, role: UserRole): User
3+
users(input: UsersInput!): [User!]!
34
}
45

56
type User {
@@ -15,3 +16,14 @@ enum UserRole {
1516
"UserRole CUSTOMER"
1617
CUSTOMER
1718
}
19+
20+
enum UserStatus {
21+
ACTIVE
22+
INACTIVE
23+
}
24+
25+
input UsersInput {
26+
name: String
27+
role: UserRole
28+
nestedInput: UsersInput
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export type Maybe<T> = T | null;
2+
export type InputMaybe<T> = Maybe<T>;
3+
/** All built-in and custom scalars, mapped to their actual values */
4+
export type Scalars = {
5+
ID: { input: string; output: string };
6+
String: { input: string; output: string };
7+
Boolean: { input: boolean; output: boolean };
8+
Int: { input: number; output: number };
9+
Float: { input: number; output: number };
10+
};
11+
12+
export type Query = {
13+
__typename?: 'Query';
14+
user?: Maybe<User>;
15+
users: Array<User>;
16+
};
17+
18+
export type QueryUserArgs = {
19+
id: Scalars['ID']['input'];
20+
role?: InputMaybe<UserRole>;
21+
};
22+
23+
export type QueryUsersArgs = {
24+
input: UsersInput;
25+
};
26+
27+
export type User = {
28+
__typename?: 'User';
29+
id: Scalars['ID']['output'];
30+
name: Scalars['String']['output'];
31+
role: UserRole;
32+
};
33+
34+
/** UserRole Description */
35+
export enum UserRole {
36+
/** UserRole ADMIN */
37+
Admin = 'ADMIN',
38+
/** UserRole CUSTOMER */
39+
Customer = 'CUSTOMER',
40+
}
41+
42+
export enum UserStatus {
43+
Active = 'ACTIVE',
44+
Inactive = 'INACTIVE',
45+
}
46+
47+
export type UsersInput = {
48+
name?: InputMaybe<Scalars['String']['input']>;
49+
nestedInput?: InputMaybe<UsersInput>;
50+
role?: InputMaybe<UserRole>;
51+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type * as Types from './_base.generated.js';
2+
3+
type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
4+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never };
5+
export type WithVariablesQueryVariables = Exact<{
6+
role?: Types.UserRole | null | undefined;
7+
}>;
8+
9+
export type WithVariablesQuery = { user: { id: string; name: string } | null };
10+
11+
export type UsersQueryVariables = Exact<{
12+
input: Types.UsersInput;
13+
}>;
14+
15+
export type UsersQuery = { users: Array<{ id: string }> };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
query WithVariables($role: UserRole) {
2+
user(id: "100") {
3+
id
4+
name
5+
}
6+
}
7+
8+
query Users($input: UsersInput!) {
9+
users(input: $input) {
10+
id
11+
}
12+
}

packages/plugins/typescript/operations/src/visitor.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,11 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
191191

192192
EnumTypeDefinition(node: EnumTypeDefinitionNode): string | null {
193193
const enumName = node.name.value;
194-
if (!this._usedNamedInputTypes[enumName] || this.config.importSchemaTypesFrom) {
195-
return null;
194+
if (
195+
!this._usedNamedInputTypes[enumName] || // If not used...
196+
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
197+
) {
198+
return null; // ... then, don't generate in this file
196199
}
197200

198201
return convertSchemaEnumToDeclarationBlockString({
@@ -216,19 +219,28 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
216219

217220
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string | null {
218221
const inputTypeName = node.name.value;
219-
if (!this._usedNamedInputTypes[inputTypeName]) {
220-
return null;
222+
if (
223+
!this._usedNamedInputTypes[inputTypeName] || // If not used...
224+
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
225+
) {
226+
return null; // ... then, don't generate in this file
221227
}
222228

229+
// Note: we usually don't need to export this type,
230+
// however, it's not possible to know if another file is using this type e.g. using `importSchemaTypesFrom`,
231+
// so it's better export the types.
232+
223233
if (isOneOfInputObjectType(this._schema.getType(inputTypeName))) {
224234
return new DeclarationBlock(this._declarationBlockConfig)
235+
.export()
225236
.asKind('type')
226237
.withName(this.convertName(node))
227238
.withComment(node.description?.value)
228239
.withContent(`\n` + (node.fields || []).join('\n |')).string;
229240
}
230241

231242
return new DeclarationBlock(this._declarationBlockConfig)
243+
.export()
232244
.asKind('type')
233245
.withName(this.convertName(node))
234246
.withComment(node.description?.value)

0 commit comments

Comments
 (0)