From 8c55279fefb262d28a0b9f0cd6f0b47420bd0db9 Mon Sep 17 00:00:00 2001 From: nodkz Date: Tue, 8 Aug 2017 14:48:03 +0600 Subject: [PATCH] feat(Resolvers): Move `connection` and `pagination` resolvers to optionalDependencies. You may not install this packages if you do not use these resolvers. --- README.md | 9 +++++++-- package.json | 10 ++++++---- src/composeWithMongoose.js | 33 ++++++++++++++++++++++++--------- src/definition.js | 6 +++++- yarn.lock | 4 ++-- 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2ccca405..6f425c15 100644 --- a/README.md +++ b/README.md @@ -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 ======= diff --git a/package.json b/package.json index 26472144..3d9f1fda 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/composeWithMongoose.js b/src/composeWithMongoose.js index c224eb55..540d6d9d 100644 --- a/src/composeWithMongoose.js +++ b/src/composeWithMongoose.js @@ -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'; @@ -14,6 +12,7 @@ import type { TypeConverterResolversOpts, TypeConverterInputTypeOpts, ConnectionSortMapOpts, + PaginationOpts, } from './definition'; export function composeWithMongoose( @@ -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, }); diff --git a/src/definition.js b/src/definition.js index 78a86273..1b718766 100644 --- a/src/definition.js +++ b/src/definition.js @@ -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 = { diff --git a/yarn.lock b/yarn.lock index 2e7f6585..79eda870 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: