Skip to content

Commit 61e61ea

Browse files
authored
(MongoDB) ObjectID Scalar Type/Commit message convention (#243)
* feat(objectid-scalar): mongodb object id scalar type Adds mongodb object id scalar type. * style(editorconfig): editorconfig to help format code Adds [editorconfig](https://editorconfig.org/) to helps maintain coding style for developers. * style(commitlint): git commit message linting Enforces commit message standards on subsequent contributors to this project. * docs(mongodb-objectid): documents the usage of the mongodb objectid Documents the usage of the mongodb objectid scalar type. * feat(mongodb-objectid): mongodb objectid scalar type Adds mongodb objectid scalar type * feat(mongodb-objectid): mongodb objectid scalar type Adds a mongodb objectid scalar type definition to the already existing list of definitions. * conflict resolution (#1) package.json rebased * feat(objectid-scalar): mongodb object id scalar type Adds mongodb object id scalar type. * docs(mongodb-objectid): documents the usage of the mongodb objectid Documents the usage of the mongodb objectid scalar type. * git rebase (#2) conflict resolution -package.json * feat(objectid-scalar): mongodb object id scalar type Adds mongodb object id scalar type. * style(editorconfig): editorconfig to help format code Adds [editorconfig](https://editorconfig.org/) to helps maintain coding style for developers. * style(commitlint): git commit message linting Enforces commit message standards on subsequent contributors to this project. * docs(mongodb-objectid): documents the usage of the mongodb objectid Documents the usage of the mongodb objectid scalar type. * feat(mongodb-objectid): mongodb objectid scalar type Adds a mongodb objectid scalar type definition to the already existing list of definitions. * build: remove package-lock.json
1 parent cb4ab1f commit 61e61ea

File tree

11 files changed

+197
-14
lines changed

11 files changed

+197
-14
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## General
2+
[*]
3+
indent_style=space
4+
indent_size=2
5+
end_of_line=lf
6+
charset=utf-8
7+
trim_trailing_whitespace=true
8+
insert_final_newline=true

CONTRIBUTING.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Issues and Pull Requests are always welcome.
2+
3+
Please read OK Grow's global [contribution guidelines](https://github.com/okgrow/guides/blob/master/open-source/contributing.md).
4+
5+
If you are interested in becoming a maintainer, get in touch with us by sending an email or opening an issue. You should already have code merged into the project. Active contributors are encouraged to get in touch.
6+
7+
Please note that all interactions in @okgrow's repos should follow our [Code of Conduct](https://github.com/okgrow/guides/blob/master/open-source/CODE_OF_CONDUCT.md).
8+
9+
All commit messages should follow the [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) message guidelines. In order to assist with this you can run `npm run-scripts commit` as a command has been added to the scripts section of the `package.json` file which will help guide formulation of a proper commit message.

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ scalar UnsignedInt
5858

5959
scalar URL
6060

61+
scalar ObjectID
62+
6163
scalar BigInt
6264

6365
scalar Long
@@ -126,13 +128,16 @@ import {
126128
USCurrencyResolver,
127129
JSONResolver,
128130
JSONObjectResolver,
131+
ObjectIDResolver,
129132
} from 'graphql-scalars';
130133
```
131134

132135
Then make sure they're in the root resolver map like this:
133136

134137
```javascript
135138
const myResolverMap = {
139+
ObjectID: ObjectIDResolver,
140+
136141
DateTime: DateTimeResolver,
137142

138143
NonPositiveInt: NonPositiveIntResolver,
@@ -147,27 +152,27 @@ const myResolverMap = {
147152
UnsignedInt: UnsignedIntResolver,
148153
BigInt: BigIntResolver,
149154
Long: LongResolver,
150-
155+
151156
EmailAddress: EmailAddressResolver,
152157
URL: URLResolver,
153158
PhoneNumber: PhoneNumberResolver,
154159
PostalCode: PostalCodeResolver,
155-
160+
156161
GUID: GUIDResolver,
157-
162+
158163
HexColorCode: HexColorCodeResolver,
159164
HSL: HSLResolver,
160165
HSLA: HSLAResolver,
161166
RGB: RGBResolver,
162167
RGBA: RGBAResolver,
163-
168+
164169
IPv4: IPv4Resolver,
165170
IPv6: IPv6Resolver,
166171
MAC: MACResolver,
167172
Port: PortResolver,
168-
173+
169174
ISBN: ISBNResolver,
170-
175+
171176
USCurrency: USCurrencyResolver,
172177
JSON: JSONResolver,
173178
JSONObject: JSONObjectResolver,
@@ -482,6 +487,10 @@ The very powerful
482487
_that_ format, parse and display it in whatever display format you want. It can also be used to
483488
parse user input and _get_ the E.164 format to pass _into_ a schema.
484489

490+
### ObjectID
491+
492+
A field whose value conforms to the mongodb object id format as explained in the [documentation](https://docs.mongodb.com/manual/reference/method/ObjectId/#ObjectId)
493+
485494
### PostalCode
486495

487496
We're going to start with a limited set as suggested [here](http://www.pixelenvision.com/1708/zip-postal-code-validation-regex-php-code-for-12-countries/)
@@ -509,7 +518,7 @@ In the future we might expand this list and use the more comprehensive list foun
509518

510519
### BigInt
511520

512-
A long integer type for [graphql-js](https://github.com/graphql/graphql-js). This implementation gives you more than 32 bits rather than the default 32-bit GraphQLInt. [It uses native `BigInt` implementation of JavaScript.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
521+
A long integer type for [graphql-js](https://github.com/graphql/graphql-js). This implementation gives you more than 32 bits rather than the default 32-bit GraphQLInt. [It uses native `BigInt` implementation of JavaScript.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
513522
The [GraphQL spec](https://facebook.github.io/graphql/#sec-Int) limits its Int type to 32-bits. Maybe you've seen this error before:
514523
[Issue on graphql-js](https://github.com/graphql/graphql-js/issues/292)
515524
```
@@ -704,13 +713,13 @@ can use them too if needed.
704713

705714
## License
706715

707-
Released under the [MIT license](https://github.com/Urigo/graphql-scalars/blob/master/LICENSE).
716+
Released under the [MIT license](./LICENSE).
708717

709718
## Contributing
710719

711720
Issues and Pull Requests are always welcome.
712721

713-
Please read our [contribution guidelines](https://github.com/Urigo/graphql-scalars/blob/master/CONTRIBUTING.md).
722+
Please read our [contribution guidelines](./CONTRIBUTING.md).
714723

715724
## Thanks
716725

commitlint.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
};

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919
"src"
2020
]
2121
},
22+
"husky": {
23+
"hooks": {
24+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
25+
}
26+
},
2227
"prettier": {
2328
"singleQuote": true,
2429
"trailingComma": "all",
2530
"printWidth": 80
2631
},
2732
"dependencies": {},
2833
"scripts": {
34+
"commit": "commit",
2935
"clean": "rm -rf dist",
3036
"prebuild": "yarn clean",
3137
"prepack": "",
@@ -37,18 +43,23 @@
3743
"bundlesize": "yarn build && cd bundle-test/ && yarn && yarn test"
3844
},
3945
"devDependencies": {
40-
"@types/node": "13.9.0",
4146
"@ardatan/bob": "0.2.7",
47+
"@commitlint/cli": "8.3.5",
48+
"@commitlint/config-conventional": "8.3.4",
49+
"@commitlint/prompt-cli": "8.3.5",
50+
"@types/node": "13.9.0",
4251
"@types/jest": "25.1.4",
4352
"graphql": "14.6.0",
4453
"graphql-tools": "4.0.7",
54+
"husky": "4.2.3",
4555
"jest": "25.1.0",
4656
"lint-staged": "10.0.8",
4757
"merge-graphql-schemas": "1.7.6",
4858
"semver": "7.1.3",
4959
"ts-jest": "25.2.1",
5060
"tslint": "6.1.0",
51-
"typescript": "3.8.3"
61+
"typescript": "3.8.3",
62+
"yarn": "1.22.0"
5263
},
5364
"peerDependencies": {
5465
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
@@ -72,4 +83,4 @@
7283
"resolutions": {
7384
"set-value": "3.0.1"
7485
}
75-
}
86+
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export {
3232
JSON as JSONDefinition,
3333
JSONObject as JSONObjectDefinition,
3434
IBAN as IBANTypeDefinition,
35+
ObjectID as ObjectIDTypeDefinition
3536
} from './typeDefs';
3637

3738
export { default as typeDefs } from './typeDefs';
@@ -70,6 +71,7 @@ export {
7071
JSON as JSONResolver,
7172
JSONObject as JSONObjectResolver,
7273
IBAN as IBANResolver,
74+
ObjectID as ObjectIDResolver,
7375
} from './resolvers';
7476

7577
import * as resolvers from './resolvers';
@@ -111,6 +113,7 @@ export {
111113
JSON as JSONMock,
112114
JSONObject as JSONObjectMock,
113115
IBAN as IBANMock,
116+
ObjectID as ObjectIDMock
114117
} from './mocks';
115118

116119
export { mocks };

src/mocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const NonNegativeInt = () => 123;
88
export const NonPositiveFloat = () => -123.45;
99
export const NonPositiveInt = () => -123;
1010
export const PhoneNumber = () => '+17895551234';
11+
export const ObjectID = () => '5e5677d71bdc2ae76344968c';
1112
export const PositiveFloat = () => 123.45;
1213
export const PositiveInt = () => 123;
1314
export const PostalCode = () => '60031';

src/resolvers/ObjectID.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ValueNode } from 'graphql/language';
2+
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql';
3+
4+
const MONGODB_OBJECTID_REGEX: RegExp = new RegExp(/^[A-Fa-f0-9]{24}$/);
5+
6+
export default new GraphQLScalarType({
7+
name: 'ObjectID',
8+
9+
description:
10+
'A field whose value conforms with the standard mongodb object ID as described here: https://docs.mongodb.com/manual/reference/method/ObjectId/#ObjectId. Example: 5e5677d71bdc2ae76344968c',
11+
12+
serialize(value: string) {
13+
if (!MONGODB_OBJECTID_REGEX.test(value)) {
14+
throw new TypeError(`Value is not a valid mongodb object id of form: ${value}`);
15+
}
16+
17+
return value;
18+
},
19+
20+
parseValue(value: string) {
21+
if (!MONGODB_OBJECTID_REGEX.test(value)) {
22+
throw new TypeError(`Value is not a valid mongodb object id of form: ${value}`);
23+
}
24+
25+
return value;
26+
},
27+
28+
parseLiteral(ast: ValueNode) {
29+
if (ast.kind !== Kind.STRING) {
30+
throw new GraphQLError(
31+
`Can only validate strings as mongodb object id but got a: ${ast.kind}`,
32+
);
33+
}
34+
35+
if (!MONGODB_OBJECTID_REGEX.test(ast.value)) {
36+
throw new TypeError(`Value is not a valid mongodb object id of form: ${ast.value}`);
37+
}
38+
39+
return ast.value;
40+
},
41+
});

src/resolvers/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import RGBA from './RGBA';
2727
import USCurrency from './USCurrency';
2828
import { JSON, JSONObject } from './JSON';
2929
import IBAN from './IBAN';
30+
import ObjectID from './ObjectID';
3031

3132
const BigIntResolver = BigIntFactory('BigInt');
3233
const LongResolver = BigIntFactory('Long');
@@ -70,5 +71,6 @@ export {
7071
USCurrency,
7172
JSON,
7273
JSONObject,
73-
IBAN
74+
IBAN,
75+
ObjectID,
7476
};

src/typeDefs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export const USCurrency = `scalar USCurrency`;
3232
export const UnsignedFloat = 'scalar UnsignedFloat';
3333
export const UnsignedInt = 'scalar UnsignedInt';
3434
export const Long = 'scalar Long';
35+
export const ObjectID = 'scalar ObjectID';
3536

3637
export default [
3738
DateTime,
@@ -66,5 +67,6 @@ export default [
6667
USCurrency,
6768
JSON,
6869
JSONObject,
69-
IBAN
70+
IBAN,
71+
ObjectID,
7072
];

0 commit comments

Comments
 (0)