Skip to content

Commit

Permalink
feat(schema): add support for filtering plural resource by indexed fi…
Browse files Browse the repository at this point in the history
…elds
  • Loading branch information
Peter Marton committed Jul 26, 2015
1 parent b0d3032 commit 7522efa
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function *run() {
}`;

// query = `{
// users {
// users(age: 19) {
// name
// age
// createdAt
Expand Down
46 changes: 29 additions & 17 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reduce, each, isDate, isArray} from 'lodash';
import {map, reduce, each, isDate, isArray} from 'lodash';

import {
GraphQLObjectType,
Expand Down Expand Up @@ -179,16 +179,29 @@ function getSchema (models) {
// TODO handle non s ended plurarls
var pluralName = `${typeName.toLowerCase()}s`;

var singularArgs = modelArgs[typeName];
var pluralArgs = reduce(modelArgs[typeName], (args, arg, argName) => {
if (argName === '_id') {
args[argName] = {
type: new GraphQLList(arg.type)
};
} else {
args[argName] = arg;
}

return args;
}, {});

// TODO: args -> filter by indexed fields

// TODO: args by index and _id
fields[singularName] = {
type: type,
args: modelArgs[typeName],
args: singularArgs,
resolve: (root, args, source, fieldASTs) => {
var projections = getProjection(fieldASTs);

args = reduce(args, (args, arg, argName) => {
var filter = reduce(args, (args, arg, argName) => {
if (arg && argName === '_id') {
args[argName] = mongoose.Types.ObjectId(arg);
} else if (arg) {
Expand All @@ -198,28 +211,27 @@ function getSchema (models) {
return args;
}, {});

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

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

// filter by array of _id(s)
if (args._id && isArray(args._id)) {
filter._id = {
$in: args._id
};
}
var filter = reduce(args, (args, arg, argName) => {
if (arg && argName === '_id') {
args[argName] = {
$in: arg
};
} else if (arg) {
args[argName] = arg;
}

return args;
}, {});

return modelMap[typeName].find(filter, projections);
}
Expand Down
21 changes: 17 additions & 4 deletions src/schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ describe('schema', () => {
});
});

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

var result = yield graphql(schema, `{
Expand All @@ -263,10 +263,23 @@ describe('schema', () => {
}
}, {
name: 1
})
});
});

// TODO: missing test cases
it('should filter data by indexed fields');
it('should filter data by indexed fields', function* () {
var findStub = this.sandbox.stub(User, 'find').returnsWithResolve([]);

var result = yield graphql(schema, `{
users(age: 17) {
name
}
}`);

expect(findStub).to.calledWith({
age: 17
}, {
name: 1
});
});
});
});

0 comments on commit 7522efa

Please sign in to comment.