Skip to content

Commit a9178c1

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 5a8a18a commit a9178c1

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
@@ -273,7 +273,7 @@ const config: CodegenConfig = {
273273
},
274274
// #endregion
275275

276-
// standalone-operations
276+
// standalone-operations/import-schema-types
277277
'./dev-test/standalone-operations/import-schema-types/_base.generated.ts': {
278278
schema: './dev-test/standalone-operations/schema.graphql',
279279
documents: ['./dev-test/standalone-operations/import-schema-types/*.graphql'],
@@ -292,6 +292,21 @@ const config: CodegenConfig = {
292292
namespacedImportName: 'Types',
293293
},
294294
},
295+
296+
// standalone-operations/with-typescript-plugin
297+
'./dev-test/standalone-operations/with-typescript-plugin/_base.generated.ts': {
298+
schema: './dev-test/standalone-operations/schema.graphql',
299+
plugins: ['typescript'],
300+
},
301+
'./dev-test/standalone-operations/with-typescript-plugin/_types.generated.ts': {
302+
schema: './dev-test/standalone-operations/schema.graphql',
303+
documents: ['./dev-test/standalone-operations/with-typescript-plugin/*.graphql'],
304+
plugins: ['typescript-operations'],
305+
config: {
306+
importSchemaTypesFrom: './dev-test/standalone-operations/with-typescript-plugin/_base.generated.ts',
307+
namespacedImportName: 'Types',
308+
},
309+
},
295310
},
296311
};
297312

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
@@ -9,3 +9,9 @@ export type WithVariablesQueryVariables = Exact<{
99
}>;
1010

1111
export type WithVariablesQuery = { user: { id: string; name: string } | null };
12+
13+
export type UsersQueryVariables = Exact<{
14+
input: Types.UsersInput;
15+
}>;
16+
17+
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
@@ -198,8 +198,11 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
198198

199199
EnumTypeDefinition(node: EnumTypeDefinitionNode): string | null {
200200
const enumName = node.name.value;
201-
if (!this._usedNamedInputTypes[enumName] || this.config.importSchemaTypesFrom) {
202-
return null;
201+
if (
202+
!this._usedNamedInputTypes[enumName] || // If not used...
203+
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
204+
) {
205+
return null; // ... then, don't generate in this file
203206
}
204207

205208
return convertSchemaEnumToDeclarationBlockString({
@@ -223,19 +226,28 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
223226

224227
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string | null {
225228
const inputTypeName = node.name.value;
226-
if (!this._usedNamedInputTypes[inputTypeName]) {
227-
return null;
229+
if (
230+
!this._usedNamedInputTypes[inputTypeName] || // If not used...
231+
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
232+
) {
233+
return null; // ... then, don't generate in this file
228234
}
229235

236+
// Note: we usually don't need to export this type,
237+
// however, it's not possible to know if another file is using this type e.g. using `importSchemaTypesFrom`,
238+
// so it's better export the types.
239+
230240
if (isOneOfInputObjectType(this._schema.getType(inputTypeName))) {
231241
return new DeclarationBlock(this._declarationBlockConfig)
242+
.export()
232243
.asKind('type')
233244
.withName(this.convertName(node))
234245
.withComment(node.description?.value)
235246
.withContent(`\n` + (node.fields || []).join('\n |')).string;
236247
}
237248

238249
return new DeclarationBlock(this._declarationBlockConfig)
250+
.export()
239251
.asKind('type')
240252
.withName(this.convertName(node))
241253
.withComment(node.description?.value)

0 commit comments

Comments
 (0)