Skip to content

Commit dbc609d

Browse files
yoadsnYoad
andauthored
fix: improve typedefs for connection resolver factory (#338)
* Improve typedefs for connection resolver factory * Fix lint problem Co-authored-by: Yoad <[email protected]>
1 parent a4e0662 commit dbc609d

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ UserTC.mongooseResolvers.findMany(opts);
376376

377377
```ts
378378
type ConnectionResolverOpts<TContext = any> = {
379-
sort: ConnectionSortMapOpts;
379+
/** See below **/
380+
sort?: ConnectionSortMapOpts;
380381
name?: string;
381382
defaultLimit?: number | undefined;
382383
edgeTypeName?: string;
@@ -388,6 +389,15 @@ type ConnectionResolverOpts<TContext = any> = {
388389
}
389390
```
390391
392+
The `countOpts` and `findManyOpts` props would be used to customize the internally created `findMany` and `count` resolver factories used by the connection resolver.
393+
If not provided the default configuration for each of the resolver factories is assumed.
394+
395+
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.
396+
397+
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.
398+
399+
400+
391401
### `count(opts?: CountResolverOpts)`
392402
393403
```ts

src/resolvers/__tests__/connection-test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import { count } from '../count';
77
import { convertModelToGraphQL } from '../../fieldsConverter';
88
import { ExtendedResolveParams } from '..';
99

10+
jest.mock('../findMany', () => ({
11+
findMany: jest.fn((...args) => jest.requireActual('../findMany').findMany(...args)),
12+
}));
13+
jest.mock('../count', () => ({
14+
count: jest.fn((...args) => jest.requireActual('../count').count(...args)),
15+
}));
16+
1017
beforeAll(() => UserModel.base.createConnection());
1118
afterAll(() => UserModel.base.disconnect());
1219

@@ -111,8 +118,6 @@ describe('connection() resolver', () => {
111118
beforeEach(() => {
112119
schemaComposer.clear();
113120
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
114-
UserTC.setResolver('findMany', findMany(UserModel, UserTC));
115-
UserTC.setResolver('count', count(UserModel, UserTC));
116121
});
117122

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

290-
it('should return mongoose documents for custom resolvers from opts', async () => {
295+
it('should use internal resolver custom opts', async () => {
291296
schemaComposer.clear();
292297
UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer);
293-
UserTC.setResolver('customFindMany', findMany(UserModel, UserTC));
294-
UserTC.setResolver('customCount', count(UserModel, UserTC));
298+
const mockCountOpts = { suffix: 'ABC' };
299+
const mockFindManyOpts = { suffix: 'DEF' };
295300
const resolver = connection(UserModel, UserTC, {
296-
findResolverName: 'customFindMany',
297-
countResolverName: 'customCount',
301+
countOpts: mockCountOpts,
302+
findManyOpts: mockFindManyOpts,
298303
} as any);
299304
if (!resolver) throw new Error('Connection resolver is undefined');
300305

301-
const result = await resolver.resolve({ args: { first: 20 } });
302-
expect(result.edges[0].node).toBeInstanceOf(UserModel);
303-
expect(result.edges[1].node).toBeInstanceOf(UserModel);
306+
expect(count).toHaveBeenCalledWith(expect.anything(), expect.anything(), mockCountOpts);
307+
expect(findMany).toHaveBeenCalledWith(
308+
expect.anything(),
309+
expect.anything(),
310+
mockFindManyOpts
311+
);
304312
});
305313
});
306314
});

src/resolvers/connection.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@ import type { Document, Model } from 'mongoose';
22
import {
33
prepareConnectionResolver,
44
ConnectionResolverOpts as _ConnectionResolverOpts,
5+
ConnectionSortMapOpts as _ConnectionSortMapOpts,
56
ConnectionTArgs,
67
} from 'graphql-compose-connection';
78
import type { Resolver, ObjectTypeComposer } from 'graphql-compose';
89
import { CountResolverOpts, count } from './count';
910
import { FindManyResolverOpts, findMany } from './findMany';
1011
import { getUniqueIndexes, extendByReversedIndexes, IndexT } from '../utils/getIndexesFromModel';
1112

13+
/**
14+
* Allows customization of the generated `connection` resolver.
15+
*
16+
* It is based on the `ConnectionResolverOpts` from: https://github.com/graphql-compose/graphql-compose-connection
17+
* With the following changes:
18+
* - `sort` prop is optional - when not provided derived from existing model indexes
19+
* - `findManyResolver` prop is removed - use the optional `findManyOpts` to customize the internally created `findMany` resolver
20+
* - `countResolver` prop is removed - use the optional `countOpts` to customize the internally created `count` resolver
21+
*/
1222
export type ConnectionResolverOpts<TContext = any> = Omit<
1323
_ConnectionResolverOpts<TContext>,
14-
'countResolver' | 'findManyResolver'
24+
'countResolver' | 'findManyResolver' | 'sort'
1525
> & {
1626
countOpts?: CountResolverOpts;
1727
findManyOpts?: FindManyResolverOpts;
28+
sort?: _ConnectionSortMapOpts;
1829
};
1930

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

0 commit comments

Comments
 (0)