Skip to content

Commit 9a5f31c

Browse files
Borduhhcharlypoly
andauthored
New option onlyEnums for Typescript (#7718)
* New option `onlyEnums` for Typescript * added generated types * Create odd-snakes-act.md * fix: typo with only operations being removed Co-authored-by: Charly POLY <1252066+charlypoly@users.noreply.github.com>
1 parent 8f2230a commit 9a5f31c

File tree

8 files changed

+131
-8
lines changed

8 files changed

+131
-8
lines changed

‎.changeset/odd-snakes-act.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-codegen/typescript": patch
3+
"@graphql-codegen/visitor-plugin-common": patch
4+
---
5+
6+
New option `onlyEnums` for Typescript

‎dev-test/codegen.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ generates:
9898
plugins:
9999
- typescript
100100
- typescript-operations
101+
./dev-test/githunt/types.onlyEnums.ts:
102+
schema: ./dev-test/githunt/schema.json
103+
documents: ./dev-test/githunt/**/*.graphql
104+
config:
105+
onlyEnums: true
106+
plugins:
107+
- typescript
101108
./dev-test/githunt/types.preResolveTypes.onlyOperationTypes.ts:
102109
schema: ./dev-test/githunt/schema.json
103110
documents: ./dev-test/githunt/**/*.graphql
@@ -293,6 +300,13 @@ generates:
293300
plugins:
294301
- typescript
295302
- typescript-operations
303+
./dev-test/star-wars/types.OnlyEnums.ts:
304+
schema: ./dev-test/star-wars/schema.json
305+
documents: ./dev-test/star-wars/**/*.graphql
306+
config:
307+
onlyEnums: true
308+
plugins:
309+
- typescript
296310
./dev-test/star-wars/types.preResolveTypes.onlyOperationTypes.ts:
297311
schema: ./dev-test/star-wars/schema.json
298312
documents: ./dev-test/star-wars/**/*.graphql

‎dev-test/githunt/types.onlyEnums.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** A list of options for the sort order of the feed */
2+
export enum FeedType {
3+
/** Sort by a combination of freshness and score, using Reddit's algorithm */
4+
Hot = 'HOT',
5+
/** Newest entries first */
6+
New = 'NEW',
7+
/** Highest score entries first */
8+
Top = 'TOP',
9+
}
10+
11+
/** The type of vote to record, when submitting a vote */
12+
export enum VoteType {
13+
Cancel = 'CANCEL',
14+
Down = 'DOWN',
15+
Up = 'UP',
16+
}

‎dev-test/star-wars/types.OnlyEnums.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** The episodes in the Star Wars trilogy */
2+
export enum Episode {
3+
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
4+
Empire = 'EMPIRE',
5+
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
6+
Jedi = 'JEDI',
7+
/** Star Wars Episode IV: A New Hope, released in 1977. */
8+
Newhope = 'NEWHOPE',
9+
}
10+
11+
/** Units of height */
12+
export enum LengthUnit {
13+
/** Primarily used in the United States */
14+
Foot = 'FOOT',
15+
/** The standard unit around the world */
16+
Meter = 'METER',
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type TestQueryVariables = Exact<{ [key: string]: never }>;
2+
3+
export type TestQuery = {
4+
__typename?: 'Query';
5+
testArr1?: Array<string | null> | null;
6+
testArr2: Array<string | null>;
7+
testArr3: Array<string>;
8+
};

‎packages/plugins/other/visitor-plugin-common/src/base-types-visitor.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface ParsedTypesConfig extends ParsedConfig {
4949
enumValues: ParsedEnumValuesMap;
5050
declarationKind: DeclarationKindConfig;
5151
addUnderscoreToArgsType: boolean;
52+
onlyEnums: boolean;
5253
onlyOperationTypes: boolean;
5354
enumPrefix: boolean;
5455
fieldWrapperValue: string;
@@ -170,6 +171,23 @@ export interface RawTypesConfig extends RawConfig {
170171
* ```
171172
*/
172173
wrapFieldDefinitions?: boolean;
174+
/**
175+
* @description This will cause the generator to emit types for enums only
176+
* @default false
177+
*
178+
* @exampleMarkdown
179+
* ## Override all definition types
180+
*
181+
* ```yml
182+
* generates:
183+
* path/to/file.ts:
184+
* plugins:
185+
* - typescript
186+
* config:
187+
* onlyEnums: true
188+
* ```
189+
*/
190+
onlyEnums?: boolean;
173191
/**
174192
* @description This will cause the generator to emit types for operations only (basically only enums and scalars)
175193
* @default false
@@ -294,6 +312,7 @@ export class BaseTypesVisitor<
294312
) {
295313
super(rawConfig, {
296314
enumPrefix: getConfigValue(rawConfig.enumPrefix, true),
315+
onlyEnums: getConfigValue(rawConfig.onlyEnums, false),
297316
onlyOperationTypes: getConfigValue(rawConfig.onlyOperationTypes, false),
298317
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
299318
enumValues: parseEnumValues({
@@ -368,6 +387,7 @@ export class BaseTypesVisitor<
368387
}
369388

370389
public get scalarsDefinition(): string {
390+
if (this.config.onlyEnums) return '';
371391
const allScalars = Object.keys(this.config.scalars).map(scalarName => {
372392
const scalarValue = this.config.scalars[scalarName].type;
373393
const scalarType = this._schema.getType(scalarName);
@@ -434,10 +454,14 @@ export class BaseTypesVisitor<
434454
}
435455

436456
InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string {
457+
if (this.config.onlyEnums) return '';
458+
437459
return this.getInputObjectDeclarationBlock(node).string;
438460
}
439461

440462
InputValueDefinition(node: InputValueDefinitionNode): string {
463+
if (this.config.onlyEnums) return '';
464+
441465
const comment = transformComment(node.description as any as string, 1);
442466
const { input } = this._parsedConfig.declarationKind;
443467

@@ -454,6 +478,8 @@ export class BaseTypesVisitor<
454478
}
455479

456480
FieldDefinition(node: FieldDefinitionNode): string {
481+
if (this.config.onlyEnums) return '';
482+
457483
const typeString = node.type as any as string;
458484
const { type } = this._parsedConfig.declarationKind;
459485
const comment = this.getNodeComment(node);
@@ -462,7 +488,7 @@ export class BaseTypesVisitor<
462488
}
463489

464490
UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string {
465-
if (this.config.onlyOperationTypes) return '';
491+
if (this.config.onlyOperationTypes || this.config.onlyEnums) return '';
466492
const originalNode = parent[key] as UnionTypeDefinitionNode;
467493
const possibleTypes = originalNode.types
468494
.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t)))
@@ -531,7 +557,7 @@ export class BaseTypesVisitor<
531557
}
532558

533559
ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: number | string, parent: any): string {
534-
if (this.config.onlyOperationTypes) return '';
560+
if (this.config.onlyOperationTypes || this.config.onlyEnums) return '';
535561
const originalNode = parent[key] as ObjectTypeDefinitionNode;
536562

537563
return [this.getObjectTypeDeclarationBlock(node, originalNode).string, this.buildArgumentsBlock(originalNode)]
@@ -553,7 +579,7 @@ export class BaseTypesVisitor<
553579
}
554580

555581
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode, key: number | string, parent: any): string {
556-
if (this.config.onlyOperationTypes) return '';
582+
if (this.config.onlyOperationTypes || this.config.onlyEnums) return '';
557583
const originalNode = parent[key] as InterfaceTypeDefinitionNode;
558584

559585
return [this.getInterfaceTypeDeclarationBlock(node, originalNode).string, this.buildArgumentsBlock(originalNode)]
@@ -651,7 +677,10 @@ export class BaseTypesVisitor<
651677
return values
652678
.map(enumOption => {
653679
const optionName = this.makeValidEnumIdentifier(
654-
this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true })
680+
this.convertName(enumOption, {
681+
useTypesPrefix: false,
682+
transformUnderscore: true,
683+
})
655684
);
656685
const comment = this.getNodeComment(enumOption);
657686
const schemaEnumValue =
@@ -704,6 +733,7 @@ export class BaseTypesVisitor<
704733
name: string,
705734
field: FieldDefinitionNode
706735
): string {
736+
if (this.config.onlyEnums) return '';
707737
return this.getArgumentsObjectDeclarationBlock(node, name, field).string;
708738
}
709739

‎packages/plugins/typescript/typescript/src/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ export interface TypeScriptPluginConfig extends RawTypesConfig {
133133
* ```
134134
*/
135135
enumsAsConst?: boolean;
136+
/**
137+
* @description This will cause the generator to emit types for enums only.
138+
* @default false
139+
*
140+
* @exampleMarkdown Override all definition types
141+
* ```yml
142+
* generates:
143+
* path/to/file.ts:
144+
* plugins:
145+
* - typescript
146+
* config:
147+
* onlyEnums: true
148+
* ```
149+
*/
150+
onlyEnums?: boolean;
136151
/**
137152
* @description This will cause the generator to emit types for operations only (basically only enums and scalars).
138153
* Interacts well with `preResolveTypes: true`

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
3535
futureProofUnions: boolean;
3636
enumsAsConst: boolean;
3737
numericEnums: boolean;
38+
onlyEnums: boolean;
3839
onlyOperationTypes: boolean;
3940
immutableTypes: boolean;
4041
maybeValue: string;
@@ -66,6 +67,7 @@ export class TsVisitor<
6667
futureProofUnions: getConfigValue(pluginConfig.futureProofUnions, false),
6768
enumsAsConst: getConfigValue(pluginConfig.enumsAsConst, false),
6869
numericEnums: getConfigValue(pluginConfig.numericEnums, false),
70+
onlyEnums: getConfigValue(pluginConfig.onlyEnums, false),
6971
onlyOperationTypes: getConfigValue(pluginConfig.onlyOperationTypes, false),
7072
immutableTypes: getConfigValue(pluginConfig.immutableTypes, false),
7173
useImplementingTypes: getConfigValue(pluginConfig.useImplementingTypes, false),
@@ -147,6 +149,8 @@ export class TsVisitor<
147149
}
148150

149151
public getWrapperDefinitions(): string[] {
152+
if (this.config.onlyEnums) return [];
153+
150154
const definitions: string[] = [
151155
this.getMaybeValue(),
152156
this.getInputMaybeValue(),
@@ -166,6 +170,8 @@ export class TsVisitor<
166170
}
167171

168172
public getExactDefinition(): string {
173+
if (this.config.onlyEnums) return '';
174+
169175
return `${this.getExportPrefix()}${EXACT_SIGNATURE}`;
170176
}
171177

@@ -174,6 +180,8 @@ export class TsVisitor<
174180
}
175181

176182
public getMakeMaybeDefinition(): string {
183+
if (this.config.onlyEnums) return '';
184+
177185
return `${this.getExportPrefix()}${MAKE_MAYBE_SIGNATURE}`;
178186
}
179187

@@ -220,7 +228,8 @@ export class TsVisitor<
220228
}
221229

222230
UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string {
223-
if (this.config.onlyOperationTypes) return '';
231+
if (this.config.onlyOperationTypes || this.config.onlyEnums) return '';
232+
224233
let withFutureAddedValue: string[] = [];
225234
if (this.config.futureProofUnions) {
226235
withFutureAddedValue = [
@@ -318,7 +327,9 @@ export class TsVisitor<
318327
this.config.futureProofEnums ? [indent('| ' + wrapWithSingleQuotes('%future added value'))] : [],
319328
];
320329

321-
const enumTypeName = this.convertName(node, { useTypesPrefix: this.config.enumPrefix });
330+
const enumTypeName = this.convertName(node, {
331+
useTypesPrefix: this.config.enumPrefix,
332+
});
322333

323334
if (this.config.enumsAsTypes) {
324335
return new DeclarationBlock(this._declarationBlockConfig)
@@ -354,7 +365,10 @@ export class TsVisitor<
354365
const enumValue: string | number = valueFromConfig ?? i;
355366
const comment = transformComment(enumOption.description as any as string, 1);
356367
const optionName = this.makeValidEnumIdentifier(
357-
this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true })
368+
this.convertName(enumOption, {
369+
useTypesPrefix: false,
370+
transformUnderscore: true,
371+
})
358372
);
359373
return comment + indent(optionName) + ` = ${enumValue}`;
360374
})
@@ -380,7 +394,10 @@ export class TsVisitor<
380394
.withBlock(
381395
node.values
382396
.map(enumOption => {
383-
const optionName = this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true });
397+
const optionName = this.convertName(enumOption, {
398+
useTypesPrefix: false,
399+
transformUnderscore: true,
400+
});
384401
const comment = transformComment(enumOption.description as any as string, 1);
385402
const name = enumOption.name as unknown as string;
386403
const enumValue: string | number = getValueFromConfig(name) ?? name;

0 commit comments

Comments
 (0)