Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-buckets-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-operations': patch
---

Fix importing issue of Input when importSchemaTypesFrom is used
17 changes: 16 additions & 1 deletion dev-test/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const config: CodegenConfig = {
},
},

// standalone-operations
// standalone-operations/import-schema-types
'./dev-test/standalone-operations/import-schema-types/_base.generated.ts': {
schema: './dev-test/standalone-operations/schema.graphql',
documents: ['./dev-test/standalone-operations/import-schema-types/*.graphql'],
Expand All @@ -274,6 +274,21 @@ const config: CodegenConfig = {
namespacedImportName: 'Types',
},
},

// standalone-operations/with-typescript-plugin
'./dev-test/standalone-operations/with-typescript-plugin/_base.generated.ts': {
schema: './dev-test/standalone-operations/schema.graphql',
plugins: ['typescript'],
},
'./dev-test/standalone-operations/with-typescript-plugin/_types.generated.ts': {
schema: './dev-test/standalone-operations/schema.graphql',
documents: ['./dev-test/standalone-operations/with-typescript-plugin/*.graphql'],
plugins: ['typescript-operations'],
config: {
importSchemaTypesFrom: './dev-test/standalone-operations/with-typescript-plugin/_base.generated.ts',
namespacedImportName: 'Types',
},
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ export type UserRole =
| 'ADMIN'
/** UserRole CUSTOMER */
| 'CUSTOMER';

export type UsersInput = {
name?: string | null | undefined;
nestedInput?: UsersInput | null | undefined;
role?: UserRole | null | undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ export type WithVariablesQueryVariables = Exact<{
}>;

export type WithVariablesQuery = { user: { id: string; name: string } | null };

export type UsersQueryVariables = Exact<{
input: Types.UsersInput;
}>;

export type UsersQuery = { users: Array<{ id: string }> };
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ query WithVariables($role: UserRole) {
name
}
}

query Users($input: UsersInput!) {
users(input: $input) {
id
}
}
12 changes: 12 additions & 0 deletions dev-test/standalone-operations/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type Query {
user(id: ID!, role: UserRole): User
users(input: UsersInput!): [User!]!
}

type User {
Expand All @@ -15,3 +16,14 @@ enum UserRole {
"UserRole CUSTOMER"
CUSTOMER
}

enum UserStatus {
ACTIVE
INACTIVE
}

input UsersInput {
name: String
role: UserRole
nestedInput: UsersInput
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: { input: string; output: string };
String: { input: string; output: string };
Boolean: { input: boolean; output: boolean };
Int: { input: number; output: number };
Float: { input: number; output: number };
};

export type Query = {
__typename?: 'Query';
user?: Maybe<User>;
users: Array<User>;
};

export type QueryUserArgs = {
id: Scalars['ID']['input'];
role?: InputMaybe<UserRole>;
};

export type QueryUsersArgs = {
input: UsersInput;
};

export type User = {
__typename?: 'User';
id: Scalars['ID']['output'];
name: Scalars['String']['output'];
role: UserRole;
};

/** UserRole Description */
export enum UserRole {
/** UserRole ADMIN */
Admin = 'ADMIN',
/** UserRole CUSTOMER */
Customer = 'CUSTOMER',
}

export enum UserStatus {
Active = 'ACTIVE',
Inactive = 'INACTIVE',
}

export type UsersInput = {
name?: InputMaybe<Scalars['String']['input']>;
nestedInput?: InputMaybe<UsersInput>;
role?: InputMaybe<UserRole>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type * as Types from './_base.generated.js';

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 };
export type WithVariablesQueryVariables = Exact<{
role?: Types.UserRole | null | undefined;
}>;

export type WithVariablesQuery = { user: { id: string; name: string } | null };

export type UsersQueryVariables = Exact<{
input: Types.UsersInput;
}>;

export type UsersQuery = { users: Array<{ id: string }> };
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query WithVariables($role: UserRole) {
user(id: "100") {
id
name
}
}

query Users($input: UsersInput!) {
users(input: $input) {
id
}
}
20 changes: 16 additions & 4 deletions packages/plugins/typescript/operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<

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

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

InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string | null {
const inputTypeName = node.name.value;
if (!this._usedNamedInputTypes[inputTypeName]) {
return null;
if (
!this._usedNamedInputTypes[inputTypeName] || // If not used...
this.config.importSchemaTypesFrom // ... Or, is imported from a shared file
) {
return null; // ... then, don't generate in this file
}

// Note: we usually don't need to export this type,
// however, it's not possible to know if another file is using this type e.g. using `importSchemaTypesFrom`,
// so it's better export the types.

if (isOneOfInputObjectType(this._schema.getType(inputTypeName))) {
return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node))
.withComment(node.description?.value)
.withContent(`\n` + (node.fields || []).join('\n |')).string;
}

return new DeclarationBlock(this._declarationBlockConfig)
.export()
.asKind('type')
.withName(this.convertName(node))
.withComment(node.description?.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2714,7 +2714,7 @@ export type Q2Query = { search: Array<

expect(content).toMatchInlineSnapshot(
`
"type InputType = {
"export type InputType = {
t?: string | null | undefined;
};

Expand Down Expand Up @@ -2979,7 +2979,7 @@ export type Q2Query = { search: Array<
| 'Write'
| 'All';

type PREFIX_Filter = {
export type PREFIX_Filter = {
match: string;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('TypeScript Operations Plugin - Default Scalar types', () => {
expect(result).toMatchInlineSnapshot(`
"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 };
type UserInput = {
export type UserInput = {
nonNullableDate: unknown;
nullableDate?: unknown;
dateArray1?: Array<unknown> | null | undefined;
Expand Down Expand Up @@ -161,7 +161,7 @@ describe('TypeScript Operations Plugin - Default Scalar types', () => {
expect(result).toMatchInlineSnapshot(`
"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 };
type UserInput = {
export type UserInput = {
nonNullableDate: any;
nullableDate?: any;
dateArray1?: Array<any> | null | undefined;
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('TypeScript Operations Plugin - Default Scalar types', () => {
expect(result).toMatchInlineSnapshot(`
"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 };
type UserInput = {
export type UserInput = {
nonNullableDate: Date;
nullableDate?: Date | null | undefined;
dateArray1?: Array<Date | null | undefined> | null | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ describe('TypeScript Operations Plugin - Import Types', () => {

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 };
/** UsersInput Description */
type UsersInput = {
/** UsersInput from */
from?: unknown;
/** UsersInput to */
to?: unknown;
role?: UserRole | null | undefined;
};

export type UserQueryVariables = Exact<{
id: string;
}>;
Expand Down Expand Up @@ -251,15 +242,6 @@ describe('TypeScript Operations Plugin - Import Types', () => {

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 };
/** UsersInput Description */
type UsersInput = {
/** UsersInput from */
from?: unknown;
/** UsersInput to */
to?: unknown;
role?: UserRole | null | undefined;
};

export type UserQueryVariables = Exact<{
id: string;
}>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('TypeScript Operations Plugin - Input', () => {
| 'CUSTOMER';

/** UsersInput Description */
type UsersInput = {
export type UsersInput = {
/** UsersInput from */
from?: Date | null | undefined;
/** UsersInput to */
Expand All @@ -112,7 +112,7 @@ describe('TypeScript Operations Plugin - Input', () => {
nestedInput?: UsersInput | null | undefined;
};

type UsersBestFriendInput = {
export type UsersBestFriendInput = {
name?: string | null | undefined;
};

Expand Down Expand Up @@ -214,7 +214,7 @@ describe('TypeScript Operations Plugin - Input', () => {
| 'CUSTOMER';

/** UsersInput Description */
type UsersInput = {
export type UsersInput = {
/** UsersInput from */
readonly from?: Date | null | undefined;
/** UsersInput to */
Expand All @@ -229,7 +229,7 @@ describe('TypeScript Operations Plugin - Input', () => {
readonly nestedInput?: UsersInput | null | undefined;
};

type UsersBestFriendInput = {
export type UsersBestFriendInput = {
readonly name?: string | null | undefined;
};

Expand Down Expand Up @@ -323,7 +323,7 @@ describe('TypeScript Operations Plugin - Input', () => {
| 'CUSTOMER';

/** UsersInput Description */
type UsersInput =
export type UsersInput =
{ /** UsersInput from */
from: Date; to?: never; timezone?: never; role?: never; ageRange1?: never; ageRange3?: never; bestFriend?: never; nestedInput?: never; }
| { from?: never; /** UsersInput to */
Expand All @@ -335,7 +335,7 @@ describe('TypeScript Operations Plugin - Input', () => {
| { from?: never; to?: never; timezone?: never; role?: never; ageRange1?: never; ageRange3?: never; bestFriend: UsersBestFriendInput; nestedInput?: never; }
| { from?: never; to?: never; timezone?: never; role?: never; ageRange1?: never; ageRange3?: never; bestFriend?: never; nestedInput: UsersInput; };

type UsersBestFriendInput = {
export type UsersBestFriendInput = {
name?: string | null | undefined;
};

Expand Down Expand Up @@ -414,7 +414,7 @@ describe('TypeScript Operations Plugin - Input', () => {
expect(result).toMatchInlineSnapshot(`
"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 };
type UserInput = {
export type UserInput = {
dateRange1?: Array<Date | null> | null;
dateRange2: Array<Date | null>;
dateRange3?: Array<Date> | null;
Expand All @@ -423,7 +423,7 @@ describe('TypeScript Operations Plugin - Input', () => {
nestedInput?: UserInput | null;
};

type UserBestFriendInput = {
export type UserBestFriendInput = {
name?: string | null;
bestFriendDateRange1?: Array<Date | null> | null;
bestFriendDateRange2: Array<Date | null>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('TypeScript Operations Plugin - Standalone', () => {
| 'CUSTOMER';

/** UsersInput Description */
type UsersInput = {
export type UsersInput = {
/** UsersInput from */
from?: unknown;
/** UsersInput to */
Expand Down Expand Up @@ -251,11 +251,11 @@ describe('TypeScript Operations Plugin - Standalone', () => {
| 'ENUM_E'
| 'ENUM_F';

type EnumsInner = {
export type EnumsInner = {
enumsDeep: Array<EnumInnerArray>;
};

type UsersInput = {
export type UsersInput = {
enum: EnumRoot;
enums: Array<EnumRootArray>;
innerEnums: EnumsInner;
Expand Down Expand Up @@ -652,7 +652,7 @@ describe('TypeScript Operations Plugin - Standalone', () => {
| 'CUSTOMER';

/** UsersInput Description */
type UsersInput = {
export type UsersInput = {
/** UsersInput from */
from?: unknown;
/** UsersInput to */
Expand Down