diff --git a/README.md b/README.md index 0460e75b..e9f37b1e 100644 --- a/README.md +++ b/README.md @@ -376,7 +376,8 @@ UserTC.mongooseResolvers.findMany(opts); ```ts type ConnectionResolverOpts = { - sort: ConnectionSortMapOpts; + /** See below **/ + sort?: ConnectionSortMapOpts; name?: string; defaultLimit?: number | undefined; edgeTypeName?: string; @@ -388,6 +389,15 @@ type ConnectionResolverOpts = { } ``` +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 diff --git a/src/resolvers/__tests__/connection-test.ts b/src/resolvers/__tests__/connection-test.ts index 9f0d59ad..a9bc03ba 100644 --- a/src/resolvers/__tests__/connection-test.ts +++ b/src/resolvers/__tests__/connection-test.ts @@ -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()); @@ -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; @@ -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 + ); }); }); }); diff --git a/src/resolvers/connection.ts b/src/resolvers/connection.ts index 0bca0bee..a921b557 100644 --- a/src/resolvers/connection.ts +++ b/src/resolvers/connection.ts @@ -2,6 +2,7 @@ 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'; @@ -9,12 +10,22 @@ 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 = Omit< _ConnectionResolverOpts, - 'countResolver' | 'findManyResolver' + 'countResolver' | 'findManyResolver' | 'sort' > & { countOpts?: CountResolverOpts; findManyOpts?: FindManyResolverOpts; + sort?: _ConnectionSortMapOpts; }; export function connection(