Skip to content

Commit

Permalink
feat: add support for schemaComposer, fix flowtype errors
Browse files Browse the repository at this point in the history
BREAKING CHANGE: required min version of graphql-compose@3.0.0
  • Loading branch information
nodkz committed Feb 15, 2018
1 parent d2d9be6 commit 7863d44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"homepage": "https://github.com/graphql-compose/graphql-compose-pagination",
"peerDependencies": {
"graphql-compose": ">=2.9.5 || >=3.0.0"
"graphql-compose": ">=3.0.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand Down
56 changes: 31 additions & 25 deletions src/paginationResolver.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
/* @flow */
/* eslint-disable no-param-reassign, no-use-before-define */

import { Resolver, TypeComposer } from 'graphql-compose';
import type { ResolveParams, ProjectionType } from 'graphql-compose';
import type {
Resolver,
TypeComposer,
ResolveParams, // eslint-disable-line
ProjectionType,
} from 'graphql-compose';
import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
import type { ComposeWithPaginationOpts } from './composeWithPagination';
import preparePaginationType from './types/paginationType';

const DEFAULT_PER_PAGE = 20;

export type PaginationResolveParams<TSource, TContext> = {
source: TSource,
export type PaginationResolveParams<TContext> = {
source: any,
args: {
page?: ?number,
perPage?: ?number,
Expand Down Expand Up @@ -39,24 +43,24 @@ export type PaginationInfoType = {|
hasNextPage: boolean,
|};

export function preparePaginationResolver<TSource, TContext>(
typeComposer: TypeComposer,
export function preparePaginationResolver(
tc: TypeComposer,
opts: ComposeWithPaginationOpts
): Resolver {
if (!typeComposer || typeComposer.constructor.name !== 'TypeComposer') {
if (!tc || tc.constructor.name !== 'TypeComposer') {
throw new Error('First arg for prepareConnectionResolver() should be instance of TypeComposer');
}

if (!opts.countResolverName) {
throw new Error(
`TypeComposer(${typeComposer.getTypeName()}) provided to composeWithConnection ` +
`TypeComposer(${tc.getTypeName()}) provided to composeWithConnection ` +
'should have option `opts.countResolverName`.'
);
}
const countResolver = typeComposer.getResolver(opts.countResolverName);
const countResolver = tc.getResolver(opts.countResolverName);
if (!countResolver) {
throw new Error(
`TypeComposer(${typeComposer.getTypeName()}) provided to composeWithConnection ` +
`TypeComposer(${tc.getTypeName()}) provided to composeWithConnection ` +
`should have resolver with name '${opts.countResolverName}' ` +
'due opts.countResolverName.'
);
Expand All @@ -65,14 +69,14 @@ export function preparePaginationResolver<TSource, TContext>(

if (!opts.findResolverName) {
throw new Error(
`TypeComposer(${typeComposer.getTypeName()}) provided to composeWithConnection ` +
`TypeComposer(${tc.getTypeName()}) provided to composeWithConnection ` +
'should have option `opts.findResolverName`.'
);
}
const findManyResolver = typeComposer.getResolver(opts.findResolverName);
const findManyResolver = tc.getResolver(opts.findResolverName);
if (!findManyResolver) {
throw new Error(
`TypeComposer(${typeComposer.getTypeName()}) provided to composeWithConnection ` +
`TypeComposer(${tc.getTypeName()}) provided to composeWithConnection ` +
`should have resolver with name '${opts.findResolverName}' ` +
'due opts.countResolverName.'
);
Expand All @@ -93,8 +97,8 @@ export function preparePaginationResolver<TSource, TContext>(
}
}

return new Resolver({
type: preparePaginationType(typeComposer),
return new tc.constructor.schemaComposer.Resolver({
type: preparePaginationType(tc),
name: 'pagination',
kind: 'query',
args: {
Expand All @@ -110,12 +114,14 @@ export function preparePaginationResolver<TSource, TContext>(
...(additionalArgs: any),
},
// eslint-disable-next-line
resolve: async (resolveParams: $Shape<PaginationResolveParams<TSource, TContext>>) => {
resolve: async /* :: <TContext> */(
rp /* : $Shape<PaginationResolveParams<TContext>> */
) => {
let countPromise;
let findManyPromise;
const { projection = {}, args, rawQuery } = resolveParams;
const findManyParams: $Shape<ResolveParams<TSource, TContext>> = {
...resolveParams,
const { projection = {}, args, rawQuery } = rp;
const findManyParams /* : $Shape<ResolveParams<any, TContext>> */ = {
...rp,
};

const page = parseInt(args.page, 10) || 1;
Expand All @@ -127,11 +133,11 @@ export function preparePaginationResolver<TSource, TContext>(
throw new Error('Argument `perPage` should be positive number.');
}

const countParams: $Shape<ResolveParams<TSource, TContext>> = {
...resolveParams,
const countParams /* : $Shape<ResolveParams<any, TContext>> */ = {
...rp,
rawQuery,
args: {
filter: { ...resolveParams.args.filter },
filter: { ...rp.args.filter },
},
};

Expand All @@ -146,7 +152,7 @@ export function preparePaginationResolver<TSource, TContext>(

if (projection && projection.items) {
// combine top level projection
// (maybe somebody add additional fields via resolveParams.projection)
// (maybe somebody add additional fields via rp.projection)
// and items (record needed fields)
findManyParams.projection = { ...projection, ...projection.items };
} else {
Expand All @@ -162,8 +168,8 @@ export function preparePaginationResolver<TSource, TContext>(
}

// pass findMany ResolveParams to top resolver
resolveParams.findManyResolveParams = findManyParams;
resolveParams.countResolveParams = countParams;
rp.findManyResolveParams = findManyParams;
rp.countResolveParams = countParams;

// This allows to optimize and not actually call the findMany resolver
// if only the count is projected
Expand Down

0 comments on commit 7863d44

Please sign in to comment.