import { NODE_TYPES } from 'common/enums' ; import { fromGlobalId , toGlobalId } from 'common/utils' ; import { ArticleToCommentsResolver } from 'definitions' ; const resolver = async ( { articleId } , { input : { sort , first , ... rest } } , { dataSources : { atomService , commentService } } ) => { const order = sort === 'oldest' ? 'asc' : 'desc' ; if ( ! rest . before && typeof first === 'undefined' ) { first = 10 ; } let before ; let after ; if ( rest . after ) { after = fromGlobalId ( rest . after ) . id ; } if ( rest . before ) { before = fromGlobalId ( rest . before ) . id ; } const { id : targetTypeId } = await atomService . findFirst ( { table : 'entity_type' , where : { table : 'article' } , } ) ; let where = { targetId : articleId , targetTypeId } as { [ key ] : any ; } ; if ( rest . filter ) { const { parentComment , author , state } = rest . filter ; if ( parentComment || parentComment === null ) { where = { parentCommentId : parentComment ? fromGlobalId ( parentComment ) . id : null , ... where , } ; } if ( author ) { where = { authorId : fromGlobalId ( author ) . id , ... where , } ; } if ( state ) { where = { state , ... where , } ; } } const [ comments , range ] = await Promise . all ( [ commentService . find ( { sort , before , after , first , where , order , includeAfter : rest . includeAfter , includeBefore : rest . includeBefore , } ) , commentService . range ( where ) , ] ) ; const edges = comments . map ( ( comment ) => ( { cursor : toGlobalId ( { type : NODE_TYPES . Comment , id : comment . id } ) , node : comment , } ) ) ; const firstEdge = edges [ 0 ] ; const firstId = firstEdge && parseInt ( firstEdge . node . id , 10 ) ; const lastEdge = edges [ edges . length - 1 ] ; const lastId = lastEdge && parseInt ( lastEdge . node . id , 10 ) ; return { edges , totalCount : range . count , pageInfo : { startCursor : firstEdge ? firstEdge . cursor : '' , endCursor : lastEdge ? lastEdge . cursor : '' , hasPreviousPage : order === 'asc' ? firstId > range . min : firstId < range . max , hasNextPage : order === 'asc' ? lastId < range . max : lastId > range . min , } , } ; } ; export default resolver ;