import type { Resolver , ObjectTypeComposer , InterfaceTypeComposer } from 'graphql-compose' ; import type { Model , Document } from 'mongoose' ; import { filterHelper , filterHelperArgs , FilterHelperArgsOpts , prepareNestedAliases , } from './helpers' ; import type { ExtendedResolveParams } from './index' ; import { beforeQueryHelper } from './helpers/beforeQueryHelper' ; export interface CountResolverOpts { /** If you want to generate different resolvers you may avoid Type name collision by adding a suffix to type names */ suffix ? : string ; /** Customize input-type for `filter` argument. If `false` then arg will be removed. */ filter ? : FilterHelperArgsOpts | false ; } type TArgs = { filter ? : any ; } ; export function count < TSource = any , TContext = any , TDoc extends Document = any > ( model , tc , opts ? ) { if ( ! model || ! model . modelName || ! model . schema ) { throw new Error ( 'First arg for Resolver count() should be instance of Mongoose Model.' ) ; } if ( ! tc || tc . constructor . name !== 'ObjectTypeComposer' ) { throw new Error ( 'Second arg for Resolver count() should be instance of ObjectTypeComposer.' ) ; } const aliases = prepareNestedAliases ( model . schema ) ; return tc . schemaComposer . createResolver < TSource , TArgs > ( { type : 'Int' , name : 'count' , kind : 'query' , args : { ... filterHelperArgs ( tc , model , { prefix : 'FilterCount' , suffix : `${ opts ?. suffix || '' }Input` , ... opts ?. filter , } ) , } , resolve : ( ( resolveParams ) => { resolveParams . query = model . find ( ) ; resolveParams . model = model ; filterHelper ( resolveParams , aliases ) ; if ( resolveParams . query . countDocuments ) { resolveParams . query . countDocuments ( ) ; return beforeQueryHelper ( resolveParams ) ; } else { resolveParams . query . count ( ) ; return beforeQueryHelper ( resolveParams ) ; } } ) as any , } ) ; }