Skip to content

Commit caddfcd

Browse files
committed
refactor: rewrite code according to a new features of graphql-compose@3
- add support for `schemaComposer` - rewrite resolvers on async/await syntax - bunch of code/tests cleanups - add `node8` and `es` build for modern environments (use native async/await) BREAKING CHANGE: works only with `graphql-compose` v3 BREAKING CHANGE: `removeById` will not throw error if record not found BREAKING CHANGE: drop node v4 support
1 parent 42fa0b1 commit caddfcd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1333
-1578
lines changed

.babelrc

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,44 @@
22
"plugins": [
33
"transform-object-rest-spread",
44
"transform-flow-strip-types",
5-
["transform-runtime", { "polyfill": false }]
6-
],
7-
"presets": [
8-
["env", {
9-
"targets": {
10-
"node": 4
11-
},
12-
}]
135
],
6+
"env": {
7+
"lib": {
8+
"plugins": [
9+
["transform-runtime", { "polyfill": false }]
10+
],
11+
"presets": [
12+
["env", {
13+
"targets": {
14+
"node": 6
15+
},
16+
}]
17+
]
18+
},
19+
"es": {
20+
},
21+
"node8": {
22+
"plugins": [
23+
["transform-runtime", { "polyfill": false }]
24+
],
25+
"presets": [
26+
["env", {
27+
"targets": {
28+
"node": "8.0.0"
29+
},
30+
"loose": true,
31+
"modules": "commonjs"
32+
}]
33+
]
34+
},
35+
"test": {
36+
"presets": [
37+
["env", {
38+
"targets": {
39+
"node": "current"
40+
},
41+
}]
42+
]
43+
}
44+
}
1445
}

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
lib/*
2+
es/*
3+
node8/*
24
flow-typed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ node_modules
4141
# Transpiled code
4242
/es
4343
/lib
44+
/node8
4445

4546
coverage
4647
.nyc_output

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ If you want to add additional resolvers [`connection`](https://github.com/nodkz/
2323
npm install graphql-compose-connection graphql-compose-pagination --save
2424
```
2525

26+
#### Different builds
27+
This library contains different builds for any purposes:
28+
```js
29+
// Default import for using under node v6 and above
30+
import { composeWithMongoose } from 'graphql-compose-mongoose';
31+
32+
// For using node 8 and above (native async/await)
33+
import { composeWithMongoose } from 'graphql-compose-mongoose/node8';
34+
35+
// Source code without Flowtype declarations
36+
import { composeWithMongoose } from 'graphql-compose-mongoose/es';
37+
```
38+
2639
Example
2740
=======
2841
Live demo: [https://graphql-compose.herokuapp.com/](https://graphql-compose.herokuapp.com/)
@@ -31,7 +44,7 @@ Source code: https://github.com/nodkz/graphql-compose-mongoose-example
3144

3245
```js
3346
import mongoose from 'mongoose';
34-
import composeWithMongoose from 'graphql-compose-mongoose';
47+
import { composeWithMongoose } from 'graphql-compose-mongoose';
3548
import { GQC } from 'graphql-compose';
3649

3750
// STEP 1: DEFINE MONGOOSE SCHEMA AND MODEL

package.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"graphql-compose-pagination": ">=1.1.5"
3232
},
3333
"peerDependencies": {
34-
"graphql-compose": ">=2.9.0 || >=3.0.0",
34+
"graphql-compose": ">=3.0.2",
3535
"mongoose": ">=4.0.0 || >=5.0.0"
3636
},
3737
"devDependencies": {
@@ -47,17 +47,17 @@
4747
"eslint": "^4.17.0",
4848
"eslint-config-airbnb-base": "^12.1.0",
4949
"eslint-config-prettier": "^2.9.0",
50-
"eslint-plugin-flowtype": "^2.43.0",
50+
"eslint-plugin-flowtype": "^2.44.0",
5151
"eslint-plugin-import": "^2.8.0",
5252
"eslint-plugin-prettier": "^2.6.0",
5353
"flow-bin": "^0.65.0",
5454
"graphql": "0.13.0",
55-
"graphql-compose": "^3.0.0-beta.5",
55+
"graphql-compose": "^3.0.2",
5656
"graphql-compose-connection": ">=2.5.7",
5757
"graphql-compose-pagination": ">=1.1.5",
58-
"jest": "^22.2.2",
58+
"jest": "^22.3.0",
5959
"mongodb-memory-server": "^1.6.5",
60-
"mongoose": "^5.0.4",
60+
"mongoose": "^5.0.5",
6161
"prettier": "^1.10.2",
6262
"request": "^2.83.0",
6363
"rimraf": "^2.6.2",
@@ -74,9 +74,11 @@
7474
]
7575
},
7676
"scripts": {
77-
"build": "npm run build-cjs && npm run build-flow",
78-
"build-cjs": "rimraf lib && babel src --ignore __tests__,__mocks__ -d lib",
79-
"build-flow": "find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/lib\\//g'`.flow; done",
77+
"build": "npm run build-lib && npm run build-es && npm run build-node8",
78+
"build-lib": "rimraf lib && BABEL_ENV=lib babel src --ignore __tests__,__mocks__ -d lib && COPY_TO_FOLDER=lib npm run build-flow",
79+
"build-es": "rimraf es && BABEL_ENV=es babel src --ignore __tests__,__mocks__ -d es && COPY_TO_FOLDER=es npm run build-flow",
80+
"build-node8": "rimraf node8 && BABEL_ENV=node8 babel src --ignore __tests__,__mocks__ -d node8 && COPY_TO_FOLDER=node8 npm run build-flow",
81+
"build-flow": "echo `$1` && find ./src -name '*.js' -not -path '*/__*' | while read filepath; do cp $filepath `echo ./${COPY_TO_FOLDER:-lib}$filepath | sed 's/.\\/src\\//\\//g'`.flow; done",
8082
"watch": "jest --watch",
8183
"coverage": "jest --coverage --maxWorkers 2",
8284
"lint": "eslint --ext .js ./src",

