Skip to content

Commit 8c55279

Browse files
committed
feat(Resolvers): Move connection and pagination resolvers to optionalDependencies. You may not install this packages if you do not use these resolvers.
1 parent 5e08527 commit 8c55279

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose)
1414
Installation
1515
============
1616
```
17-
npm install graphql graphql-compose graphql-compose-connection graphql-compose-pagination mongoose graphql-compose-mongoose --save
17+
npm install graphql graphql-compose mongoose graphql-compose-mongoose --save
18+
```
19+
Modules `graphql`, `graphql-compose`, `mongoose` are in `peerDependencies`, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
20+
21+
If you want to add additional resolvers [`connection`](https://github.com/nodkz/graphql-compose-connection) and/or [`pagination`](https://github.com/nodkz/graphql-compose-pagination) - just install following packages and `graphql-compose-mongoose` will add them automatically.
22+
```
23+
npm install graphql-compose-connection graphql-compose-pagination --save
1824
```
19-
Modules `graphql`, `graphql-compose`, `graphql-compose-connection`, `graphql-compose-pagination`, `mongoose` are in `peerDependencies`, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.
2025

2126
Example
2227
=======

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
"babel-runtime": "^6.23.0",
2727
"object-path": "^0.11.4"
2828
},
29+
"optionalDependencies": {
30+
"graphql-compose-connection": ">=2.2.1",
31+
"graphql-compose-pagination": ">=1.0.0"
32+
},
2933
"peerDependencies": {
3034
"graphql-compose": ">=1.20.3 || >=2.0.0",
31-
"graphql-compose-connection": ">=2.2.1",
32-
"graphql-compose-pagination": ">=1.0.0",
3335
"mongoose": ">=4.0.0"
3436
},
3537
"devDependencies": {
@@ -50,8 +52,8 @@
5052
"flow-bin": "^0.52.0",
5153
"graphql": "^0.10.3",
5254
"graphql-compose": "^2.0.0",
53-
"graphql-compose-connection": "^2.2.2",
54-
"graphql-compose-pagination": "^1.0.0",
55+
"graphql-compose-connection": ">=2.2.1",
56+
"graphql-compose-pagination": ">=1.0.0",
5557
"jest": "^20.0.4",
5658
"mongodb-memory-server": "^1.3.4",
5759
"mongoose": "^4.10.7",

src/composeWithMongoose.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* @flow */
2-
/* eslint-disable no-use-before-define, no-param-reassign */
2+
/* eslint-disable no-use-before-define, no-param-reassign, global-require */
33

44
import { TypeComposer, InputTypeComposer } from 'graphql-compose';
5-
import composeWithConnection from 'graphql-compose-connection';
6-
import composeWithPagination from 'graphql-compose-pagination';
75
import { convertModelToGraphQL } from './fieldsConverter';
86
import * as resolvers from './resolvers';
97
import { getUniqueIndexes, extendByReversedIndexes } from './utils/getIndexesFromModel';
@@ -14,6 +12,7 @@ import type {
1412
TypeConverterResolversOpts,
1513
TypeConverterInputTypeOpts,
1614
ConnectionSortMapOpts,
15+
PaginationOpts,
1716
} from './definition';
1817

1918
export function composeWithMongoose(
@@ -125,20 +124,36 @@ export function createResolvers(
125124
}
126125

127126
if (!{}.hasOwnProperty.call(opts, 'pagination') || opts.pagination !== false) {
128-
const pOpts = opts.pagination || {};
129-
composeWithPagination(typeComposer, {
130-
findResolverName: 'findMany',
131-
countResolverName: 'count',
132-
...pOpts,
133-
});
127+
preparePaginationResolver(typeComposer, opts.pagination || {});
134128
}
135129
}
136130

131+
export function preparePaginationResolver(typeComposer: TypeComposer, opts: PaginationOpts) {
132+
try {
133+
require.resolve('graphql-compose-pagination');
134+
} catch (e) {
135+
return;
136+
}
137+
const composeWithPagination = require('graphql-compose-pagination').default;
138+
composeWithPagination(typeComposer, {
139+
findResolverName: 'findMany',
140+
countResolverName: 'count',
141+
...opts,
142+
});
143+
}
144+
137145
export function prepareConnectionResolver(
138146
model: MongooseModelT,
139147
typeComposer: TypeComposer,
140148
opts: ConnectionSortMapOpts
141149
) {
150+
try {
151+
require.resolve('graphql-compose-connection');
152+
} catch (e) {
153+
return;
154+
}
155+
const composeWithConnection = require('graphql-compose-connection').default;
156+
142157
const uniqueIndexes = extendByReversedIndexes(getUniqueIndexes(model), {
143158
reversedFirst: true,
144159
});

src/definition.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ export type TypeConverterResolversOpts = {
186186
filter?: FilterHelperArgsOpts | false,
187187
},
188188
connection?: ConnectionSortMapOpts | false,
189-
pagination?: { perPage?: number } | false,
189+
pagination?: PaginationOpts | false,
190+
};
191+
192+
export type PaginationOpts = {
193+
perPage?: number
190194
};
191195

192196
export type FilterHelperArgsOpts = {

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,13 +1898,13 @@ graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4
18981898
version "1.0.1"
18991899
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
19001900

1901-
graphql-compose-connection@^2.2.2:
1901+
graphql-compose-connection@>=2.2.1:
19021902
version "2.2.2"
19031903
resolved "https://registry.yarnpkg.com/graphql-compose-connection/-/graphql-compose-connection-2.2.2.tgz#16bc70feade9a127c672ef96fd7a4974bcd0559a"
19041904
dependencies:
19051905
babel-runtime "^6.23.0"
19061906

1907-
graphql-compose-pagination@^1.0.0:
1907+
graphql-compose-pagination@>=1.0.0:
19081908
version "1.0.0"
19091909
resolved "https://registry.yarnpkg.com/graphql-compose-pagination/-/graphql-compose-pagination-1.0.0.tgz#ae880d7ca2f22dc8337ac1b98f1628d17f944d3f"
19101910
dependencies:

0 commit comments

Comments
 (0)