Skip to content

Commit

Permalink
Use TypeScript string enums (apollographql#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecstasy2 authored and Bobo Diallo committed Sep 26, 2017
1 parent ee7a771 commit 12e0ee8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/typescript/codeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ function enumerationDeclaration(generator: CodeGenerator, type: GraphQLEnumType)
generator.printOnNewline(`// ${line.trim()}`);
})
}
generator.printOnNewline(`export type ${name} =`);
const nValues = values.length;
values.forEach((value, i) => {
generator.printOnNewline(`export enum ${name} {`);
values.forEach((value) => {
if (!value.description || value.description.indexOf('\n') === -1) {
generator.printOnNewline(` "${value.value}"${i === nValues - 1 ? ';' : ' |'}${wrap(' // ', value.description)}`)
generator.printOnNewline(` ${value.value} = "${value.value}",${wrap(' // ', value.description)}`)
} else {
if (value.description) {
value.description.split('\n')
.forEach(line => {
generator.printOnNewline(` // ${line.trim()}`);
})
}
generator.printOnNewline(` "${value.value}"${i === nValues - 1 ? ';' : ' |'}`)
generator.printOnNewline(` ${value.value} = "${value.value}",`)
}
});
generator.printOnNewline(`}`);
generator.printNewline();
}

Expand Down
43 changes: 24 additions & 19 deletions test/typescript/__snapshots__/codeGeneration.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ exports[`TypeScript code generation #generateSource() should generate correct li
// This file was automatically generated and should not be edited.
// The episodes in the Star Wars trilogy
export type Episode =
\\"NEWHOPE\\" | // Star Wars Episode IV: A New Hope, released in 1977.
\\"EMPIRE\\" | // Star Wars Episode V: The Empire Strikes Back, released in 1980.
\\"JEDI\\"; // Star Wars Episode VI: Return of the Jedi, released in 1983.
export enum Episode {
NEWHOPE = \\"NEWHOPE\\", // Star Wars Episode IV: A New Hope, released in 1977.
EMPIRE = \\"EMPIRE\\", // Star Wars Episode V: The Empire Strikes Back, released in 1980.
JEDI = \\"JEDI\\", // Star Wars Episode VI: Return of the Jedi, released in 1983.
}
export type HeroAndFriendsNamesQueryVariables = {
Expand Down Expand Up @@ -140,10 +141,11 @@ exports[`TypeScript code generation #generateSource() should generate mutation o
// This file was automatically generated and should not be edited.
// The episodes in the Star Wars trilogy
export type Episode =
\\"NEWHOPE\\" | // Star Wars Episode IV: A New Hope, released in 1977.
\\"EMPIRE\\" | // Star Wars Episode V: The Empire Strikes Back, released in 1980.
\\"JEDI\\"; // Star Wars Episode VI: Return of the Jedi, released in 1983.
export enum Episode {
NEWHOPE = \\"NEWHOPE\\", // Star Wars Episode IV: A New Hope, released in 1977.
EMPIRE = \\"EMPIRE\\", // Star Wars Episode V: The Empire Strikes Back, released in 1980.
JEDI = \\"JEDI\\", // Star Wars Episode VI: Return of the Jedi, released in 1983.
}
export type ReviewInput = {
Expand Down Expand Up @@ -219,10 +221,11 @@ exports[`TypeScript code generation #generateSource() should generate simple nes
// This file was automatically generated and should not be edited.
// The episodes in the Star Wars trilogy
export type Episode =
\\"NEWHOPE\\" | // Star Wars Episode IV: A New Hope, released in 1977.
\\"EMPIRE\\" | // Star Wars Episode V: The Empire Strikes Back, released in 1980.
\\"JEDI\\"; // Star Wars Episode VI: Return of the Jedi, released in 1983.
export enum Episode {
NEWHOPE = \\"NEWHOPE\\", // Star Wars Episode IV: A New Hope, released in 1977.
EMPIRE = \\"EMPIRE\\", // Star Wars Episode V: The Empire Strikes Back, released in 1980.
JEDI = \\"JEDI\\", // Star Wars Episode VI: Return of the Jedi, released in 1983.
}
export type HeroAndFriendsNamesQueryVariables = {
Expand Down Expand Up @@ -306,10 +309,11 @@ exports[`TypeScript code generation #generateSource() should generate simple que
// This file was automatically generated and should not be edited.
// The episodes in the Star Wars trilogy
export type Episode =
\\"NEWHOPE\\" | // Star Wars Episode IV: A New Hope, released in 1977.
\\"EMPIRE\\" | // Star Wars Episode V: The Empire Strikes Back, released in 1980.
\\"JEDI\\"; // Star Wars Episode VI: Return of the Jedi, released in 1983.
export enum Episode {
NEWHOPE = \\"NEWHOPE\\", // Star Wars Episode IV: A New Hope, released in 1977.
EMPIRE = \\"EMPIRE\\", // Star Wars Episode V: The Empire Strikes Back, released in 1980.
JEDI = \\"JEDI\\", // Star Wars Episode VI: Return of the Jedi, released in 1983.
}
export type HeroNameQueryVariables = {
Expand All @@ -336,11 +340,12 @@ exports[`TypeScript code generation #generateSource() should handle comments in
"/* tslint:disable */
// This file was automatically generated and should not be edited.
export type EnumCommentTestCase =
\\"first\\" | // This is a single-line comment
export enum EnumCommentTestCase {
first = \\"first\\", // This is a single-line comment
// This is a multi-line
// comment.
\\"second\\";
second = \\"second\\",
}
export type CustomScalarQuery = {
Expand Down

0 comments on commit 12e0ee8

Please sign in to comment.