diff --git a/src/__tests__/fieldConverter-test.js b/src/__tests__/fieldConverter-test.js index b4a71b49..714b288f 100644 --- a/src/__tests__/fieldConverter-test.js +++ b/src/__tests__/fieldConverter-test.js @@ -3,6 +3,7 @@ import { EnumTypeComposer, schemaComposer, ListComposer, SchemaComposer } from 'graphql-compose'; import { UserModel } from '../__mocks__/userModel'; +import { mongoose } from '../__mocks__/mongooseCommon'; import { deriveComplexType, getFieldsFromModel, @@ -199,6 +200,21 @@ describe('fieldConverter', () => { expect(genderEnum.getFieldNames().length).toBe(fields.gender.enumValues.length); expect(genderEnum.getField('male').value).toBe(fields.gender.enumValues[0]); }); + + it('should work for Enums with keys preparation', () => { + const model = mongoose.model( + 'EnumTest', + mongoose.Schema({ + oc: { + type: String, + enum: ['ocpp1.6', 'ocpp2.0'], + }, + }) + ); + const flds: any = getFieldsFromModel(model); + const ocEnum = enumToGraphQL(flds.oc, '', schemaComposer); + expect(ocEnum.getFieldNames()).toEqual(['ocpp1_6', 'ocpp2_0']); + }); }); describe('embeddedToGraphQL()', () => { diff --git a/src/fieldsConverter.js b/src/fieldsConverter.js index 29bceba7..3c09a7fe 100644 --- a/src/fieldsConverter.js +++ b/src/fieldsConverter.js @@ -345,8 +345,9 @@ export function enumToGraphQL( const desc = _getFieldDescription(field); if (desc) etc.setDescription(desc); - const fields = valueList.reduce((result, val) => { - result[val] = { value: val }; // eslint-disable-line no-param-reassign + const fields = valueList.reduce((result, value) => { + const key = value.replace(/[^_a-zA-Z0-9]/g, '_'); + result[key] = { value }; // eslint-disable-line no-param-reassign return result; }, {}); etc.setFields(fields);