From 7522efa18993910e711aad2bc361967fd88cca29 Mon Sep 17 00:00:00 2001 From: Peter Marton Date: Sun, 26 Jul 2015 18:43:09 +0200 Subject: [PATCH] feat(schema): add support for filtering plural resource by indexed fields --- example/app.js | 2 +- src/schema.js | 46 +++++++++++++++++++++++++++++----------------- src/schema.spec.js | 21 +++++++++++++++++---- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/example/app.js b/example/app.js index 4d4b88e..e6c5d1e 100644 --- a/example/app.js +++ b/example/app.js @@ -27,7 +27,7 @@ function *run() { }`; // query = `{ - // users { + // users(age: 19) { // name // age // createdAt diff --git a/src/schema.js b/src/schema.js index 9f82692..e49885e 100644 --- a/src/schema.js +++ b/src/schema.js @@ -1,4 +1,4 @@ -import {reduce, each, isDate, isArray} from 'lodash'; +import {map, reduce, each, isDate, isArray} from 'lodash'; import { GraphQLObjectType, @@ -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) { @@ -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); } diff --git a/src/schema.spec.js b/src/schema.spec.js index a783e42..2674c2e 100644 --- a/src/schema.spec.js +++ b/src/schema.spec.js @@ -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, `{ @@ -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 + }); + }); }); });