Skip to content

Commit bc57633

Browse files
committed
Merge branch 'master' of github.com:mickhansen/graphql-sequelize
2 parents f429a17 + bddfaff commit bc57633

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ userType = new GraphQLObjectType({
172172
});
173173
```
174174

175+
### ENUM attributes with non-alphanumeric characters
176+
177+
GraphQL enum types [only support ASCII alphanumeric characters and underscores](https://facebook.github.io/graphql/#Name).
178+
If you have other characters, like a dash (`-`) in your Sequelize enum types,
179+
they will be converted to camelCase. For example: `foo-bar` becomes `fooBar`.
180+
175181
### VIRTUAL attributes and GraphQL fields
176182

177183
If you have `Sequelize.VIRTUAL` attributes on your sequelize model, you need to explicitly set the return type and any field dependencies via `new Sequelize.VIRTUAL(returnType, [dependencies ... ])`.

src/typeMapper.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function toGraphQL(sequelizeType, sequelizeTypes) {
3232
} = sequelizeTypes;
3333

3434
// Regex for finding special characters
35-
const specialChars = /[^a-z\d]/i;
35+
const specialChars = /[^a-z\d_]/i;
3636

3737
if (sequelizeType instanceof BOOLEAN) return GraphQLBoolean;
3838
if (sequelizeType instanceof FLOAT) return GraphQLFloat;
@@ -58,16 +58,17 @@ export function toGraphQL(sequelizeType, sequelizeTypes) {
5858
if (sequelizeType instanceof ENUM) {
5959
return new GraphQLEnumType({
6060
values: sequelizeType.values.reduce((obj, value) => {
61+
let sanitizedValue = value;
6162
if (specialChars.test(value)) {
62-
value = value.split(specialChars).reduce((reduced, val, idx) => {
63+
sanitizedValue = value.split(specialChars).reduce((reduced, val, idx) => {
6364
let newVal = val;
6465
if (idx > 0) {
6566
newVal = `${val[0].toUpperCase()}${val.slice(1)}`;
6667
}
6768
return `${reduced}${newVal}`;
6869
});
6970
}
70-
obj[value] = {value};
71+
obj[sanitizedValue] = {value};
7172
return obj;
7273
}, {})
7374
});

test/attributeFields.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('attributeFields', function () {
3939
type: Sequelize.ENUM('first', 'second')
4040
},
4141
enumTwo: {
42-
type: Sequelize.ENUM('first', 'second')
42+
type: Sequelize.ENUM('foo_bar', 'foo-bar')
4343
},
4444
list: {
4545
type: Sequelize.ARRAY(Sequelize.STRING)
@@ -108,4 +108,20 @@ describe('attributeFields', function () {
108108
expect(fields.enum.type.name).to.equal(modelName + 'enum' + 'EnumType');
109109
expect(fields.enumTwo.type.name).to.equal(modelName + 'enumTwo' + 'EnumType');
110110
});
111+
112+
it('should support enum values with characters not allowed by GraphQL', function () {
113+
var fields = attributeFields(Model);
114+
115+
expect(fields.enumTwo.type.getValues()).to.not.be.undefined;
116+
expect(fields.enumTwo.type.getValues()[1].name).to.equal('fooBar');
117+
expect(fields.enumTwo.type.getValues()[1].value).to.equal('foo-bar');
118+
});
119+
120+
it('should support enum values with underscores', function () {
121+
var fields = attributeFields(Model);
122+
123+
expect(fields.enumTwo.type.getValues()).to.not.be.undefined;
124+
expect(fields.enumTwo.type.getValues()[0].name).to.equal('foo_bar');
125+
expect(fields.enumTwo.type.getValues()[0].value).to.equal('foo_bar');
126+
});
111127
});

0 commit comments

Comments
 (0)