Skip to content

Commit 3bffc66

Browse files
committed
Add count resolver
1 parent ff80038 commit 3bffc66

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/definition.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export type MongooseQuery = {
6161
setOptions(opts: ObjectMap): MongooseQuery,
6262
update(data: ObjectMap): MongooseQuery,
6363
remove(conditions: ?Object, options?: Object): MongooseQuery,
64+
count(conditions: ?Object): MongooseQuery,
6465
};
6566

6667
export type MongoseDocument = {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* @flow */
2+
3+
import { expect } from 'chai';
4+
import { UserModel } from '../../__mocks__/userModel.js';
5+
import count from '../count';
6+
import Resolver from '../../../../graphql-compose/src/resolver/resolver';
7+
import { GraphQLObjectType, GraphQLInt } from 'graphql';
8+
9+
const UserType = new GraphQLObjectType({
10+
name: 'MockUserType',
11+
});
12+
13+
describe('count() ->', () => {
14+
let user1;
15+
let user2;
16+
17+
before('clear UserModel collection', (done) => {
18+
UserModel.collection.drop(() => {
19+
done();
20+
});
21+
});
22+
23+
before('add test user document to mongoDB', () => {
24+
user1 = new UserModel({
25+
name: 'userName1',
26+
skills: ['js', 'ruby', 'php', 'python'],
27+
gender: 'male',
28+
relocation: true,
29+
});
30+
31+
user2 = new UserModel({
32+
name: 'userName2',
33+
skills: ['go', 'erlang'],
34+
gender: 'female',
35+
relocation: false,
36+
});
37+
38+
return Promise.all([
39+
user1.save(),
40+
user2.save(),
41+
]);
42+
});
43+
44+
it('should return Resolver object', () => {
45+
const resolver = count(UserModel, UserType);
46+
expect(resolver).to.be.instanceof(Resolver);
47+
});
48+
49+
describe('Resolver.args', () => {
50+
it('should have `filter` arg', () => {
51+
const resolver = count(UserModel, UserType);
52+
expect(resolver.hasArg('filter')).to.be.true;
53+
});
54+
});
55+
56+
describe('Resolver.resolve():Promise', () => {
57+
it('should be fulfilled promise', async () => {
58+
const result = count(UserModel, UserType).resolve({});
59+
await expect(result).be.fulfilled;
60+
});
61+
62+
it('should return total number of documents in collection if args is empty', async () => {
63+
const result = await count(UserModel, UserType).resolve({ args: {} });
64+
expect(result).to.equal(2);
65+
});
66+
67+
it('should return number of document by filter data', async () => {
68+
const result = await count(UserModel, UserType).resolve(
69+
{ args: { filter: { gender: 'male' } } }
70+
);
71+
expect(result).to.equal(1);
72+
});
73+
});
74+
75+
describe('Resolver.getOutputType()', () => {
76+
it('should return GraphQLInt type', () => {
77+
const outputType = count(UserModel, UserType).getOutputType();
78+
expect(outputType).to.equal(GraphQLInt);
79+
});
80+
});
81+
});

src/resolvers/count.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* @flow */
2+
/* eslint-disable no-param-reassign */
3+
4+
import type {
5+
MongooseModelT,
6+
GraphQLObjectType,
7+
ExtendedResolveParams,
8+
} from '../definition';
9+
import Resolver from '../../../graphql-compose/src/resolver/resolver';
10+
import { GraphQLInt } from 'graphql';
11+
12+
import { filterHelperArgsGen, filterHelper } from './helpers/filter';
13+
14+
export default function count(model: MongooseModelT, gqType: GraphQLObjectType): Resolver {
15+
return new Resolver({
16+
outputType: GraphQLInt,
17+
name: 'count',
18+
kind: 'query',
19+
args: {
20+
...filterHelperArgsGen(model, {
21+
filterTypeName: `Filter${gqType.name}Input`,
22+
}),
23+
},
24+
resolve: (resolveParams : ExtendedResolveParams) => {
25+
resolveParams.query = model.find();
26+
filterHelper(resolveParams);
27+
return resolveParams.query.count().exec();
28+
},
29+
});
30+
}

0 commit comments

Comments
 (0)