Skip to content

Commit

Permalink
feat: use existed PaginationInfo type if it's already present in sc…
Browse files Browse the repository at this point in the history
…hemaComposer
  • Loading branch information
nodkz committed May 31, 2018
1 parent 0945eab commit 33d30f7
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 264 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": ">=4.0.0 || >=3.0.0"
"graphql-compose": ">=4.0.0 || >=3.0.2"
},
"devDependencies": {
"babel-cli": "^6.26.0",
Expand Down
64 changes: 20 additions & 44 deletions src/__mocks__/userTypeComposer.js → src/__mocks__/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,16 @@
/* eslint-disable no-param-reassign */

import { TypeComposer, Resolver } from 'graphql-compose';
import {
GraphQLString,
GraphQLObjectType,
GraphQLInputObjectType,
GraphQLEnumType,
GraphQLInt,
} from 'graphql-compose/lib/graphql';

export const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: {
type: GraphQLInt,
},
name: {
type: GraphQLString,
},
age: {
type: GraphQLInt,
},
gender: {
type: GraphQLString,
},
},
});

export const userTypeComposer = new TypeComposer(UserType);
import { GraphQLEnumType } from 'graphql-compose/lib/graphql';

export const UserTC = TypeComposer.create(`
type User {
id: Int
name: String
age: Int
gender: String
}
`);

