Skip to content

Commit

Permalink
feat(model): add support for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Mar 24, 2016
1 parent 21e1e31 commit a0d151a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function getField(schemaPath) {
nonNull: !!index
};

if (schemaPath.enumValues && schemaPath.enumValues.length > 0) {
field.enumValues = schemaPath.enumValues;
}

// ObjectID ref
if (ref) {
field.reference = ref;
Expand Down
17 changes: 16 additions & 1 deletion src/type/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ function stringToGraphQLType(type) {
}
}

/**
* Returns a GraphQL Enum type based on a List of Strings
* @param {Array} list
* @param {String} name
* @return {Object}
*/
function listToGraphQLEnumType(list, name) {
const values = {};
list.forEach((val) => { values[val] = {value: val}; });
return new GraphQLEnumType({ name, values });
}

/**
* Extracts the fields of a GraphQL type
* @param {GraphQLType} type
Expand Down Expand Up @@ -190,7 +202,8 @@ function getType(graffitiModels, {name, description, fields}, path = [], rootTyp
// These references have to be resolved when all type definitions are avaiable
resolveReference[graphQLType.name] = resolveReference[graphQLType.name] || {};
const graphQLTypeFields = reduce(fields, (graphQLFields,
{name, description, type, subtype, reference, nonNull, hidden, hooks, fields: subfields, embeddedModel}, key) => {
{name, description, type, subtype, reference, nonNull, hidden, hooks,
fields: subfields, embeddedModel, enumValues}, key) => {
name = name || key;
const newPath = [...path, name];

Expand Down Expand Up @@ -231,6 +244,8 @@ function getType(graffitiModels, {name, description, fields}, path = [], rootTyp
: getType(graffitiModels, embeddedModel, ['embedded']);
type.mongooseEmbedded = true;
graphQLField.type = type;
} else if (enumValues && type === 'String') {
graphQLField.type = listToGraphQLEnumType(enumValues, getTypeFieldName(graphQLType.name, `${name}Enum`));
} else {
graphQLField.type = stringToGraphQLType(type);
}
Expand Down

0 comments on commit a0d151a

Please sign in to comment.