11import type { Document , Model } from 'mongoose' ;
2- import type { ConnectionSortMapOpts as _ConnectionSortMapOpts } from 'graphql-compose-connection' ;
3- import type {
4- Resolver ,
5- ObjectTypeComposer ,
6- ObjectTypeComposerFieldConfigMap ,
7- } from 'graphql-compose' ;
2+ import {
3+ prepareConnectionResolver ,
4+ ConnectionResolverOpts as _ConnectionResolverOpts ,
5+ ConnectionTArgs ,
6+ } from 'graphql-compose-connection' ;
7+ import type { Resolver , ObjectTypeComposer } from 'graphql-compose' ;
8+ import { CountResolverOpts , count } from './count' ;
9+ import { FindManyResolverOpts , findMany } from './findMany' ;
810import { getUniqueIndexes , extendByReversedIndexes , IndexT } from '../utils/getIndexesFromModel' ;
911
10- export type ConnectionResolverOpts < TContext = any > = _ConnectionSortMapOpts & {
11- edgeFields ?: ObjectTypeComposerFieldConfigMap < any , TContext > ;
12- connectionResolverName ?: string ;
13- findResolverName ?: string ;
14- countResolverName ?: string ;
15- edgeTypeName ?: string ;
16- } ;
17-
18- type TArgs = {
19- first ?: number ;
20- after ?: string ;
21- last ?: number ;
22- before ?: string ;
23- filter ?: any ;
24- sort ?: Record < string , any > ;
12+ export type ConnectionResolverOpts < TContext = any > = Omit <
13+ _ConnectionResolverOpts < TContext > ,
14+ 'countResolver' | 'findManyResolver'
15+ > & {
16+ countOpts ?: CountResolverOpts ;
17+ findManyOpts ?: FindManyResolverOpts ;
2518} ;
2619
2720export function connection < TSource = any , TContext = any , TDoc extends Document = any > (
2821 model : Model < TDoc > ,
2922 tc : ObjectTypeComposer < TDoc , TContext > ,
3023 opts ?: ConnectionResolverOpts < TContext >
31- ) : Resolver < TSource , TContext , TArgs , TDoc > | undefined {
32- try {
33- require . resolve ( 'graphql-compose-connection' ) ;
34- } catch ( e ) {
35- return undefined ;
36- }
37- // eslint-disable-next-line @typescript-eslint/no-var-requires
38- const prepareConnectionResolver = require ( 'graphql-compose-connection' ) . prepareConnectionResolver ;
39-
40- if ( ! prepareConnectionResolver ) {
41- throw new Error (
42- 'You should update `graphql-compose-connection` package till 3.2.0 version or above'
43- ) ;
44- }
45-
24+ ) : Resolver < TSource , TContext , ConnectionTArgs , TDoc > | undefined {
4625 const uniqueIndexes = extendByReversedIndexes ( getUniqueIndexes ( model ) , {
4726 reversedFirst : true ,
4827 } ) ;
@@ -69,24 +48,14 @@ export function connection<TSource = any, TContext = any, TDoc extends Document
6948 } ,
7049 } ;
7150 } ) ;
72- const {
73- connectionResolverName = 'connection' ,
74- findResolverName = 'findMany' ,
75- countResolverName = 'count' ,
76- edgeFields,
77- edgeTypeName,
78- ...sortOptions
79- } = opts || { } ;
80- return prepareConnectionResolver ( tc , {
81- connectionResolverName,
82- findResolverName,
83- countResolverName,
84- sort : {
85- ...sortConfigs ,
86- ...sortOptions ,
87- } ,
88- edgeFields,
89- edgeTypeName,
51+
52+ const { findManyOpts, countOpts, ...restOpts } = opts || { } ;
53+
54+ return prepareConnectionResolver < any , any > ( tc , {
55+ findManyResolver : findMany ( model , tc , findManyOpts ) ,
56+ countResolver : count ( model , tc , countOpts ) ,
57+ sort : sortConfigs ,
58+ ...restOpts ,
9059 } ) ;
9160}
9261
0 commit comments