Skip to content

Commit 57adbf0

Browse files
committed
Fixes #20
1 parent 206144f commit 57adbf0

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/attributeFields.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as typeMapper from './typeMapper';
2-
import { GraphQLNonNull } from 'graphql';
2+
import { GraphQLNonNull, GraphQLEnumType } from 'graphql';
33

44
module.exports = function (Model, options) {
55
options = options || {};
@@ -14,6 +14,10 @@ module.exports = function (Model, options) {
1414
type: typeMapper.toGraphQL(type, Model.sequelize.constructor)
1515
};
1616

17+
if ( memo[key].type instanceof GraphQLEnumType ) {
18+
memo[key].type.name = `${Model.name}${key}EnumType`;
19+
}
20+
1721
if (attribute.allowNull === false || attribute.primaryKey === true) {
1822
memo[key].type = new GraphQLNonNull(memo[key].type);
1923
}

test/attributeFields.test.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import {
1919

2020
describe('attributeFields', function () {
2121
var Model;
22-
22+
var modelName = Math.random().toString();
2323
before(function () {
24-
Model = sequelize.define(Math.random().toString(), {
24+
Model = sequelize.define(modelName, {
2525
email: {
2626
type: Sequelize.STRING,
2727
allowNull: false
@@ -38,6 +38,9 @@ describe('attributeFields', function () {
3838
enum: {
3939
type: Sequelize.ENUM('first', 'second')
4040
},
41+
enumTwo: {
42+
type: Sequelize.ENUM('first', 'second')
43+
},
4144
list: {
4245
type: Sequelize.ARRAY(Sequelize.STRING)
4346
},
@@ -55,7 +58,7 @@ describe('attributeFields', function () {
5558
it('should return fields for a simple model', function () {
5659
var fields = attributeFields(Model);
5760

58-
expect(Object.keys(fields)).to.deep.equal(['id', 'email', 'firstName', 'lastName', 'float', 'enum', 'list', 'virtualInteger', 'virtualBoolean']);
61+
expect(Object.keys(fields)).to.deep.equal(['id', 'email', 'firstName', 'lastName', 'float', 'enum', 'enumTwo', 'list', 'virtualInteger', 'virtualBoolean']);
5962

6063
expect(fields.id.type).to.be.an.instanceOf(GraphQLNonNull);
6164
expect(fields.id.type.ofType).to.equal(GraphQLInt);
@@ -69,6 +72,8 @@ describe('attributeFields', function () {
6972

7073
expect(fields.enum.type).to.be.an.instanceOf(GraphQLEnumType);
7174

75+
expect(fields.enumTwo.type).to.be.an.instanceOf(GraphQLEnumType);
76+
7277
expect(fields.list.type).to.be.an.instanceOf(GraphQLList);
7378

7479
expect(fields.float.type).to.equal(GraphQLFloat);
@@ -80,9 +85,19 @@ describe('attributeFields', function () {
8085

8186
it('should be possible to exclude fields', function () {
8287
var fields = attributeFields(Model, {
83-
exclude: ['id', 'email', 'float', 'enum', 'list', 'virtualInteger', 'virtualBoolean']
88+
exclude: ['id', 'email', 'float', 'enum', 'enumTwo', 'list', 'virtualInteger', 'virtualBoolean']
8489
});
8590

8691
expect(Object.keys(fields)).to.deep.equal(['firstName', 'lastName']);
8792
});
93+
94+
it('should automatically name enum types', function () {
95+
var fields = attributeFields(Model);
96+
97+
expect(fields.enum.type.name).to.not.be.undefined;
98+
expect(fields.enumTwo.type.name).to.not.be.undefined;
99+
100+
expect(fields.enum.type.name).to.equal(modelName + 'enum' + 'EnumType');
101+
expect(fields.enumTwo.type.name).to.equal(modelName + 'enumTwo' + 'EnumType');
102+
});
88103
});

0 commit comments

Comments
 (0)