Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema): add support for filtering plural resource by indexed fields #2

Merged
merged 1 commit into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
});
});
});
});