From 0e7e51fbc250f3e96f346caed708834ffc0a8ddf Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Sun, 28 Jul 2024 14:30:57 +0200 Subject: [PATCH] feat: reduce code output --- .changeset/cold-pants-carry.md | 5 + .changeset/silent-maps-dress.md | 6 + .../src/base-types-visitor.ts | 5 + .../typescript/typescript/src/config.ts | 4 + .../typescript/typescript/src/visitor.ts | 3 + packages/presets/client/src/index.ts | 6 +- .../client/tests/client-preset.spec.ts | 180 +----------------- 7 files changed, 29 insertions(+), 180 deletions(-) create mode 100644 .changeset/cold-pants-carry.md create mode 100644 .changeset/silent-maps-dress.md diff --git a/.changeset/cold-pants-carry.md b/.changeset/cold-pants-carry.md new file mode 100644 index 00000000000..79ea7164050 --- /dev/null +++ b/.changeset/cold-pants-carry.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/client-preset': minor +--- + +Reduce noise of generated code by only generating code relevant to GraphQL operations. diff --git a/.changeset/silent-maps-dress.md b/.changeset/silent-maps-dress.md new file mode 100644 index 00000000000..f03e56e998b --- /dev/null +++ b/.changeset/silent-maps-dress.md @@ -0,0 +1,6 @@ +--- +'@graphql-codegen/visitor-plugin-common': minor +'@graphql-codegen/typescript': minor +--- + +Add config option `onlyTypescriptOperationTypes` for only emitting types relevant for the typescript-operations plugin. diff --git a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts index 92e75e63bd5..8e22f37e06a 100644 --- a/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts +++ b/packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts @@ -60,6 +60,7 @@ export interface ParsedTypesConfig extends ParsedConfig { wrapEntireDefinitions: boolean; ignoreEnumValuesFromSchema: boolean; directiveArgumentAndInputFieldMappings: ParsedDirectiveArgumentAndInputFieldMappings; + onlyTypescriptOperationTypes: boolean; } export interface RawTypesConfig extends RawConfig { @@ -704,6 +705,7 @@ export class BaseTypesVisitor< } FieldDefinition(node: FieldDefinitionNode): string { + if (this.config.onlyTypescriptOperationTypes) return ''; if (this.config.onlyEnums) return ''; const typeString = node.type as any as string; @@ -714,6 +716,7 @@ export class BaseTypesVisitor< } UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string { + if (this.config.onlyTypescriptOperationTypes) return ''; if (this.config.onlyOperationTypes || this.config.onlyEnums) return ''; const originalNode = parent[key] as UnionTypeDefinitionNode; const possibleTypes = originalNode.types @@ -783,6 +786,7 @@ export class BaseTypesVisitor< } ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string { + if (this.config.onlyTypescriptOperationTypes) return ''; if (this.config.onlyOperationTypes || this.config.onlyEnums) return ''; const originalNode = parent[key] as ObjectTypeDefinitionNode; @@ -805,6 +809,7 @@ export class BaseTypesVisitor< } InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string { + if (this.config.onlyTypescriptOperationTypes) return ''; if (this.config.onlyOperationTypes || this.config.onlyEnums) return ''; const originalNode = parent[key] as InterfaceTypeDefinitionNode; diff --git a/packages/plugins/typescript/typescript/src/config.ts b/packages/plugins/typescript/typescript/src/config.ts index b9aed7cf961..3f5df71ad1c 100644 --- a/packages/plugins/typescript/typescript/src/config.ts +++ b/packages/plugins/typescript/typescript/src/config.ts @@ -512,4 +512,8 @@ export interface TypeScriptPluginConfig extends RawTypesConfig { * ``` */ allowEnumStringTypes?: boolean; + /** + * @description Only generate types used by typescript operations + */ + onlyTypescriptOperationTypes?: boolean; } diff --git a/packages/plugins/typescript/typescript/src/visitor.ts b/packages/plugins/typescript/typescript/src/visitor.ts index 16c8ca78bbb..29026a53b26 100644 --- a/packages/plugins/typescript/typescript/src/visitor.ts +++ b/packages/plugins/typescript/typescript/src/visitor.ts @@ -44,6 +44,7 @@ export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig { inputMaybeValue: string; noExport: boolean; useImplementingTypes: boolean; + onlyTypescriptOperationTypes: boolean; } export const EXACT_SIGNATURE = `type Exact = { [K in keyof T]: T[K] };`; @@ -77,6 +78,7 @@ export class TsVisitor< useImplementingTypes: getConfigValue(pluginConfig.useImplementingTypes, false), entireFieldWrapperValue: getConfigValue(pluginConfig.entireFieldWrapperValue, 'T'), wrapEntireDefinitions: getConfigValue(pluginConfig.wrapEntireFieldDefinitions, false), + onlyTypescriptOperationTypes: getConfigValue(pluginConfig.onlyTypescriptOperationTypes, false), ...additionalConfig, } as TParsedConfig); @@ -243,6 +245,7 @@ export class TsVisitor< } UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string { + if (this.config.onlyTypescriptOperationTypes) return ''; if (this.config.onlyOperationTypes || this.config.onlyEnums) return ''; let withFutureAddedValue: string[] = []; diff --git a/packages/presets/client/src/index.ts b/packages/presets/client/src/index.ts index 5b0804ad9fa..742fe496f80 100644 --- a/packages/presets/client/src/index.ts +++ b/packages/presets/client/src/index.ts @@ -213,7 +213,11 @@ export const preset: Types.OutputPreset = { const plugins: Array = [ { [`add`]: { content: `/* eslint-disable */` } }, - { [`typescript`]: {} }, + { + [`typescript`]: { + onlyTypescriptOperationTypes: true, + }, + }, { [`typescript-operations`]: {} }, { [`typed-document-node`]: { diff --git a/packages/presets/client/tests/client-preset.spec.ts b/packages/presets/client/tests/client-preset.spec.ts index 41b6ccf51c7..c5b74433ea0 100644 --- a/packages/presets/client/tests/client-preset.spec.ts +++ b/packages/presets/client/tests/client-preset.spec.ts @@ -355,13 +355,6 @@ export * from "./gql";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -485,13 +478,6 @@ export * from "./gql";`); Float: { input: number; output: number; } }; - export type Query = { - __typename: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -509,7 +495,7 @@ export * from "./gql";`); export const BDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"B"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"b"}}]}}]} as unknown as DocumentNode;" `); - expect(graphqlFile.content).toContain("__typename: 'Query';"); + expect(graphqlFile.content).toContain("__typename: 'Query',"); }); it('prevent duplicate operations', async () => { @@ -598,11 +584,6 @@ export * from "./gql";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1292,13 +1273,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type BbbQueryVariables = Exact<{ [key: string]: never; }>; @@ -1370,13 +1344,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1451,13 +1418,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1532,13 +1492,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1615,13 +1568,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AaaQueryVariables = Exact<{ [key: string]: never; }>; @@ -1694,13 +1640,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1779,13 +1718,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1865,13 +1797,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -1951,13 +1876,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Query = { - __typename?: 'Query'; - a?: Maybe; - b?: Maybe; - c?: Maybe; - }; - export type AQueryVariables = Exact<{ [key: string]: never; }>; @@ -2043,17 +1961,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type A = { - __typename?: 'A'; - a: A; - b: Scalars['String']['output']; - }; - - export type Query = { - __typename?: 'Query'; - a: A; - }; - export type AbFragment = ( { __typename?: 'A', b: string } & { ' $fragmentRefs'?: { 'AcFragment': AcFragment;'AaFragment': AaFragment } } @@ -2122,18 +2029,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Foo = { - __typename?: 'Foo'; - id?: Maybe; - value?: Maybe; - }; - - export type Query = { - __typename?: 'Query'; - foo?: Maybe; - foos?: Maybe>>; - }; - export type FooQueryVariables = Exact<{ [key: string]: never; }>; @@ -2207,18 +2102,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Foo = { - __typename?: 'Foo'; - id?: Maybe; - value?: Maybe; - }; - - export type Query = { - __typename?: 'Query'; - foo?: Maybe; - foos?: Maybe>>; - }; - export type FooQueryVariables = Exact<{ [key: string]: never; }>; @@ -2292,18 +2175,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Foo = { - __typename?: 'Foo'; - id?: Maybe; - value?: Maybe; - }; - - export type Query = { - __typename?: 'Query'; - foo?: Maybe; - foos?: Maybe>>; - }; - export type FooQueryVariables = Exact<{ [key: string]: never; }>; @@ -2433,18 +2304,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Foo = { - __typename?: 'Foo'; - id?: Maybe; - value?: Maybe; - }; - - export type Query = { - __typename?: 'Query'; - foo?: Maybe; - foos?: Maybe>>; - }; - export type FooQueryVariables = Exact<{ [key: string]: never; }>; @@ -2572,17 +2431,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Foo = { - __typename?: 'Foo'; - value?: Maybe; - }; - - export type Query = { - __typename?: 'Query'; - foo?: Maybe; - foos?: Maybe>>; - }; - export type FooQueryVariables = Exact<{ [key: string]: never; }>; @@ -2859,32 +2707,6 @@ export * from "./gql.js";`); Float: { input: number; output: number; } }; - export type Mutation = { - __typename?: 'Mutation'; - createRegion?: Maybe; - }; - - - export type MutationCreateRegionArgs = { - regionDescription: Scalars['String']['input']; - }; - - export type Query = { - __typename?: 'Query'; - regions?: Maybe>>; - }; - - export type Region = { - __typename?: 'Region'; - regionDescription: Scalars['String']['output']; - regionId: Scalars['Int']['output']; - }; - - export type Subscription = { - __typename?: 'Subscription'; - onRegionCreated: Region; - }; - export type OnRegionCreatedSubscriptionVariables = Exact<{ [key: string]: never; }>;