Skip to content

Commit

Permalink
fix: improve typedefs for connection resolver factory (#338)
Browse files Browse the repository at this point in the history
* Improve typedefs for connection resolver factory

* Fix lint problem

Co-authored-by: Yoad <[email protected]>
  • Loading branch information
yoadsn and Yoad authored May 18, 2021
1 parent a4e0662 commit dbc609d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ UserTC.mongooseResolvers.findMany(opts);

```ts
type ConnectionResolverOpts<TContext = any> = {
sort: ConnectionSortMapOpts;
/** See below **/
sort?: ConnectionSortMapOpts;
name?: string;
defaultLimit?: number | undefined;
edgeTypeName?: string;
Expand All @@ -388,6 +389,15 @@ type ConnectionResolverOpts<TContext = any> = {
}
```
The `countOpts` and `findManyOpts` props would be used to customize the internally created `findMany` and `count` resolver factories used by the connection resolver.
If not provided the default configuration for each of the resolver factories is assumed.
The `sort` prop is optional. When provided it is used to customize the sorting behaviour of the connection. When not provided, the sorting configuration is derived from the existing indexes on the model.
Please refer to the documentation of the [graphql-compose-connection](https://github.com/graphql-compose/graphql-compose-connection) plugin for more details on the sorting customization parameter.
### `count(opts?: CountResolverOpts)`
```ts
Expand Down
28 changes: 18 additions & 10 deletions src/resolvers/__tests__/connection-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import { count } from '../count';
import { convertModelToGraphQL } from '../../fieldsConverter';
import { ExtendedResolveParams } from '..';

jest.mock('../findMany', () => ({
findMany: jest.fn((...args) => jest.requireActual('../findMany').findMany(...args)),
}));
jest.mock('../count', () => ({
count: jest.fn((...args) => jest.requireActual('../count').count(...args)),
}));

beforeAll(() => UserModel.base.createConnection());
afterAll(() => UserModel.base.disconnect());

Expand Down Expand Up @@ -111,8 +118,6 @@ describe('connection() resolver', () => {
beforeEach(() => {
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setResolver('findMany', findMany(UserModel, UserTC));
UserTC.setResolver('count', count(UserModel, UserTC));
});

let user1: IUser;
Expand Down Expand Up @@ -287,20 +292,23 @@ describe('connection() resolver', () => {
expect(result).toHaveProperty('edges.0.node', { overrides: true });
});

it('should return mongoose documents for custom resolvers from opts', async () => {
it('should use internal resolver custom opts', async () => {
schemaComposer.clear();
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
UserTC.setResolver('customFindMany', findMany(UserModel, UserTC));
UserTC.setResolver('customCount', count(UserModel, UserTC));
const mockCountOpts = { suffix: 'ABC' };
const mockFindManyOpts = { suffix: 'DEF' };
const resolver = connection(UserModel, UserTC, {
findResolverName: 'customFindMany',
countResolverName: 'customCount',
countOpts: mockCountOpts,
findManyOpts: mockFindManyOpts,
} as any);
if (!resolver) throw new Error('Connection resolver is undefined');

const result = await resolver.resolve({ args: { first: 20 } });
expect(result.edges[0].node).toBeInstanceOf(UserModel);
expect(result.edges[1].node).toBeInstanceOf(UserModel);
expect(count).toHaveBeenCalledWith(expect.anything(), expect.anything(), mockCountOpts);
expect(findMany).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
mockFindManyOpts
);
});
});
});
Expand Down
13 changes: 12 additions & 1 deletion src/resolvers/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@ import type { Document, Model } from 'mongoose';
import {
prepareConnectionResolver,
ConnectionResolverOpts as _ConnectionResolverOpts,
ConnectionSortMapOpts as _ConnectionSortMapOpts,
ConnectionTArgs,
} from 'graphql-compose-connection';
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
import { CountResolverOpts, count } from './count';
import { FindManyResolverOpts, findMany } from './findMany';
import { getUniqueIndexes, extendByReversedIndexes, IndexT } from '../utils/getIndexesFromModel';

/**
* Allows customization of the generated `connection` resolver.
*
* It is based on the `ConnectionResolverOpts` from: https://github.com/graphql-compose/graphql-compose-connection
* With the following changes:
* - `sort` prop is optional - when not provided derived from existing model indexes
* - `findManyResolver` prop is removed - use the optional `findManyOpts` to customize the internally created `findMany` resolver
* - `countResolver` prop is removed - use the optional `countOpts` to customize the internally created `count` resolver
*/
export type ConnectionResolverOpts<TContext = any> = Omit<
_ConnectionResolverOpts<TContext>,
'countResolver' | 'findManyResolver'
'countResolver' | 'findManyResolver' | 'sort'
> & {
countOpts?: CountResolverOpts;
findManyOpts?: FindManyResolverOpts;
sort?: _ConnectionSortMapOpts;
};

export function connection<TSource = any, TContext = any, TDoc extends Document = any>(
Expand Down

0 comments on commit dbc609d

Please sign in to comment.