@@ -10,9 +10,15 @@ import {
1010import { getThreadsByChannels } from '../../models/thread' ;
1111import { canViewCommunity } from '../../utils/permissions' ;
1212
13+ export type CommunityThreadConnectionPaginationOptions = {
14+ after : string ,
15+ first : number ,
16+ sort : 'latest' | 'trending' ,
17+ } ;
18+
1319// prettier-ignore
14- export default async ( root : DBCommunity , args : PaginationOptions , ctx : GraphQLContext ) => {
15- const { first = 10 , after } = args
20+ export default async ( root : DBCommunity , args : CommunityThreadConnectionPaginationOptions , ctx : GraphQLContext ) => {
21+ const { first = 10 , after, sort = 'latest' } = args
1622 const { user, loaders } = ctx
1723 const { id } = root
1824
@@ -29,7 +35,7 @@ export default async (root: DBCommunity, args: PaginationOptions, ctx: GraphQLCo
2935 // Get the index from the encoded cursor, asdf234gsdf-2 => ["-2", "2"]
3036 const lastDigits = cursor . match ( / - ( \d + ) $ / ) ;
3137 const lastThreadIndex =
32- lastDigits && lastDigits . length > 0 && parseInt ( lastDigits [ 1 ] , 10 ) ;
38+ lastDigits && lastDigits . length > 0 && parseInt ( lastDigits [ 1 ] , 10 ) || 0 ;
3339 const currentUser = user ;
3440
3541 // if the user is signed in, only return stories for the channels
@@ -44,10 +50,10 @@ export default async (root: DBCommunity, args: PaginationOptions, ctx: GraphQLCo
4450 channels = await getPublicChannelsByCommunity ( id ) ;
4551 }
4652
47- // $FlowFixMe
4853 const threads = await getThreadsByChannels ( channels , {
4954 first,
5055 after : lastThreadIndex ,
56+ sort,
5157 } ) ;
5258
5359 return {
0 commit comments