Skip to content

Commit

Permalink
feat(schema): add support for Number, Boolean and Date types
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Jul 26, 2015
1 parent 2b62c48 commit a07767c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
5 changes: 5 additions & 0 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ function *run() {
var query = `{
user(id: "559645cd1a38532d14349246") {
name
age
createdAt
nums
removed
friends {
name
age
}
}
}`;
Expand Down
6 changes: 5 additions & 1 deletion example/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ var UserSchema = new mongoose.Schema({
name: {
type: String
},
age: Number,
createdAt: Date,
friends: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
}],
nums: [Number],
removed: Boolean
});

var User = mongoose.model('User', UserSchema);
Expand Down
70 changes: 54 additions & 16 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {reduce, each} from 'lodash';
import {reduce, each, isDate} from 'lodash';

import {
GraphQLObjectType,
GraphQLNonNull,
GraphQLSchema,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLList
} from 'graphql/lib/type';

Expand Down Expand Up @@ -62,31 +64,67 @@ function getSchema (models) {
// String
if (['String', 'ObjectID'].indexOf(path.instance) > -1) {
return {
type: new GraphQLNonNull(GraphQLString)
type: GraphQLString
};
}

// Array of refs
else if (path.instance === 'Array') {
var type = path.caster.options.ref;

// TODO: handle non ref arrays
// Number
else if (path.instance === 'Number') {
return {
type: GraphQLInt
};
}

// Date
else if (path.instance === 'Date') {
return {
type: new GraphQLList(types[type]),
type: GraphQLString,
resolve: (modelInstance, params, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
return modelMap[type].find({
_id: {
// to make it easily testable
$in: modelInstance[path.path].map((id) => id.toString())
}
}, projections);
if (isDate(modelInstance[fieldASTs.name.value])) {
return modelInstance[fieldASTs.name.value].toISOString();
}

return null;
}
};
}

// TODO: handle Number, Date etc.
// Boolean
else if (path.instance === 'Boolean') {
return {
type: GraphQLBoolean
};
}

// Array
else if (path.instance === 'Array') {
var type = path.caster.options.ref;

// Array of refs
if (path.caster.instance === 'ObjectID') {
return {
type: new GraphQLList(types[type]),
resolve: (modelInstance, params, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
return modelMap[type].find({
_id: {
// to make it easily testable
$in: modelInstance[path.path].map((id) => id.toString())
}
}, projections);
}
};

// Array of primitives
// FIXME: cannot handle array
} else {
return {
// type: new GraphQLList(getField(path.caster))
};
}
}

// TODO: handle more type
}

// Create top level fields
Expand Down

0 comments on commit a07767c

Please sign in to comment.