@@ -60,11 +60,11 @@ if (helper.sequelize.dialect.name === 'postgres') {
6060
6161 } ) ;
6262
63- this . User . Tasks = this . User . hasMany ( this . Task , { as : 'tasks' } ) ;
63+ this . User . Tasks = this . User . hasMany ( this . Task , { as : 'tasks' , foreignKey : 'userId' } ) ;
6464 this . User . Projects = this . User . belongsToMany ( this . Project , { as : 'projects' , through : this . ProjectMember } ) ;
6565
66- this . Project . Tasks = this . Project . hasMany ( this . Task , { as : 'tasks' } ) ;
67- this . Task . Project = this . Task . belongsTo ( this . Project , { as : 'project' } ) ;
66+ this . Project . Tasks = this . Project . hasMany ( this . Task , { as : 'tasks' , foreignKey : 'projectId' } ) ;
67+ this . Task . Project = this . Task . belongsTo ( this . Project , { as : 'project' , foreignKey : 'projectId' } ) ;
6868
6969 this . taskType = new GraphQLObjectType ( {
7070 name : this . Task . name ,
@@ -182,6 +182,33 @@ if (helper.sequelize.dialect.name === 'postgres') {
182182 }
183183 } ) ;
184184
185+ this . viewerTaskConnection = sequelizeConnection ( {
186+ name : 'Viewer' + this . Task . name ,
187+ nodeType : this . taskType ,
188+ target : this . Task ,
189+ orderBy : new GraphQLEnumType ( {
190+ name : 'Viewer' + this . Task . name + 'ConnectionOrder' ,
191+ values : {
192+ ID : { value : [ this . Task . primaryKeyAttribute , 'ASC' ] } ,
193+ }
194+ } ) ,
195+ before : ( options , args , root ) => {
196+ options . where = options . where || { } ;
197+ options . where . userId = root . viewer . get ( 'id' ) ;
198+ return options ;
199+ }
200+ } ) ;
201+ this . viewerType = new GraphQLObjectType ( {
202+ name : 'Viewer' ,
203+ fields : {
204+ tasks : {
205+ type : this . viewerTaskConnection . connectionType ,
206+ args : this . viewerTaskConnection . connectionArgs ,
207+ resolve : this . viewerTaskConnection . resolve
208+ }
209+ }
210+ } ) ;
211+
185212 this . schema = new GraphQLSchema ( {
186213 query : new GraphQLObjectType ( {
187214 name : 'RootQueryType' ,
@@ -194,15 +221,22 @@ if (helper.sequelize.dialect.name === 'postgres') {
194221 }
195222 } ,
196223 resolve : resolver ( this . User )
197- }
224+ } ,
225+ viewer : {
226+ type : this . viewerType ,
227+ resolve : function ( source , args , info ) {
228+ return info . rootValue . viewer ;
229+ }
230+ } ,
198231 }
199232 } )
200233 } ) ;
201234
202235 await this . sequelize . sync ( { force : true } ) ;
203236
204- let taskId = 0
205- , now = new Date ( 2015 , 10 , 17 , 3 , 24 , 0 , 0 ) ;
237+ let now = new Date ( 2015 , 10 , 17 , 3 , 24 , 0 , 0 ) ;
238+
239+ this . taskId = 0 ;
206240
207241 [ this . projectA , this . projectB ] = await Promise . join (
208242 this . Project . create ( { } ) ,
@@ -211,15 +245,15 @@ if (helper.sequelize.dialect.name === 'postgres') {
211245
212246 this . userA = await this . User . create ( {
213247 [ this . User . Tasks . as ] : [
214- { id : ++ taskId , name : 'AAA' , createdAt : new Date ( now - 45000 ) , projectId : this . projectA . get ( 'id' ) , completed : false } ,
215- { id : ++ taskId , name : 'ABA' , createdAt : new Date ( now - 40000 ) , projectId : this . projectA . get ( 'id' ) , completed : true } ,
216- { id : ++ taskId , name : 'ABC' , createdAt : new Date ( now - 35000 ) , projectId : this . projectA . get ( 'id' ) , completed : true } ,
217- { id : ++ taskId , name : 'ABC' , createdAt : new Date ( now - 30000 ) , projectId : this . projectA . get ( 'id' ) , completed : false } ,
218- { id : ++ taskId , name : 'BAA' , createdAt : new Date ( now - 25000 ) , projectId : this . projectA . get ( 'id' ) , completed : false } ,
219- { id : ++ taskId , name : 'BBB' , createdAt : new Date ( now - 20000 ) , projectId : this . projectB . get ( 'id' ) , completed : true } ,
220- { id : ++ taskId , name : 'CAA' , createdAt : new Date ( now - 15000 ) , projectId : this . projectB . get ( 'id' ) , completed : true } ,
221- { id : ++ taskId , name : 'CCC' , createdAt : new Date ( now - 10000 ) , projectId : this . projectB . get ( 'id' ) , completed : false } ,
222- { id : ++ taskId , name : 'DDD' , createdAt : new Date ( now - 5000 ) , projectId : this . projectB . get ( 'id' ) , completed : false }
248+ { id : ++ this . taskId , name : 'AAA' , createdAt : new Date ( now - 45000 ) , projectId : this . projectA . get ( 'id' ) , completed : false } ,
249+ { id : ++ this . taskId , name : 'ABA' , createdAt : new Date ( now - 40000 ) , projectId : this . projectA . get ( 'id' ) , completed : true } ,
250+ { id : ++ this . taskId , name : 'ABC' , createdAt : new Date ( now - 35000 ) , projectId : this . projectA . get ( 'id' ) , completed : true } ,
251+ { id : ++ this . taskId , name : 'ABC' , createdAt : new Date ( now - 30000 ) , projectId : this . projectA . get ( 'id' ) , completed : false } ,
252+ { id : ++ this . taskId , name : 'BAA' , createdAt : new Date ( now - 25000 ) , projectId : this . projectA . get ( 'id' ) , completed : false } ,
253+ { id : ++ this . taskId , name : 'BBB' , createdAt : new Date ( now - 20000 ) , projectId : this . projectB . get ( 'id' ) , completed : true } ,
254+ { id : ++ this . taskId , name : 'CAA' , createdAt : new Date ( now - 15000 ) , projectId : this . projectB . get ( 'id' ) , completed : true } ,
255+ { id : ++ this . taskId , name : 'CCC' , createdAt : new Date ( now - 10000 ) , projectId : this . projectB . get ( 'id' ) , completed : false } ,
256+ { id : ++ this . taskId , name : 'DDD' , createdAt : new Date ( now - 5000 ) , projectId : this . projectB . get ( 'id' ) , completed : false }
223257 ]
224258 } , {
225259 include : [ this . User . Tasks ]
@@ -626,6 +660,47 @@ if (helper.sequelize.dialect.name === 'postgres') {
626660 expect ( result . data . user . tasks . totalCount ) . to . equal ( 0 ) ;
627661 expect ( result . data . user . tasks . pageInfo . hasNextPage ) . to . equal ( false ) ;
628662 } ) ;
663+
664+ it ( 'should support model connections' , async function ( ) {
665+ let viewer = await this . User . create ( ) ;
666+
667+ let tasks = await Promise . join (
668+ viewer . createTask ( {
669+ id : ++ this . taskId
670+ } ) ,
671+ viewer . createTask ( {
672+ id : ++ this . taskId
673+ } ) ,
674+ this . Task . create ( {
675+ id : ++ this . taskId
676+ } )
677+ ) ;
678+
679+ let result = await graphql ( this . schema , `
680+ {
681+ viewer {
682+ tasks {
683+ edges {
684+ cursor
685+ node {
686+ id
687+ name
688+ }
689+ }
690+ }
691+ }
692+ }
693+ ` , {
694+ viewer : viewer
695+ } ) ;
696+
697+ expect ( result . data . viewer . tasks . edges . length ) . to . equal ( 2 ) ;
698+ expect (
699+ result . data . viewer . tasks . edges . map ( edge => fromGlobalId ( edge . node . id ) . id ) . sort ( )
700+ ) . deep . equal (
701+ tasks . slice ( 0 , 2 ) . map ( task => task . get ( 'id' ) . toString ( ) ) . sort ( )
702+ ) ;
703+ } ) ;
629704 } ) ;
630705 } ) ;
631706}
0 commit comments