Skip to content

Commit 8b10f22

Browse files
georgehbsaihaj
andauthored
fix: ensure falsy enum values are still mapped (#7900)
* fix: ensure falsy enum values are still mapped * Create spotty-waves-whisper.md Co-authored-by: Saihajpreet Singh <saihajpreet.singh@gmail.com>
1 parent aa1e6ea commit 8b10f22

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

‎.changeset/spotty-waves-whisper.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+
Ensure falsy enum values are still mapped

‎packages/plugins/other/visitor-plugin-common/src/enum-values.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function parseEnumValues({
2424
for (const enumTypeName of allEnums) {
2525
const enumType = schema.getType(enumTypeName) as GraphQLEnumType;
2626
for (const { name, value } of enumType.getValues()) {
27-
if (value && value !== name) {
27+
if (value !== name) {
2828
mapOrStr[enumTypeName] = mapOrStr[enumTypeName] || {};
2929
if (typeof mapOrStr[enumTypeName] !== 'string' && !mapOrStr[enumTypeName][name]) {
3030
mapOrStr[enumTypeName][name] = typeof value === 'string' ? escapeString(value) : value;

‎packages/plugins/typescript/typescript/tests/typescript.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,38 @@ describe('TypeScript', () => {
637637
expect(output).toContain(`SomethingElse = '99'`);
638638
});
639639

640+
it('#7898 - falsy enum value set on schema with enumsAsTypes set', async () => {
641+
const testSchema = new GraphQLSchema({
642+
types: [
643+
new GraphQLObjectType({
644+
name: 'Query',
645+
fields: {
646+
test: {
647+
type: new GraphQLEnumType({
648+
name: 'MyEnum',
649+
values: {
650+
EnumValueName: {
651+
value: 0,
652+
},
653+
},
654+
}),
655+
},
656+
},
657+
}),
658+
],
659+
});
660+
661+
const result = (await plugin(
662+
testSchema,
663+
[],
664+
{ enumsAsTypes: true },
665+
{ outputFile: '' }
666+
)) as Types.ComplexPluginOutput;
667+
const output = mergeOutputs([result]);
668+
expect(output).not.toContain('EnumValueName');
669+
expect(output).toContain('0');
670+
});
671+
640672
it('#6532 - numeric enum values with namingConvention', async () => {
641673
const testSchema = buildSchema(/* GraphQL */ `
642674
type Query {

0 commit comments

Comments
 (0)