Skip to content

Commit f589fdc

Browse files
committed
fix: improve parseValue for MongoID salar type
1 parent 2e26806 commit f589fdc

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

flow-typed/npm/mongoose_v4.x.x.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55

66
import mongoose from "mongoose";
77

8-
type MongoId =
9-
| typeof mongoose.Types.ObjectId
10-
| {
11-
toString(): string
12-
};
13-
8+
type MongoId = ObjectId;
149
type MongoOrScalarId = MongoId | string | number;
1510

1611
type SchemaFields = {
@@ -63,10 +58,22 @@ type IndexOpts = {|
6358
weights?: Object
6459
|};
6560

61+
declare class ObjectId {
62+
constructor(id?: string | number | ObjectId): this,
63+
toHexString(): string,
64+
toString(): string,
65+
toJSON(): string,
66+
inspect(): string,
67+
equals(otherId: string | number | ObjectId): boolean;
68+
getTimestamp(): Date,
69+
70+
static createFromTime(time: number): ObjectId,
71+
static createFromHexString(str: string): ObjectId,
72+
static isValid(id: string | number | ObjectId): boolean,
73+
}
74+
6675
type Mongoose$Types = {|
67-
ObjectId: {
68-
$call: (id: string | MongoId) => MongoId
69-
},
76+
ObjectId: Class<ObjectId>,
7077
Mixed: Object,
7178
Embedded: Object,
7279
Document: Object,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* @flow */
2+
3+
import mongoose from 'mongoose';
4+
import { Kind } from 'graphql-compose/lib/graphql';
5+
import GraphQLMongoID from '../mongoid';
6+
7+
const ObjectId = mongoose.Types.ObjectId;
8+
9+
describe('GraphQLMongoID', () => {
10+
describe('serialize', () => {
11+
it('pass ObjectId', () => {
12+
const id = new ObjectId('5a0d77aa7e65a808ad24937f');
13+
expect(GraphQLMongoID.serialize(id)).toBe('5a0d77aa7e65a808ad24937f');
14+
});
15+
16+
it('pass String', () => {
17+
const id = '5a0d77aa7e65a808ad249000';
18+
expect(GraphQLMongoID.serialize(id)).toBe('5a0d77aa7e65a808ad249000');
19+
});
20+
});
21+
22+
describe('parseValue', () => {
23+
it('pass ObjectId', () => {
24+
const id = new ObjectId('5a0d77aa7e65a808ad24937f');
25+
expect(GraphQLMongoID.parseValue(id)).toBe(id);
26+
});
27+
28+
it('pass ObjectId as string', () => {
29+
const id = '5a0d77aa7e65a808ad249000';
30+
expect(GraphQLMongoID.parseValue(id)).toEqual(id);
31+
});
32+
33+
it('pass integer', () => {
34+
const id = 123;
35+
expect(GraphQLMongoID.parseValue(id)).toEqual(id);
36+
});
37+
38+
it('pass any custom string', () => {
39+
const id = 'custom_id';
40+
expect(GraphQLMongoID.parseValue(id)).toEqual(id);
41+
});
42+
});
43+
44+
describe('parseLiteral', () => {
45+
it('parse a ast STRING literal', async () => {
46+
const ast = {
47+
kind: Kind.STRING,
48+
value: '5a0d77aa7e65a808ad249000',
49+
};
50+
const id: any = GraphQLMongoID.parseLiteral(ast);
51+
expect(id).toEqual('5a0d77aa7e65a808ad249000');
52+
});
53+
54+
it('parse a ast INT literal', async () => {
55+
const ast: any = {
56+
kind: Kind.INT,
57+
value: 123,
58+
};
59+
const id: any = GraphQLMongoID.parseLiteral(ast);
60+
expect(id).toEqual(123);
61+
});
62+
});
63+
});

src/types/mongoid.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
/* @flow */
22

3+
import mongoose from 'mongoose';
34
import { GraphQLScalarType, Kind } from 'graphql-compose/lib/graphql';
45

6+
const ObjectId = mongoose.Types.ObjectId;
7+
58
const GraphQLMongoID = new GraphQLScalarType({
69
name: 'MongoID',
710
description:
@@ -10,7 +13,12 @@ const GraphQLMongoID = new GraphQLScalarType({
1013
'(https://docs.mongodb.com/manual/reference/bson-types/#objectid). ' +
1114
'But MongoDB also may accepts string or integer as correct values for _id field.',
1215
serialize: String,
13-
parseValue: String,
16+
parseValue(value: any) {
17+
if (!ObjectId.isValid(value) && typeof value !== 'string') {
18+
throw new TypeError('Field error: value is an invalid ObjectId');
19+
}
20+
return value;
21+
},
1422
parseLiteral(ast) {
1523
return ast.kind === Kind.STRING || ast.kind === Kind.INT ? ast.value : null;
1624
},

0 commit comments

Comments
 (0)