Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit TypeScript string unions instead of enums by default #1750

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- `apollo-codegen-swift`
- Add documentation to input structs' constructors [#1619](https://github.com/apollographql/apollo-tooling/pull/1619)
- `apollo-codegen-typescript`
- <First `apollo-codegen-typescript` related entry goes here>
- Emit TypeScript string unions instead of enums by default [#1750](https://github.com/apollographql/apollo-tooling/pull/1750)
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-codegen-core/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface CompilerOptions {
// this option is only implemented in the ts codegen, so we name it
// `ts` fileExtension for now.
tsFileExtension?: string;
tsUseEnums?: boolean;
useReadOnlyTypes?: boolean;
suppressSwiftMultilineStringLiterals?: boolean;
omitDeprecatedEnumCases?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -252,11 +248,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -393,11 +385,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -455,11 +443,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -551,11 +535,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -693,10 +673,7 @@ TypescriptGeneratedFile {
// START Enums and Input Objects
//==============================================================

export enum EnumCommentTestCase {
first = \\"first\\",
second = \\"second\\",
}
export type EnumCommentTestCase = \\"first\\" | \\"second\\";

export interface Duplicate {
propA: EnumCommentTestCase;
Expand Down Expand Up @@ -876,11 +853,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1045,11 +1018,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1162,11 +1131,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1263,11 +1228,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1376,11 +1337,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1429,10 +1386,7 @@ TypescriptGeneratedFile {
// START Enums and Input Objects
//==============================================================

export enum EnumCommentTestCase {
first = \\"first\\",
second = \\"second\\",
}
export type EnumCommentTestCase = \\"first\\" | \\"second\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1481,10 +1435,7 @@ TypescriptGeneratedFile {
// START Enums and Input Objects
//==============================================================

export enum EnumCommentTestCase {
first = \\"first\\",
second = \\"second\\",
}
export type EnumCommentTestCase = \\"first\\" | \\"second\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1569,11 +1520,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1680,11 +1627,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -1748,11 +1691,7 @@ TypescriptGeneratedFile {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

/**
* The input object sent when passing in a color
Expand Down Expand Up @@ -1925,11 +1864,7 @@ exports[`Typescript codeGeneration multiple files 4`] = `
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

/**
* The input object sent when passing in a color
Expand Down Expand Up @@ -1968,11 +1903,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -2093,11 +2024,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

//==============================================================
// END Enums and Input Objects
Expand Down Expand Up @@ -2155,11 +2082,7 @@ Object {
/**
* The episodes in the Star Wars trilogy
*/
export enum Episode {
EMPIRE = \\"EMPIRE\\",
JEDI = \\"JEDI\\",
NEWHOPE = \\"NEWHOPE\\",
}
export type Episode = \\"EMPIRE\\" | \\"JEDI\\" | \\"NEWHOPE\\";

/**
* The input object sent when passing in a color
Expand Down
34 changes: 27 additions & 7 deletions packages/apollo-codegen-typescript/src/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,34 @@ export default class TypescriptGenerator {

public enumerationDeclaration(type: GraphQLEnumType) {
const { name, description } = type;
const enumMembers = sortEnumValues(type.getValues()).map(({ value }) => {
return t.TSEnumMember(t.identifier(value), t.stringLiteral(value));
});

const typeAlias = t.exportNamedDeclaration(
t.TSEnumDeclaration(t.identifier(name), enumMembers),
[]
);
const createEnumDeclaration = () => {
const enumMembers = sortEnumValues(type.getValues()).map(({ value }) => {
return t.TSEnumMember(t.identifier(value), t.stringLiteral(value));
});

return t.exportNamedDeclaration(
t.TSEnumDeclaration(t.identifier(name), enumMembers),
[]
);
};

const createStringUnionDeclaration = () => {
const unionMembers = t.TSUnionType(
sortEnumValues(type.getValues()).map(({ value }) =>
t.TSLiteralType(t.stringLiteral(value))
)
);

return t.exportNamedDeclaration(
t.TSTypeAliasDeclaration(t.identifier(name), null, unionMembers),
[]
);
};

const typeAlias = this.options.tsUseEnums
? createEnumDeclaration()
: createStringUnionDeclaration();

if (description) {
typeAlias.leadingComments = [
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ OPTIONS
--tsFileExtension=tsFileExtension By default, TypeScript will output "ts" files. Set "tsFileExtension" to
specify a different file extension, for example "d.ts"

--tsUseEnums Use TypeScript enums for GraphQL enums. By default, string unions are emitted

--useFlowExactObjects Use Flow exact objects for generated types [flow only]

--useFlowReadOnlyTypes Use read only types for generated types [flow only]. **Deprecated in favor
Expand Down
5 changes: 5 additions & 0 deletions packages/apollo/src/commands/client/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export default class Generate extends ClientCommand {
tsFileExtension: flags.string({
description:
'By default, TypeScript will output "ts" files. Set "tsFileExtension" to specify a different file extension, for example "d.ts"'
}),
tsUseEnums: flags.boolean({
description:
"Use TypeScript enums for GraphQL enums. By default, string unions are emitted"
})
};

Expand Down Expand Up @@ -221,6 +225,7 @@ export default class Generate extends ClientCommand {
flags.useReadOnlyTypes || flags.useFlowReadOnlyTypes,
globalTypesFile: flags.globalTypesFile,
tsFileExtension: flags.tsFileExtension,
tsUseEnums: flags.tsUseEnums,
suppressSwiftMultilineStringLiterals:
flags.suppressSwiftMultilineStringLiterals,
omitDeprecatedEnumCases: flags.omitDeprecatedEnumCases
Expand Down