Skip to content

Commit 6c9e727

Browse files
committed
Add mongoid graphql type. Fix tests.
1 parent 7b5d71a commit 6c9e727

File tree

7 files changed

+74
-45
lines changed

7 files changed

+74
-45
lines changed

src/fieldsConverter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
GraphQLString,
1010
GraphQLFloat,
1111
GraphQLBoolean,
12-
GraphQLID,
1312
GraphQLList,
1413
GraphQLEnumType,
1514
GraphQLObjectType,
1615
} from 'graphql/type';
16+
import GraphQLMongoID from './types/mongoid';
1717

1818
import {
1919
GraphQLDate,
@@ -203,7 +203,7 @@ function removePseudoIdField(gqType: GraphQLOutputType): GraphQLOutputType {
203203
const composer = new TypeComposer(gqType);
204204
const gqFields = composer.getFields();
205205
Object.keys(gqFields).forEach(name => {
206-
if (gqFields[name].type === GraphQLID) {
206+
if (gqFields[name].type === GraphQLMongoID) {
207207
composer.removeField(name);
208208
}
209209
});
@@ -220,7 +220,7 @@ export function scalarToGraphQL(field: MongooseFieldT): GraphQLOutputType {
220220
case 'Date': return GraphQLDate;
221221
case 'Buffer': return GraphQLBuffer;
222222
case 'Boolean': return GraphQLBoolean;
223-
case 'ObjectID': return GraphQLID;
223+
case 'ObjectID': return GraphQLMongoID;
224224
default: return GraphQLGeneric;
225225
}
226226
}

src/resolvers/__tests__/findById-test.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { expect } from 'chai';
44
import { UserModel } from '../../__mocks__/userModel.js';
55
import findById from '../findById';
66
import Resolver from '../../../../graphql-compose/src/resolver/resolver';
7-
import { GraphQLNonNull, GraphQLID } from 'graphql';
7+
import InputTypeComposer from '../../../../graphql-compose/src/typeInputComposer';
8+
import { GraphQLNonNull } from 'graphql';
9+
import GraphQLMongoID from '../../types/mongoid';
810

911
describe('findById() ->', () => {
1012
let user;
@@ -23,12 +25,14 @@ describe('findById() ->', () => {
2325
expect(resolver).to.be.instanceof(Resolver);
2426
});
2527

26-
it('Resolver object should have non-null `id` arg', () => {
27-
const resolver = findById(UserModel);
28-
expect(resolver.hasArg('id')).to.be.true;
29-
const argConfig = resolver.getArg('id');
30-
expect(argConfig.type).to.be.instanceof(GraphQLNonNull);
31-
expect(argConfig.type.ofType).to.equal(GraphQLID);
28+
describe('Resolver.args', () => {
29+
it('should have non-null `id` arg', () => {
30+
const resolver = findById(UserModel);
31+
expect(resolver.hasArg('_id')).to.be.true;
32+
const argConfig = resolver.getArg('_id');
33+
expect(argConfig).property('type').that.instanceof(GraphQLNonNull);
34+
expect(argConfig).deep.property('type.ofType').that.equal(GraphQLMongoID);
35+
});
3236
});
3337

3438
describe('Resolver.resolve():Promise', () => {
@@ -38,7 +42,7 @@ describe('findById() ->', () => {
3842
});
3943

4044
it('should be rejected if args.id is not objectId', async () => {
41-
const result = findById(UserModel).resolve({ args: { id: 1 } });
45+
const result = findById(UserModel).resolve({ args: { _id: 1 } });
4246
await expect(result).be.rejected;
4347
});
4448

@@ -48,7 +52,7 @@ describe('findById() ->', () => {
4852
});
4953

5054
it('should return document if provided existed id', async () => {
51-
const result = await findById(UserModel).resolve({ args: { id: user._id } });
55+
const result = await findById(UserModel).resolve({ args: { _id: user._id } });
5256
expect(result).have.property('name').that.equal(user.name);
5357
});
5458
});

src/resolvers/__tests__/findByIds-test.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { expect } from 'chai';
44
import { UserModel } from '../../__mocks__/userModel.js';
55
import findByIds from '../findByIds';
66
import Resolver from '../../../../graphql-compose/src/resolver/resolver';
7-
import { GraphQLNonNull, GraphQLID, GraphQLList, GraphQLObjectType } from 'graphql';
7+
import InputTypeComposer from '../../../../graphql-compose/src/typeInputComposer';
8+
import { GraphQLNonNull, GraphQLList, GraphQLObjectType } from 'graphql';
9+
import GraphQLMongoID from '../../types/mongoid';
810

911
const UserType = new GraphQLObjectType({
1012
name: 'MockUserType',
@@ -36,23 +38,25 @@ describe('findByIds() ->', () => {
3638
expect(resolver).to.be.instanceof(Resolver);
3739
});
3840

39-
it('should have non-null `ids` arg', () => {
40-
const resolver = findByIds(UserModel, UserType);
41-
expect(resolver.hasArg('ids')).to.be.true;
42-
const argConfig = resolver.getArg('ids');
43-
expect(argConfig.type).to.be.instanceof(GraphQLNonNull);
44-
expect(argConfig.type.ofType).to.be.instanceof(GraphQLList);
45-
expect(argConfig.type.ofType.ofType).to.equal(GraphQLID);
46-
});
41+
describe('Resolver.args', () => {
42+
it('should have non-null `_ids` arg', () => {
43+
const resolver = findByIds(UserModel, UserType);
44+
expect(resolver.hasArg('_ids')).to.be.true;
45+
const argConfig = resolver.getArg('_ids');
46+
expect(argConfig).property('type').that.instanceof(GraphQLNonNull);
47+
expect(argConfig).deep.property('type.ofType').that.instanceof(GraphQLList);
48+
expect(argConfig).deep.property('type.ofType.ofType').that.equal(GraphQLMongoID);
49+
});
4750

48-
it('Resolver object should have `limit` arg', () => {
49-
const resolver = findByIds(UserModel, UserType);
50-
expect(resolver.hasArg('limit')).to.be.true;
51-
});
51+
it('should have `limit` arg', () => {
52+
const resolver = findByIds(UserModel, UserType);
53+
expect(resolver.hasArg('limit')).to.be.true;
54+
});
5255

53-
it('Resolver object should have `sort` arg', () => {
54-
const resolver = findByIds(UserModel, UserType);
55-
expect(resolver.hasArg('sort')).to.be.true;
56+
it('should have `sort` arg', () => {
57+
const resolver = findByIds(UserModel, UserType);
58+
expect(resolver.hasArg('sort')).to.be.true;
59+
});
5660
});
5761

5862
describe('Resolver.resolve():Promise', () => {
@@ -61,21 +65,21 @@ describe('findByIds() ->', () => {
6165
await expect(result).be.fulfilled;
6266
});
6367

64-
it('should return empty array if args.ids is empty', async () => {
68+
it('should return empty array if args._ids is empty', async () => {
6569
const result = await findByIds(UserModel, UserType).resolve({});
6670
expect(result).to.be.instanceOf(Array);
6771
expect(result).to.be.empty;
6872
});
6973

70-
it('should return empty array if args.ids is not valid objectIds', async () => {
71-
const result = await findByIds(UserModel, UserType).resolve({ args: { ids: ['d', 'e'] } });
74+
it('should return empty array if args._ids is not valid objectIds', async () => {
75+
const result = await findByIds(UserModel, UserType).resolve({ args: { _ids: ['d', 'e'] } });
7276
expect(result).to.be.instanceOf(Array);
7377
expect(result).to.be.empty;
7478
});
7579

7680
it('should return array of documents', async () => {
7781
const result = await findByIds(UserModel, UserType)
78-
.resolve({ args: { ids: [user1._id, user2._id, user3._id] } });
82+
.resolve({ args: { _ids: [user1._id, user2._id, user3._id] } });
7983

8084
expect(result).to.be.instanceOf(Array);
8185
expect(result).to.have.lengthOf(3);
@@ -86,7 +90,7 @@ describe('findByIds() ->', () => {
8690
it('should return array of documents if object id is string', async () => {
8791
const stringId = `${user1._id}`;
8892
const result = await findByIds(UserModel, UserType)
89-
.resolve({ args: { ids: [stringId] } });
93+
.resolve({ args: { _ids: [stringId] } });
9094

9195
expect(result).to.be.instanceOf(Array);
9296
expect(result).to.have.lengthOf(1);

src/resolvers/findById.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Resolver from '../../../graphql-compose/src/resolver/resolver';
99

1010
import {
1111
GraphQLNonNull,
12-
GraphQLID,
1312
} from 'graphql';
13+
import GraphQLMongoID from '../types/mongoid';
1414

1515
import { projectionHelper } from './helpers/projection';
1616

@@ -20,16 +20,16 @@ export default function findById(model: MongooseModelT, gqType: GraphQLObjectTyp
2020
name: 'findById',
2121
kind: 'query',
2222
args: {
23-
id: {
24-
name: 'id',
25-
type: new GraphQLNonNull(GraphQLID),
23+
_id: {
24+
name: '_id',
25+
type: new GraphQLNonNull(GraphQLMongoID),
2626
},
2727
},
2828
resolve: (resolveParams : ExtendedResolveParams = {}) => {
2929
const args = resolveParams.args || {};
3030

31-
if (args.id) {
32-
resolveParams.query = model.findById(args.id); // eslint-disable-line
31+
if (args._id) {
32+
resolveParams.query = model.findById(args._id); // eslint-disable-line
3333
projectionHelper(resolveParams);
3434
return resolveParams.query.exec();
3535
}

src/resolvers/findByIds.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import mongoose from 'mongoose';
1111
import {
1212
GraphQLNonNull,
1313
GraphQLList,
14-
GraphQLID,
1514
} from 'graphql';
15+
import GraphQLMongoID from '../types/mongoid';
1616

1717
import { limitHelperArgs, limitHelper } from './helpers/limit';
1818
import { sortHelperArgsGen, sortHelper } from './helpers/sort';
@@ -24,9 +24,9 @@ export default function findByIds(model: MongooseModelT, gqType: GraphQLObjectTy
2424
name: 'findByIds',
2525
kind: 'query',
2626
args: {
27-
ids: {
28-
name: 'ids',
29-
type: new GraphQLNonNull(new GraphQLList(GraphQLID)),
27+
_ids: {
28+
name: '_ids',
29+
type: new GraphQLNonNull(new GraphQLList(GraphQLMongoID)),
3030
},
3131
...limitHelperArgs,
3232
...sortHelperArgsGen(model, {
@@ -37,9 +37,9 @@ export default function findByIds(model: MongooseModelT, gqType: GraphQLObjectTy
3737
const args = resolveParams.args || {};
3838

3939
const selector = {};
40-
if (Array.isArray(args.ids)) {
40+
if (Array.isArray(args._ids)) {
4141
selector._id = {
42-
$in: args.ids
42+
$in: args._ids
4343
.filter(id => mongoose.Types.ObjectId.isValid(id))
4444
.map(id => mongoose.Types.ObjectId(id)), // eslint-disable-line
4545
};

src/resolvers/findMany.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Resolver from '../../../graphql-compose/src/resolver/resolver';
1010
import {
1111
GraphQLList,
1212
} from 'graphql';
13+
import GraphQLMongoID from '../types/mongoid';
1314

1415
import { skipHelperArgs, skipHelper } from './helpers/skip';
1516
import { limitHelperArgs, limitHelper } from './helpers/limit';

src/types/mongoid.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { GraphQLScalarType } from 'graphql/type';
2+
import { Kind } from 'graphql/language';
3+
4+
const GraphQLMongoID = new GraphQLScalarType({
5+
name: 'MongoID',
6+
description:
7+
'The `ID` scalar type represents a unique MongoDB identifier in collection.' +
8+
'MongoDB by default use 12-byte ObjectId value ' +
9+
'(https://docs.mongodb.com/manual/reference/bson-types/#objectid).' +
10+
'But also it accepts string or integer as correct values for _id field.',
11+
serialize: String,
12+
parseValue: String,
13+
parseLiteral(ast) {
14+
return ast.kind === Kind.STRING || ast.kind === Kind.INT ?
15+
ast.value :
16+
null;
17+
},
18+
});
19+
20+
export default GraphQLMongoID;

0 commit comments

Comments
 (0)