import { ARTICLE_STATE } from 'common/enums' ; import { connectionFromArray , connectionFromPromisedArray , fromConnectionArgs , } from 'common/utils' ; import { CircleToWorksResolver } from 'definitions' ; const resolver = async ( { id } , { input } , { dataSources : { atomService } , knex } ) => { if ( ! id ) { return connectionFromArray ( [ ] , input ) ; } const { take , skip } = fromConnectionArgs ( input ) ; const countQuery = knex . from ( 'article_circle' ) . innerJoin ( 'article' , 'article.id' , 'article_circle.article_id' ) . where ( { circleId : id , 'article.state' : ARTICLE_STATE . active } ) . count ( ) . first ( ) ; const articlesQuery = knex . select ( ) . from ( 'article_circle' ) . innerJoin ( 'article' , 'article.id' , 'article_circle.article_id' ) . where ( { circleId : id , 'article.state' : ARTICLE_STATE . active } ) . orderBy ( 'article_circle.updated_at' , 'desc' ) . offset ( skip ) . limit ( take ) ; const [ count , articles ] = await Promise . all ( [ countQuery , articlesQuery ] ) ; const totalCount = parseInt ( count ? ( count . count as string ) : '0' , 10 ) ; return connectionFromPromisedArray ( atomService . draftIdLoader . loadMany ( articles . map ( ( { draftId } ) => draftId ) ) , input , totalCount ) ; } ; export default resolver ;