1
1
import {
2
- Connection ,
3
2
ConnectionArguments ,
4
3
getOffsetWithDefault ,
5
4
offsetToCursor
6
5
} from 'graphql-relay'
7
6
import { Repository } from 'typeorm'
8
7
8
+ import { Connection } from './connection.interface'
9
+
9
10
export async function connectionFromRepository < T > (
10
11
args : ConnectionArguments ,
11
12
repository : Repository < T >
12
13
) : Promise < Connection < T > > {
13
14
const { before, after, first, last } = args
14
15
15
- const total = await repository . count ( )
16
+ const totalCount = await repository . count ( )
16
17
17
18
// offsets
18
- const beforeOffset = getOffsetWithDefault ( before , total )
19
+ const beforeOffset = getOffsetWithDefault ( before , totalCount )
19
20
const afterOffset = getOffsetWithDefault ( after , - 1 )
20
21
21
22
let startOffset = Math . max ( - 1 , afterOffset ) + 1
22
- let endOffset = Math . min ( beforeOffset , total )
23
+ let endOffset = Math . min ( beforeOffset , totalCount )
23
24
24
25
if ( first ) {
25
26
endOffset = Math . min ( endOffset , startOffset + first )
@@ -44,7 +45,7 @@ export async function connectionFromRepository<T>(
44
45
// page info
45
46
const { length, 0 : firstEdge , [ length - 1 ] : lastEdge } = edges
46
47
const lowerBound = after ? afterOffset + 1 : 0
47
- const upperBound = before ? Math . min ( beforeOffset , total ) : total
48
+ const upperBound = before ? Math . min ( beforeOffset , totalCount ) : totalCount
48
49
49
50
const pageInfo = {
50
51
startCursor : firstEdge ? firstEdge . cursor : null ,
@@ -55,6 +56,7 @@ export async function connectionFromRepository<T>(
55
56
56
57
return {
57
58
edges,
58
- pageInfo
59
+ pageInfo,
60
+ totalCount
59
61
}
60
62
}
0 commit comments