7
7
} from 'graphql-relay' ;
8
8
9
9
import {
10
- GraphQLList ,
11
- GraphQLEnumType
10
+ GraphQLList
12
11
} from 'graphql' ;
13
12
14
13
import {
@@ -105,7 +104,7 @@ export function nodeType(connectionType) {
105
104
export function sequelizeConnection ( {
106
105
name,
107
106
nodeType,
108
- target,
107
+ target : targetMaybeThunk ,
109
108
orderBy : orderByEnum ,
110
109
before,
111
110
after,
@@ -123,29 +122,22 @@ export function sequelizeConnection({
123
122
edgeFields
124
123
} ) ;
125
124
126
- const model = target . target ? target . target : target ;
127
125
const SEPERATOR = '$' ;
128
126
const PREFIX = 'arrayconnection' + SEPERATOR ;
129
127
130
- if ( orderByEnum === undefined ) {
131
- orderByEnum = new GraphQLEnumType ( {
132
- name : name + 'ConnectionOrder' ,
133
- values : {
134
- ID : { value : [ model . primaryKeyAttribute , 'ASC' ] }
135
- }
136
- } ) ;
137
- }
138
-
139
128
before = before || ( ( options ) => options ) ;
140
129
after = after || ( ( result ) => result ) ;
141
130
142
131
let $connectionArgs = {
143
- ...connectionArgs ,
144
- orderBy : {
145
- type : new GraphQLList ( orderByEnum )
146
- }
132
+ ...connectionArgs
147
133
} ;
148
134
135
+ if ( orderByEnum ) {
136
+ $connectionArgs . orderBy = {
137
+ type : new GraphQLList ( orderByEnum )
138
+ } ;
139
+ }
140
+
149
141
let orderByAttribute = function ( orderAttr , { source, args, context, info} ) {
150
142
return typeof orderAttr === 'function' ? orderAttr ( source , args , context , info ) : orderAttr ;
151
143
} ;
@@ -166,7 +158,7 @@ export function sequelizeConnection({
166
158
* @return {String } The Base64 encoded cursor string
167
159
*/
168
160
let toCursor = function ( item , index ) {
169
- let id = item . get ( model . primaryKeyAttribute ) ;
161
+ let id = item . get ( item . Model . primaryKeyAttribute ) ;
170
162
return base64 ( PREFIX + id + SEPERATOR + index ) ;
171
163
} ;
172
164
@@ -189,6 +181,8 @@ export function sequelizeConnection({
189
181
let argsToWhere = function ( args ) {
190
182
let result = { } ;
191
183
184
+ if ( where === undefined ) return result ;
185
+
192
186
_ . each ( args , ( value , key ) => {
193
187
if ( key in $connectionArgs ) return ;
194
188
_ . assign ( result , where ( key , value , result ) ) ;
@@ -213,16 +207,21 @@ export function sequelizeConnection({
213
207
} ;
214
208
} ;
215
209
216
- let $resolver = require ( './resolver' ) ( target , {
210
+ let $resolver = require ( './resolver' ) ( targetMaybeThunk , {
217
211
handleConnection : false ,
218
212
list : true ,
219
213
before : function ( options , args , context , info ) {
214
+ const target = info . target ;
215
+ const model = target . target ? target . target : target ;
216
+
220
217
if ( args . first || args . last ) {
221
218
options . limit = parseInt ( args . first || args . last , 10 ) ;
222
219
}
223
220
if ( ! args . orderBy ) {
224
- args . orderBy = [ orderByEnum . _values [ 0 ] . value ] ;
225
- } else if ( typeof args . orderBy === 'string' ) {
221
+ args . orderBy = [
222
+ [ model . primaryKeyAttribute , 'ASC' ]
223
+ ] ;
224
+ } else if ( orderByEnum && typeof args . orderBy === 'string' ) {
226
225
args . orderBy = [ orderByEnum . _nameLookup [ args . orderBy ] . value ] ;
227
226
}
228
227
@@ -275,6 +274,7 @@ export function sequelizeConnection({
275
274
after : async function ( values , args , context , info ) {
276
275
const {
277
276
source,
277
+ target
278
278
} = info ;
279
279
280
280
var cursor = null ;
0 commit comments