@@ -75,11 +75,12 @@ export function prepareConnectionResolver(
75
75
...additionalArgs ,
76
76
sort : {
77
77
type : sortEnumType ,
78
- defaultValue : sortEnumType . getValues ( ) [ 0 ] . name , // first enum used by default
78
+ defaultValue : sortEnumType . getValues ( ) [ 0 ] . value , // first enum used by default
79
79
description : 'Sort argument for data ordering' ,
80
80
} ,
81
81
} ,
82
- resolve : ( resolveParams : ConnectionResolveParams ) => {
82
+ resolve : async ( resolveParams : ConnectionResolveParams ) => {
83
+ let countPromise ;
83
84
const { projection = { } , args = { } } = resolveParams ;
84
85
const findManyParams : ResolveParams = Object . assign (
85
86
{ } ,
@@ -88,39 +89,51 @@ export function prepareConnectionResolver(
88
89
) ;
89
90
const sortOptions : connectionSortOpts = args . sort ;
90
91
91
- const first = parseInt ( args . first , 10 ) ;
92
+ let filter = resolveParams . args . filter || { } ;
93
+ const beginCursorData = cursorToData ( args . after ) ;
94
+ if ( beginCursorData ) {
95
+ filter = sortOptions . directionFilter ( beginCursorData , filter , false ) ;
96
+ }
97
+ const endCursorData = cursorToData ( args . before ) ;
98
+ if ( endCursorData ) {
99
+ filter = sortOptions . directionFilter ( endCursorData , filter , true ) ;
100
+ }
101
+ findManyParams . args . filter = filter ;
102
+
103
+
104
+ let first = parseInt ( args . first , 10 ) ;
92
105
const last = parseInt ( args . last , 10 ) ;
93
106
94
- const limit = last || first ;
107
+ if ( projection . count ) {
108
+ countPromise = countResolve ( findManyParams ) ;
109
+ } else if ( ! first && last ) {
110
+ countPromise = countResolve ( findManyParams ) ;
111
+ } else {
112
+ countPromise = Promise . resolve ( 0 ) ;
113
+ }
114
+
115
+ if ( ! first && last ) {
116
+ first = ( await countPromise ) || 0 ;
117
+ }
118
+
119
+ const limit = first ;
95
120
const skip = ( first - last ) || 0 ;
96
121
97
122
findManyParams . args . limit = limit + 1 ; // +1 document, to check next page presence
98
123
if ( skip > 0 ) {
99
124
findManyParams . args . skip = skip ;
100
125
}
101
126
102
- let filter = findManyParams . args . filter || { } ;
103
- const beginCursorData = cursorToData ( args . after ) ;
104
- if ( beginCursorData ) {
105
- filter = sortOptions . cursorToFilter ( beginCursorData , filter ) ;
106
- }
107
- const endCursorData = cursorToData ( args . before ) ;
108
- if ( endCursorData ) {
109
- filter = sortOptions . cursorToFilter ( endCursorData , filter ) ;
110
- }
111
- findManyParams . args . filter = filter ;
112
- findManyParams . args . skip = skip ;
113
-
114
127
findManyParams . args . sort = sortOptions . sortValue ;
115
128
findManyParams . projection = projection ;
116
129
sortOptions . uniqueFields . forEach ( fieldName => {
117
130
findManyParams . projection [ fieldName ] = true ;
118
131
} ) ;
119
132
120
- let countPromise ;
121
- if ( projection . count ) {
122
- countPromise = countResolve ( resolveParams ) ;
123
- }
133
+ findManyParams . projection [ 'count' ] = true ;
134
+ findManyParams . projection [ 'age' ] = true ;
135
+ findManyParams . projection [ 'name' ] = true ;
136
+
124
137
const hasPreviousPage = skip > 0 ;
125
138
let hasNextPage = false ; // will be requested +1 document, to check next page presence
126
139
@@ -132,13 +145,15 @@ export function prepareConnectionResolver(
132
145
return result ;
133
146
} ;
134
147
135
- return findManyResolve ( findManyParams )
136
- . then ( recordList => {
148
+ console . log ( findManyParams . args ) ;
149
+
150
+ return Promise . all ( [ findManyResolve ( findManyParams ) , countPromise ] )
151
+ . then ( ( [ recordList , count ] ) => {
137
152
const edges = [ ] ;
138
153
// if returned more than `limit` records, strip array and mark that exists next page
139
154
if ( recordList . length > limit ) {
140
155
hasNextPage = true ;
141
- recordList = recordList . slice ( 0 , limit - 1 ) ;
156
+ recordList = recordList . slice ( 0 , limit ) ;
142
157
}
143
158
// transform record to object { cursor, node }
144
159
recordList . forEach ( record => {
@@ -147,18 +162,12 @@ export function prepareConnectionResolver(
147
162
node : record ,
148
163
} ) ;
149
164
} ) ;
150
- return edges ;
165
+ return [ edges , count ] ;
151
166
} )
152
- . then ( async ( edges ) => {
167
+ . then ( ( [ edges , count ] ) => {
153
168
const result = emptyConnection ( ) ;
154
-
155
- // pass `edge` data
156
169
result . edges = edges ;
157
-
158
- // if exists countPromise, await it's data
159
- if ( countPromise ) {
160
- result . count = await countPromise ;
161
- }
170
+ result . count = count ;
162
171
163
172
// pageInfo may be extended, so set data gradually
164
173
if ( edges . length > 0 ) {
@@ -187,17 +196,17 @@ export function emptyConnection(): GraphQLConnectionType {
187
196
} ;
188
197
}
189
198
190
- export function cursorToData ( id ?: ?string ) : ?CursorDataType {
191
- if ( id ) {
199
+ export function cursorToData ( cursor ?: ?string ) : ?CursorDataType {
200
+ if ( cursor ) {
192
201
try {
193
- return JSON . parse ( id ) || null ;
202
+ return JSON . parse ( cursor ) || null ;
194
203
} catch ( err ) {
195
204
return null ;
196
205
}
197
206
}
198
207
return null ;
199
208
}
200
209
201
- export function dataToCursor ( cursorData : CursorDataType ) : string {
202
- return JSON . stringify ( cursorData ) ;
210
+ export function dataToCursor ( data : CursorDataType ) : string {
211
+ return JSON . stringify ( data ) ;
203
212
}
0 commit comments