import { ApolloError } from 'apollo-server-express' ; import { Arg , Query , Resolver } from 'type-graphql' ; import { BaseEntity } from 'typeorm' ; export const createResolver = ( suffix , entity ) : any => { @ Resolver ( ) class BaseResolver { @ Query ( ( ) => [ entity ] , { name : `paginate${ suffix }s` , complexity : ( { args } ) => args . limit , } ) paginate ( @ Arg ( 'limit' ) limit , @ Arg ( 'offset' ) offset , @ Arg ( 'sortCol' , { nullable : true } ) sortCol ? , @ Arg ( 'sortType' , { nullable : true } ) sortType ? ) { if ( sortType && sortType !== 'ASC' && sortType !== 'DESC' ) { throw new ApolloError ( 'Sort type must be "DESC" or "ASC"' ) ; } const order = { } ; order [ sortCol ?? 'id' ] = sortType ?? 'DESC' ; return entity . find ( { skip : offset , take : limit , order , } ) ; } @ Query ( ( ) => entity , { name : `get${ suffix }` , complexity : ( { args } ) => args . limit , } ) get ( @ Arg ( 'id' ) id ) { return entity . findOne ( id ) ; } } return BaseResolver ; } ;