Skip to content

Commit

Permalink
feat(schema): filter plural resource by array of _id -s
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Jul 27, 2015
1 parent 61dcfb3 commit 26e642f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduce, each, isDate} from 'lodash';
import {reduce, each, isDate, isArray} from 'lodash';

import {
GraphQLObjectType,
Expand Down Expand Up @@ -174,9 +174,24 @@ function getSchema (models) {

fields[pluralName] = {
type: new GraphQLList(type),
args: {
_id: {
name: '_id',
type: new GraphQLList(GraphQLString)
}
},
resolve: (root, args, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
return modelMap[typeName].find({}, projections);
var filter = {};

// filter by array of _id(s)
if (args._id && isArray(args._id)) {
filter._id = {
$in: args._id
};
}

return modelMap[typeName].find(filter, projections);
}
};

Expand Down
19 changes: 19 additions & 0 deletions src/schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ describe('schema', () => {
});
});

// TODO: cover sub-documents with unit tests
it('should get data with sub-document fields');

it('should get data with ref fields', function* () {
Expand Down Expand Up @@ -207,6 +208,24 @@ describe('schema', () => {
});
});

it('should filter daya by array of _id(s)', function* () {
var findStub = this.sandbox.stub(User, 'find').returnsWithResolve([]);

var result = yield graphql(schema, `{
users(_id: ["aaa", "bbb"]) {
name
}
}`);

expect(findStub).to.calledWith({
_id: {
$in: ['aaa', 'bbb']
}
}, {
name: 1
})
});

// TODO: missing test cases
it('should filter data by indexed fields');
});
Expand Down

0 comments on commit 26e642f

Please sign in to comment.