Skip to content

Commit

Permalink
feat(Resolvers): Move connection and pagination resolvers to opti…
Browse files Browse the repository at this point in the history
…onalDependencies. You may not install this packages if you do not use these resolvers.
  • Loading branch information
nodkz committed Aug 8, 2017
1 parent 5e08527 commit 8c55279
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose)
Installation
============
```
npm install graphql graphql-compose graphql-compose-connection graphql-compose-pagination mongoose graphql-compose-mongoose --save
npm install graphql graphql-compose mongoose graphql-compose-mongoose --save
```
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.

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.
```
npm install graphql-compose-connection graphql-compose-pagination --save
```
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.

Example
=======
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
"babel-runtime": "^6.23.0",
"object-path": "^0.11.4"
},
"optionalDependencies": {
"graphql-compose-connection": ">=2.2.1",
"graphql-compose-pagination": ">=1.0.0"
},
"peerDependencies": {
"graphql-compose": ">=1.20.3 || >=2.0.0",
"graphql-compose-connection": ">=2.2.1",
"graphql-compose-pagination": ">=1.0.0",
"mongoose": ">=4.0.0"
},
"devDependencies": {
Expand All @@ -50,8 +52,8 @@
"flow-bin": "^0.52.0",
"graphql": "^0.10.3",
"graphql-compose": "^2.0.0",
"graphql-compose-connection": "^2.2.2",
"graphql-compose-pagination": "^1.0.0",
"graphql-compose-connection": ">=2.2.1",
"graphql-compose-pagination": ">=1.0.0",
"jest": "^20.0.4",
"mongodb-memory-server": "^1.3.4",
"mongoose": "^4.10.7",
Expand Down
33 changes: 24 additions & 9 deletions src/composeWithMongoose.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* @flow */
/* eslint-disable no-use-before-define, no-param-reassign */
/* eslint-disable no-use-before-define, no-param-reassign, global-require */

import { TypeComposer, InputTypeComposer } from 'graphql-compose';
import composeWithConnection from 'graphql-compose-connection';
import composeWithPagination from 'graphql-compose-pagination';
import { convertModelToGraphQL } from './fieldsConverter';
import * as resolvers from './resolvers';
import { getUniqueIndexes, extendByReversedIndexes } from './utils/getIndexesFromModel';
Expand All @@ -14,6 +12,7 @@ import type {
TypeConverterResolversOpts,
TypeConverterInputTypeOpts,
ConnectionSortMapOpts,
PaginationOpts,
} from './definition';

export function composeWithMongoose(
Expand Down Expand Up @@ -125,20 +124,36 @@ export function createResolvers(
}

if (!{}.hasOwnProperty.call(opts, 'pagination') || opts.pagination !== false) {
const pOpts = opts.pagination || {};
composeWithPagination(typeComposer, {
findResolverName: 'findMany',
countResolverName: 'count',
...pOpts,
});
preparePaginationResolver(typeComposer, opts.pagination || {});
}
}

export function preparePaginationResolver(typeComposer: TypeComposer, opts: PaginationOpts) {
try {
require.resolve('graphql-compose-pagination');
} catch (e) {
return;
}
const composeWithPagination = require('graphql-compose-pagination').default;
composeWithPagination(typeComposer, {
findResolverName: 'findMany',
countResolverName: 'count',
...opts,
});
}

export function prepareConnectionResolver(
model: MongooseModelT,
typeComposer: TypeComposer,
opts: ConnectionSortMapOpts
) {
try {
require.resolve('graphql-compose-connection');
} catch (e) {
return;
}
const composeWithConnection = require('graphql-compose-connection').default;

const uniqueIndexes = extendByReversedIndexes(getUniqueIndexes(model), {
reversedFirst: true,
});
Expand Down
6 changes: 5 additions & 1 deletion src/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ export type TypeConverterResolversOpts = {
filter?: FilterHelperArgsOpts | false,
},
connection?: ConnectionSortMapOpts | false,
pagination?: { perPage?: number } | false,
pagination?: PaginationOpts | false,
};

export type PaginationOpts = {
perPage?: number
};

export type FilterHelperArgsOpts = {
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1898,13 +1898,13 @@ graceful-fs@^4.1.10, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.4
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"

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

graphql-compose-pagination@^1.0.0:
graphql-compose-pagination@>=1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/graphql-compose-pagination/-/graphql-compose-pagination-1.0.0.tgz#ae880d7ca2f22dc8337ac1b98f1628d17f944d3f"
dependencies:
Expand Down

0 comments on commit 8c55279

Please sign in to comment.