Skip to content

Commit

Permalink
New option onlyEnums for Typescript (#7718)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Borduhh and charlypoly committed May 5, 2022
1 parent 8f2230a commit 9a5f31c
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-snakes-act.md
@@ -0,0 +1,6 @@
---
"@graphql-codegen/typescript": patch
"@graphql-codegen/visitor-plugin-common": patch
---

New option `onlyEnums` for Typescript
14 changes: 14 additions & 0 deletions dev-test/codegen.yml
Expand Up @@ -98,6 +98,13 @@ generates:
plugins:
- typescript
- typescript-operations
./dev-test/githunt/types.onlyEnums.ts:
schema: ./dev-test/githunt/schema.json
documents: ./dev-test/githunt/**/*.graphql
config:
onlyEnums: true
plugins:
- typescript
./dev-test/githunt/types.preResolveTypes.onlyOperationTypes.ts:
schema: ./dev-test/githunt/schema.json
documents: ./dev-test/githunt/**/*.graphql
Expand Down Expand Up @@ -293,6 +300,13 @@ generates:
plugins:
- typescript
- typescript-operations
./dev-test/star-wars/types.OnlyEnums.ts:
schema: ./dev-test/star-wars/schema.json
documents: ./dev-test/star-wars/**/*.graphql
config:
onlyEnums: true
plugins:
- typescript
./dev-test/star-wars/types.preResolveTypes.onlyOperationTypes.ts:
schema: ./dev-test/star-wars/schema.json
documents: ./dev-test/star-wars/**/*.graphql
Expand Down
16 changes: 16 additions & 0 deletions dev-test/githunt/types.onlyEnums.ts
@@ -0,0 +1,16 @@
/** A list of options for the sort order of the feed */
export enum FeedType {
/** Sort by a combination of freshness and score, using Reddit's algorithm */
Hot = 'HOT',
/** Newest entries first */
New = 'NEW',
/** Highest score entries first */
Top = 'TOP',
}

/** The type of vote to record, when submitting a vote */
export enum VoteType {
Cancel = 'CANCEL',
Down = 'DOWN',
Up = 'UP',
}
17 changes: 17 additions & 0 deletions dev-test/star-wars/types.OnlyEnums.ts
@@ -0,0 +1,17 @@
/** The episodes in the Star Wars trilogy */
export enum Episode {
/** Star Wars Episode V: The Empire Strikes Back, released in 1980. */
Empire = 'EMPIRE',
/** Star Wars Episode VI: Return of the Jedi, released in 1983. */
Jedi = 'JEDI',
/** Star Wars Episode IV: A New Hope, released in 1977. */
Newhope = 'NEWHOPE',
}

/** Units of height */
export enum LengthUnit {
/** Primarily used in the United States */
Foot = 'FOOT',
/** The standard unit around the world */
Meter = 'METER',
}
8 changes: 8 additions & 0 deletions dev-test/test-schema/types.onlyEnums.ts
@@ -0,0 +1,8 @@
export type TestQueryVariables = Exact<{ [key: string]: never }>;

export type TestQuery = {
__typename?: 'Query';
testArr1?: Array<string | null> | null;
testArr2: Array<string | null>;
testArr3: Array<string>;
};
Expand Up @@ -49,6 +49,7 @@ export interface ParsedTypesConfig extends ParsedConfig {
enumValues: ParsedEnumValuesMap;
declarationKind: DeclarationKindConfig;
addUnderscoreToArgsType: boolean;
onlyEnums: boolean;
onlyOperationTypes: boolean;
enumPrefix: boolean;
fieldWrapperValue: string;
Expand Down Expand Up @@ -170,6 +171,23 @@ export interface RawTypesConfig extends RawConfig {
* ```
*/
wrapFieldDefinitions?: boolean;
/**
* @description This will cause the generator to emit types for enums only
* @default false
*
* @exampleMarkdown
* ## Override all definition types
*
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* config:
* onlyEnums: true
* ```
*/
onlyEnums?: boolean;
/**
* @description This will cause the generator to emit types for operations only (basically only enums and scalars)
* @default false
Expand Down Expand Up @@ -294,6 +312,7 @@ export class BaseTypesVisitor<
) {
super(rawConfig, {
enumPrefix: getConfigValue(rawConfig.enumPrefix, true),
onlyEnums: getConfigValue(rawConfig.onlyEnums, false),
onlyOperationTypes: getConfigValue(rawConfig.onlyOperationTypes, false),
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
enumValues: parseEnumValues({
Expand Down Expand Up @@ -368,6 +387,7 @@ export class BaseTypesVisitor<
}

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

InputObjectTypeDefinition(node: InputObjectTypeDefinitionNode): string {
if (this.config.onlyEnums) return '';

return this.getInputObjectDeclarationBlock(node).string;
}

InputValueDefinition(node: InputValueDefinitionNode): string {
if (this.config.onlyEnums) return '';

const comment = transformComment(node.description as any as string, 1);
const { input } = this._parsedConfig.declarationKind;

Expand All @@ -454,6 +478,8 @@ export class BaseTypesVisitor<
}

FieldDefinition(node: FieldDefinitionNode): string {
if (this.config.onlyEnums) return '';

const typeString = node.type as any as string;
const { type } = this._parsedConfig.declarationKind;
const comment = this.getNodeComment(node);
Expand All @@ -462,7 +488,7 @@ export class BaseTypesVisitor<
}

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

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

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

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

return [this.getInterfaceTypeDeclarationBlock(node, originalNode).string, this.buildArgumentsBlock(originalNode)]
Expand Down Expand Up @@ -651,7 +677,10 @@ export class BaseTypesVisitor<
return values
.map(enumOption => {
const optionName = this.makeValidEnumIdentifier(
this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true })
this.convertName(enumOption, {
useTypesPrefix: false,
transformUnderscore: true,
})
);
const comment = this.getNodeComment(enumOption);
const schemaEnumValue =
Expand Down Expand Up @@ -704,6 +733,7 @@ export class BaseTypesVisitor<
name: string,
field: FieldDefinitionNode
): string {
if (this.config.onlyEnums) return '';
return this.getArgumentsObjectDeclarationBlock(node, name, field).string;
}

Expand Down
15 changes: 15 additions & 0 deletions packages/plugins/typescript/typescript/src/config.ts
Expand Up @@ -133,6 +133,21 @@ export interface TypeScriptPluginConfig extends RawTypesConfig {
* ```
*/
enumsAsConst?: boolean;
/**
* @description This will cause the generator to emit types for enums only.
* @default false
*
* @exampleMarkdown Override all definition types
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* config:
* onlyEnums: true
* ```
*/
onlyEnums?: boolean;
/**
* @description This will cause the generator to emit types for operations only (basically only enums and scalars).
* Interacts well with `preResolveTypes: true`
Expand Down
25 changes: 21 additions & 4 deletions packages/plugins/typescript/typescript/src/visitor.ts
Expand Up @@ -35,6 +35,7 @@ export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig {
futureProofUnions: boolean;
enumsAsConst: boolean;
numericEnums: boolean;
onlyEnums: boolean;
onlyOperationTypes: boolean;
immutableTypes: boolean;
maybeValue: string;
Expand Down Expand Up @@ -66,6 +67,7 @@ export class TsVisitor<
futureProofUnions: getConfigValue(pluginConfig.futureProofUnions, false),
enumsAsConst: getConfigValue(pluginConfig.enumsAsConst, false),
numericEnums: getConfigValue(pluginConfig.numericEnums, false),
onlyEnums: getConfigValue(pluginConfig.onlyEnums, false),
onlyOperationTypes: getConfigValue(pluginConfig.onlyOperationTypes, false),
immutableTypes: getConfigValue(pluginConfig.immutableTypes, false),
useImplementingTypes: getConfigValue(pluginConfig.useImplementingTypes, false),
Expand Down Expand Up @@ -147,6 +149,8 @@ export class TsVisitor<
}

public getWrapperDefinitions(): string[] {
if (this.config.onlyEnums) return [];

const definitions: string[] = [
this.getMaybeValue(),
this.getInputMaybeValue(),
Expand All @@ -166,6 +170,8 @@ export class TsVisitor<
}

public getExactDefinition(): string {
if (this.config.onlyEnums) return '';

return `${this.getExportPrefix()}${EXACT_SIGNATURE}`;
}

Expand All @@ -174,6 +180,8 @@ export class TsVisitor<
}

public getMakeMaybeDefinition(): string {
if (this.config.onlyEnums) return '';

return `${this.getExportPrefix()}${MAKE_MAYBE_SIGNATURE}`;
}

Expand Down Expand Up @@ -220,7 +228,8 @@ export class TsVisitor<
}

UnionTypeDefinition(node: UnionTypeDefinitionNode, key: string | number | undefined, parent: any): string {
if (this.config.onlyOperationTypes) return '';
if (this.config.onlyOperationTypes || this.config.onlyEnums) return '';

let withFutureAddedValue: string[] = [];
if (this.config.futureProofUnions) {
withFutureAddedValue = [
Expand Down Expand Up @@ -318,7 +327,9 @@ export class TsVisitor<
this.config.futureProofEnums ? [indent('| ' + wrapWithSingleQuotes('%future added value'))] : [],
];

const enumTypeName = this.convertName(node, { useTypesPrefix: this.config.enumPrefix });
const enumTypeName = this.convertName(node, {
useTypesPrefix: this.config.enumPrefix,
});

if (this.config.enumsAsTypes) {
return new DeclarationBlock(this._declarationBlockConfig)
Expand Down Expand Up @@ -354,7 +365,10 @@ export class TsVisitor<
const enumValue: string | number = valueFromConfig ?? i;
const comment = transformComment(enumOption.description as any as string, 1);
const optionName = this.makeValidEnumIdentifier(
this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true })
this.convertName(enumOption, {
useTypesPrefix: false,
transformUnderscore: true,
})
);
return comment + indent(optionName) + ` = ${enumValue}`;
})
Expand All @@ -380,7 +394,10 @@ export class TsVisitor<
.withBlock(
node.values
.map(enumOption => {
const optionName = this.convertName(enumOption, { useTypesPrefix: false, transformUnderscore: true });
const optionName = this.convertName(enumOption, {
useTypesPrefix: false,
transformUnderscore: true,
});
const comment = transformComment(enumOption.description as any as string, 1);
const name = enumOption.name as unknown as string;
const enumValue: string | number = getValueFromConfig(name) ?? name;
Expand Down

1 comment on commit 9a5f31c

@vercel
Copy link

@vercel vercel bot commented on 9a5f31c May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.