Skip to content

Commit

Permalink
fix(FieldConverter): illegal symbols for Enum keys now converted to _…
Browse files Browse the repository at this point in the history
… (underscore)
  • Loading branch information
nodkz committed May 8, 2020
1 parent fc8faea commit c01b6af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/__tests__/fieldConverter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/fieldsConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c01b6af

Please sign in to comment.