Skip to content

Commit

Permalink
fix(type): fix array of ObjectId without ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Aug 3, 2015
1 parent 95cf81f commit e5575f3
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 21 deletions.
3 changes: 3 additions & 0 deletions fixture/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ let UserSchema = new mongoose.Schema({
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
objectIds: [{
type: mongoose.Schema.Types.ObjectId
}],
weight: Number, // to test "floatish" numbers
createdAt: Date,
removed: Boolean,
Expand Down
7 changes: 5 additions & 2 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe('e2e', () => {
name: 'Bar',
age: 28,
mother: motherUser._id,
friends: [user1._id]
friends: [user1._id],
objectIds: [user1._id]
});

yield user2.save();
Expand Down Expand Up @@ -94,6 +95,7 @@ describe('e2e', () => {
_id
name
}
objectIds
}
}`);

Expand All @@ -110,7 +112,8 @@ describe('e2e', () => {
friends: [{
_id: user1._id.toString(),
name: 'Foo'
}]
}],
objectIds: [user1._id.toString()]
},
}
});
Expand Down
40 changes: 23 additions & 17 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ function getField(field, types, models, model) {
graphQLfield.description += ` of "${field.caster.instance}"`;
}

var refModelName;

// ObjectID
if (field.instance === 'ObjectID') {

Expand All @@ -54,13 +52,16 @@ function getField(field, types, models, model) {

graphQLfield.type = types[field.ref];
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
var projections = getProjection(fieldASTs);

return models[field.ref].model.findOne({
_id: new ObjectID(modelInstance[field.name])
}, projections);
};
} else {
}

// without ref
else {
graphQLfield.type = GraphQLString;
}
}
Expand Down Expand Up @@ -92,22 +93,27 @@ function getField(field, types, models, model) {

// Array of ObjectId
if (field.caster.instance === 'ObjectID') {
refModelName = field.caster.ref;

if (refModelName) {
graphQLfield.description += ` and reference to "${refModelName}" model`;
// with reference
if (field.caster.ref) {
graphQLfield.description += ` and reference to "${field.caster.ref}" model`;

graphQLfield.type = new GraphQLList(types[field.caster.ref]);
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
return models[field.caster.ref].model.find({
_id: {
// toString(): to make it easily testable
$in: modelInstance[field.name].map((id) => id.toString())
}
}, projections);
};
}

graphQLfield.type = new GraphQLList(types[refModelName]);
graphQLfield.resolve = (modelInstance, params, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
return models[refModelName].model.find({
_id: {
// to make it easily testable
$in: modelInstance[field.name].map((id) => id.toString())
}
}, projections);
};
// without reference
else {
graphQLfield.type = new GraphQLList(GraphQLString);
}
}

// Array of basic types
Expand Down
81 changes: 80 additions & 1 deletion src/field.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from 'chai';
import ObjectID from 'bson-objectid';
import {
GraphQLString,
GraphQLFloat,
Expand All @@ -12,6 +13,66 @@ import {getField} from './field';
var projection = require('./projection');

describe('field', () => {
it('should resolve ObjectId with ref properly', function* () {
this.sandbox.stub(projection, 'getProjection').returns({
name: 1
});

var user = new User();
var findOneStub = this.sandbox.stub(User, 'findOne').returnsWithResolve(user);

var field = getField({
instance: 'ObjectID',
name: '_id',
ref: 'User'
}, {
User: 'GraphQL Type'
}, {
User: {
model: User
}
}, {
name: 'User'
});

var result = yield field.resolve({
_id: user._id.toString()
}, undefined, undefined, {
name: {
value: '_id'
}
});

expect(findOneStub).to.calledWith({
_id: new ObjectID(user._id.toString()),
}, {
name: 1
});

expect(field).to.containSubset({
type: 'GraphQL Type',
name: '_id',
description: '"_id" field of the "User" model with type "ObjectID" and reference to "User" model'
});

expect(result).to.be.eql(user);
});

it('should resolve ObjectID without ref properly', () => {
var field = getField({
instance: 'ObjectID',
name: 'foo'
}, undefined, undefined, {
name: 'Bar'
});

expect(field).to.be.eql({
type: GraphQLString,
name: 'foo',
description: '"foo" field of the "Bar" model with type "ObjectID"'
});
});

it('should resolve String properly', () => {
var field = getField({
instance: 'String',
Expand Down Expand Up @@ -82,7 +143,7 @@ describe('field', () => {
expect(result).to.be.equal('2015-07-30T16:24:30.215Z');
});

it('should resolve Array of ObjectId properly', function* () {
it('should resolve Array of ObjectId with ref properly', function* () {
var users = [
new User(),
new User()
Expand Down Expand Up @@ -136,6 +197,24 @@ describe('field', () => {
});
});

it('should resolve Array of ObjectId without ref properly', () => {
var field = getField({
instance: 'Array',
name: 'foo',
caster: {
instance: 'ObjectID'
}
}, undefined, undefined, {
name: 'Bar'
});

expect(field).to.be.eql({
type: new GraphQLList(GraphQLString),
name: 'foo',
description: '"foo" field of the "Bar" model with type "Array" of "ObjectID"'
});
});

it('should resolve Array of String properly', () => {
var field = getField({
instance: 'Array',
Expand Down
2 changes: 1 addition & 1 deletion src/schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('schema', () => {
});
});

it('should get data with array of ref(s) fields', function* () {
it('should get data with array of ObjectId with ref(s) fields', function* () {
var user1 = new User({
name: 'Foo'
});
Expand Down

0 comments on commit e5575f3

Please sign in to comment.