export const userList = [
{ id: 1, name: 'user01', age: 11, gender: 'm' },
Expand All @@ -50,17 +33,10 @@ export const userList = [

const filterArgConfig = {
name: 'filter',
type: new GraphQLInputObjectType({
name: 'FilterUserInput',
fields: {
gender: {
type: GraphQLString,
},
age: {
type: GraphQLInt,
},
},
}),
type: `input FilterUserInput {
gender: String
age: Int
}`,
};

function filteredUserList(list, filter = {}) {
Expand Down Expand Up @@ -121,7 +97,7 @@ function prepareFilterFromArgs(resolveParams = {}) {
export const findManyResolver = new Resolver({
name: 'findMany',
kind: 'query',
type: UserType,
type: UserTC,
args: {
filter: filterArgConfig,
sort: new GraphQLEnumType({
Expand All @@ -133,8 +109,8 @@ export const findManyResolver = new Resolver({
AGE_DESC: { name: 'AGE_DESC', value: { age: -1 } },
},
}),
limit: GraphQLInt,
skip: GraphQLInt,
limit: 'Int',
skip: 'Int',
},
resolve: resolveParams => {
const args = resolveParams.args || {};
Expand All @@ -155,17 +131,17 @@ export const findManyResolver = new Resolver({
return Promise.resolve(list);
},
});
userTypeComposer.setResolver('findMany', findManyResolver);
UserTC.setResolver('findMany', findManyResolver);

export const countResolver = new Resolver({
name: 'count',
kind: 'query',
type: GraphQLInt,
type: 'Int',
args: {
filter: filterArgConfig,
},
resolve: resolveParams => {
return Promise.resolve(filteredUserList(userList, prepareFilterFromArgs(resolveParams)).length);
},
});
userTypeComposer.setResolver('count', countResolver);
UserTC.setResolver('count', countResolver);
10 changes: 0 additions & 10 deletions src/__mocks__/rootQueryTypeComposer.js

This file was deleted.

33 changes: 13 additions & 20 deletions src/__tests__/composeWithPagination-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* @flow */
/* eslint-disable no-param-reassign */

import { TypeComposer } from 'graphql-compose';
import { GraphQLSchema, GraphQLList, graphql } from 'graphql-compose/lib/graphql';
import { TypeComposer, schemaComposer } from 'graphql-compose';
import { GraphQLList, graphql } from 'graphql-compose/lib/graphql';
import { composeWithPagination } from '../composeWithPagination';
import { userTypeComposer } from '../__mocks__/userTypeComposer';
import { rootQueryTypeComposer as rootQueryTC } from '../__mocks__/rootQueryTypeComposer';
import { UserTC } from '../__mocks__/User';

describe('composeWithRelay', () => {
const userComposer = composeWithPagination(userTypeComposer, {
const userComposer = composeWithPagination(UserTC, {
countResolverName: 'count',
findResolverName: 'findMany',
perPage: 5,
Expand All @@ -28,7 +27,7 @@ describe('composeWithRelay', () => {

it('should throw error if options are empty', () => {
expect(() => {
const args: any = [userTypeComposer];
const args: any = [UserTC];
composeWithPagination(...args);
}).toThrowError('should provide non-empty options');
});
Expand Down Expand Up @@ -69,10 +68,8 @@ describe('composeWithRelay', () => {

describe('fragments fields projection of graphql-compose', () => {
it('should return object', async () => {
rootQueryTC.setField('userPagination', userTypeComposer.getResolver('pagination'));
const schema = new GraphQLSchema({
query: rootQueryTC.getType(),
});
schemaComposer.Query.setField('userPagination', UserTC.getResolver('pagination'));
const schema = schemaComposer.buildSchema();
const query = `{
userPagination(page: 1, perPage: 2) {
count,
Expand Down Expand Up @@ -126,17 +123,15 @@ describe('composeWithRelay', () => {
it('should pass `countResolveParams` to top resolverParams', async () => {
let topResolveParams: any = {};

rootQueryTC.setField(
schemaComposer.Query.setField(
'userPagination',
userTypeComposer.getResolver('pagination').wrapResolve(next => rp => {
UserTC.getResolver('pagination').wrapResolve(next => rp => {
const result = next(rp);
topResolveParams = rp;
return result;
})
);
const schema = new GraphQLSchema({
query: rootQueryTC.getType(),
});
const schema = schemaComposer.buildSchema();
const query = `{
userPagination(filter: { age: 45 }) {
count
Expand All @@ -155,17 +150,15 @@ describe('composeWithRelay', () => {
it('should pass `findManyResolveParams` to top resolverParams', async () => {
let topResolveParams: any = {};

rootQueryTC.setField(
schemaComposer.Query.setField(
'userPagination',
userTypeComposer.getResolver('pagination').wrapResolve(next => rp => {
UserTC.getResolver('pagination').wrapResolve(next => rp => {
const result = next(rp);
topResolveParams = rp;
return result;
})
);
const schema = new GraphQLSchema({
query: rootQueryTC.getType(),
});
const schema = schemaComposer.buildSchema();
const query = `{
userPagination(filter: { age: 45 }) {
count
Expand Down
20 changes: 10 additions & 10 deletions src/__tests__/mocks-userTypeComposer-test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* @flow */
/* eslint-disable no-param-reassign */

import { userTypeComposer } from '../__mocks__/userTypeComposer';
import { UserTC } from '../__mocks__/User';

describe('mocks/userTypeComposer', () => {
it('userTypeComposer should have `count` resolver', async () => {
const cnt = await userTypeComposer.getResolver('count').resolve({});
describe('mocks/UserTC', () => {
it('UserTC should have `count` resolver', async () => {
const cnt = await UserTC.getResolver('count').resolve({});
expect(cnt).toBe(15);
});

it('userTypeComposer should have `findMany` resolver', async () => {
const res = await userTypeComposer.getResolver('findMany').resolve({});
it('UserTC should have `findMany` resolver', async () => {
const res = await UserTC.getResolver('findMany').resolve({});
expect(res).toHaveLength(15);
});

it('userTypeComposer should have `findMany` resolver with working `filter` arg', async () => {
const res = await userTypeComposer.getResolver('findMany').resolve({
it('UserTC should have `findMany` resolver with working `filter` arg', async () => {
const res = await UserTC.getResolver('findMany').resolve({
args: {
filter: {
gender: 'm',
Expand All @@ -35,8 +35,8 @@ describe('mocks/userTypeComposer', () => {
expect(res).toEqual([{ id: 9, name: 'user09', age: 19, gender: 'm' }]);
});

it('userTypeComposer should have `findMany` resolver with working `sort` arg', async () => {
const res = await userTypeComposer.getResolver('findMany').resolve({
it('UserTC should have `findMany` resolver with working `sort` arg', async () => {
const res = await UserTC.getResolver('findMany').resolve({
args: {
sort: {
age: -1,
Expand Down
48 changes: 22 additions & 26 deletions src/__tests__/paginationResolver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

import { Resolver } from 'graphql-compose';
import { GraphQLInt } from 'graphql-compose/lib/graphql';
import { userTypeComposer } from '../__mocks__/userTypeComposer';
import { UserTC } from '../__mocks__/User';
import { preparePaginationResolver } from '../paginationResolver';

describe('paginationResolver', () => {
const spyFindManyResolve = jest.spyOn(userTypeComposer.getResolver('findMany'), 'resolve');
const spyCountResolve = jest.spyOn(userTypeComposer.getResolver('count'), 'resolve');
const paginationResolver = preparePaginationResolver(userTypeComposer, {
const spyFindManyResolve = jest.spyOn(UserTC.getResolver('findMany'), 'resolve');
const spyCountResolve = jest.spyOn(UserTC.getResolver('count'), 'resolve');
const paginationResolver = preparePaginationResolver(UserTC, {
countResolverName: 'count',
findResolverName: 'findMany',
perPage: 5,
Expand All @@ -29,14 +29,14 @@ describe('paginationResolver', () => {

it('should throw error if opts.countResolverName are empty', () => {
expect(() => {
const args: any = [userTypeComposer, {}];
const args: any = [UserTC, {}];
preparePaginationResolver(...args);
}).toThrowError('should have option `opts.countResolverName`');
});

it('should throw error if resolver opts.countResolverName does not exists', () => {
expect(() =>
preparePaginationResolver(userTypeComposer, {
preparePaginationResolver(UserTC, {
countResolverName: 'countDoesNotExists',
findResolverName: 'findMany',
})
Expand All @@ -45,14 +45,14 @@ describe('paginationResolver', () => {

it('should throw error if opts.findResolverName are empty', () => {
expect(() => {
const args: any = [userTypeComposer, { countResolverName: 'count' }];
const args: any = [UserTC, { countResolverName: 'count' }];
preparePaginationResolver(...args);
}).toThrowError('should have option `opts.findResolverName`');
});

it('should throw error if resolver opts.countResolverName does not exists', () => {
expect(() =>
preparePaginationResolver(userTypeComposer, {
preparePaginationResolver(UserTC, {
countResolverName: 'count',
findResolverName: 'findManyDoesNotExists',
})
Expand All @@ -70,7 +70,7 @@ describe('paginationResolver', () => {
});

it('should have type to be ConnectionType', () => {
expect((paginationResolver.type: any).name).toBe('UserPagination');
expect((paginationResolver.type: any).getTypeName()).toBe('UserPagination');
});
});

Expand All @@ -93,23 +93,19 @@ describe('paginationResolver', () => {
beforeEach(() => {
findManyResolverCalled = false;
countResolverCalled = false;
const mockedFindMany = userTypeComposer
.getResolver('findMany')
.wrapResolve(next => resolveParams => {
findManyResolverCalled = true;
spyResolveParams = resolveParams;
return next(resolveParams);
});
const mockedCount = userTypeComposer
.getResolver('findMany')
.wrapResolve(next => resolveParams => {
countResolverCalled = true;
spyResolveParams = resolveParams;
return next(resolveParams);
});
userTypeComposer.setResolver('mockedFindMany', mockedFindMany);
userTypeComposer.setResolver('mockedCount', mockedCount);
mockedpaginationResolver = preparePaginationResolver(userTypeComposer, {
const mockedFindMany = UserTC.getResolver('findMany').wrapResolve(next => resolveParams => {
findManyResolverCalled = true;
spyResolveParams = resolveParams;
return next(resolveParams);
});
const mockedCount = UserTC.getResolver('findMany').wrapResolve(next => resolveParams => {
countResolverCalled = true;
spyResolveParams = resolveParams;
return next(resolveParams);
});
UserTC.setResolver('mockedFindMany', mockedFindMany);
UserTC.setResolver('mockedCount', mockedCount);
mockedpaginationResolver = preparePaginationResolver(UserTC, {
countResolverName: 'mockedCount',
findResolverName: 'mockedFindMany',
});
Expand Down
8 changes: 4 additions & 4 deletions src/paginationResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
} from 'graphql-compose';
import type { GraphQLResolveInfo } from 'graphql-compose/lib/graphql';
import type { ComposeWithPaginationOpts } from './composeWithPagination';
import preparePaginationType from './types/paginationType';
import { preparePaginationTC } from './types/preparePaginationType';

const DEFAULT_PER_PAGE = 20;

Expand Down Expand Up @@ -98,7 +98,7 @@ export function preparePaginationResolver(
}

return new tc.constructor.schemaComposer.Resolver({
type: preparePaginationType(tc),
type: preparePaginationTC(tc),
name: 'pagination',
kind: 'query',
args: {
Expand All @@ -113,7 +113,7 @@ export function preparePaginationResolver(
},
...(additionalArgs: any),
},
// eslint-disable-next-line
// prettier-ignore
resolve: async /* :: <TContext> */(
rp /* : $Shape<PaginationResolveParams<TContext>> */
) => {
Expand Down Expand Up @@ -194,6 +194,6 @@ export function preparePaginationResolver(
};
return result;
});
},
}
});
}
Loading

0 comments on commit 33d30f7

Please sign in to comment.