src/__mocks__/languageSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

3+
import { schemaComposer } from 'graphql-compose';
34
import { Schema } from './mongooseCommon';
45
import { convertSchemaToGraphQL } from '../fieldsConverter';
56

@@ -34,4 +35,4 @@ export default LanguageSchema;
3435

3536
// Such way we can set Type name for Schema which is used in another schema.
3637
// Otherwise by default it will have name `${ParentSchemaName}${ParentSchemaFieldName}`
37-
convertSchemaToGraphQL(LanguageSchema, 'Language');
38+
convertSchemaToGraphQL(LanguageSchema, 'Language', schemaComposer);

src/__tests__/composeWithMongoose-test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22
/* eslint-disable no-unused-expressions */
33

44
import mongoose from 'mongoose';
5-
import { TypeComposer, InputTypeComposer } from 'graphql-compose';
5+
import { TypeComposer, InputTypeComposer, schemaComposer } from 'graphql-compose';
66
import { GraphQLNonNull } from 'graphql-compose/lib/graphql';
77
import { UserModel } from '../__mocks__/userModel';
88
import { composeWithMongoose } from '../composeWithMongoose';
9-
import typeStorage from '../typeStorage';
109
import GraphQLMongoID from '../types/mongoid';
1110

1211
beforeAll(() => UserModel.base.connect());
1312
afterAll(() => UserModel.base.disconnect());
1413

1514
describe('composeWithMongoose ->', () => {
1615
beforeEach(() => {
17-
typeStorage.clear();
16+
schemaComposer.clear();
1817
UserModel.schema._gqcTypeComposer = undefined;
1918
});
2019

21-
describe('MongooseModeloTypeComposer()', () => {
20+
describe('MongooseModelToTypeComposer()', () => {
2221
describe('basics', () => {
2322
it('should return TypeComposer', () => {
2423
expect(composeWithMongoose(UserModel)).toBeInstanceOf(TypeComposer);

0 commit comments

Comments
 (0)