Skip to content

Commit

Permalink
Fixed reading of enumValues from config (#4879)
Browse files Browse the repository at this point in the history
Co-authored-by: Borre <borre.mosch@cubonacci.com>
  • Loading branch information
borremosch and Borre committed Oct 10, 2020
1 parent b412d85 commit 077cf06
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-owls-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript': patch
---

Fixed reading of enumValues config values
6 changes: 3 additions & 3 deletions packages/plugins/typescript/typescript/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class TsVisitor<
node.values
.map(enumOption => {
const name = (enumOption.name as unknown) as string;
const enumValue: string | number = getValueFromConfig(name) || name;
const enumValue: string | number = getValueFromConfig(name) ?? name;
const comment = transformComment((enumOption.description as any) as string, 1);

return comment + indent('| ' + wrapWithSingleQuotes(enumValue));
Expand All @@ -223,7 +223,7 @@ export class TsVisitor<
node.values
.map((enumOption, i) => {
const valueFromConfig = getValueFromConfig((enumOption.name as unknown) as string);
const enumValue: string | number = valueFromConfig || i;
const enumValue: string | number = valueFromConfig ?? i;
const comment = transformComment((enumOption.description as any) as string, 1);

return comment + indent((enumOption.name as unknown) as string) + ` = ${enumValue}`;
Expand Down Expand Up @@ -253,7 +253,7 @@ export class TsVisitor<
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;
const enumValue: string | number = getValueFromConfig(name) ?? name;

return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
})
Expand Down
36 changes: 36 additions & 0 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,42 @@ describe('TypeScript', () => {
export type MyEnum = typeof MyEnum[keyof typeof MyEnum];`);
});

it('Should work with enum as const combined with enum values', async () => {
const schema = buildSchema(/* GraphQL */ `
enum MyEnum {
A_B_C
X_Y_Z
_TEST
My_Value
}
`);
const result = (await plugin(
schema,
[],
{
enumsAsConst: true,
enumValues: {
MyEnum: {
A_B_C: 0,
X_Y_Z: 'Foo',
_TEST: 'Bar',
My_Value: 1,
},
},
},
{ outputFile: '' }
)) as Types.ComplexPluginOutput;

expect(result.content).toBeSimilarStringTo(`
export const MyEnum = {
ABC: 0,
XYZ: 'Foo',
Test: 'Bar',
MyValue: 1
} as const;
export type MyEnum = typeof MyEnum[keyof typeof MyEnum];`);
});

it('Should work with enum and enum values (enumsAsTypes)', async () => {
const schema = buildSchema(/* GraphQL */ `
"custom enum"
Expand Down

0 comments on commit 077cf06

Please sign in to comment.