@@ -12,15 +12,14 @@ export default function generateIncludes(simpleAST, type, context, options) {
1212 type = type . ofType || type ;
1313 options = options || { } ;
1414
15- Object . keys ( simpleAST . fields ) . forEach ( function ( key ) {
15+ return Promise . all ( Object . keys ( simpleAST . fields ) . map ( function ( key ) {
1616 var association
1717 , fieldAST = simpleAST . fields [ key ]
1818 , name = fieldAST . key || key
1919 , fieldType = type . _fields [ name ] && type . _fields [ name ] . type
2020 , includeOptions
2121 , args = fieldAST . args
2222 , includeResolver = type . _fields [ name ] . resolve
23- , nestedResult
2423 , allowedAttributes
2524 , include ;
2625
@@ -38,22 +37,21 @@ export default function generateIncludes(simpleAST, type, context, options) {
3837 }
3938
4039 if ( ! fieldAST ) {
41- // No point in ncluding if no fields have been asked for
40+ // No point in including if no fields have been asked for
4241 return ;
4342 }
4443
4544 if ( includeResolver . $passthrough ) {
46- var dummyResult = generateIncludes (
45+ return generateIncludes (
4746 fieldAST ,
4847 fieldType ,
4948 context ,
5049 options
51- ) ;
52-
53- result . include = result . include . concat ( dummyResult . include ) ;
54- result . attributes = result . attributes . concat ( dummyResult . attributes ) ;
55- result . order = result . order . concat ( dummyResult . order ) ;
56- return ;
50+ ) . then ( function ( dummyResult ) {
51+ result . include = result . include . concat ( dummyResult . include ) ;
52+ result . attributes = result . attributes . concat ( dummyResult . attributes ) ;
53+ result . order = result . order . concat ( dummyResult . order ) ;
54+ } ) ;
5755 }
5856
5957 association = includeResolver . $association ;
@@ -63,67 +61,69 @@ export default function generateIncludes(simpleAST, type, context, options) {
6361 includeOptions = argsToFindOptions ( args , association . target ) ;
6462 allowedAttributes = Object . keys ( association . target . rawAttributes ) ;
6563
66-
6764 if ( options . filterAttributes ) {
6865 includeOptions . attributes = ( includeOptions . attributes || [ ] )
69- . concat ( Object . keys ( fieldAST . fields ) . map ( key => fieldAST . fields [ key ] . key || key ) )
70- . filter ( inList . bind ( null , allowedAttributes ) ) ;
66+ . concat ( Object . keys ( fieldAST . fields ) . map ( key => fieldAST . fields [ key ] . key || key ) )
67+ . filter ( inList . bind ( null , allowedAttributes ) ) ;
7168 } else {
7269 includeOptions . attributes = allowedAttributes ;
7370 }
7471
75- if ( includeResolver . $before ) {
76- includeOptions = includeResolver . $before ( includeOptions , args , context , {
77- ast : fieldAST ,
78- type : type
79- } ) ;
80- }
72+ return Promise . resolve ( ) . then ( function ( ) {
73+ if ( includeResolver . $before ) {
74+ return includeResolver . $before ( includeOptions , args , context , {
75+ ast : fieldAST ,
76+ type : type
77+ } ) ;
78+ }
79+ return includeOptions ;
80+ } ) . then ( function ( includeOptions ) {
81+ if ( association . associationType === 'BelongsTo' ) {
82+ result . attributes . push ( association . foreignKey ) ;
83+ } else if ( association . source . primaryKeyAttribute ) {
84+ result . attributes . push ( association . source . primaryKeyAttribute ) ;
85+ }
8186
82- if ( association . associationType === 'BelongsTo' ) {
83- result . attributes . push ( association . foreignKey ) ;
84- } else if ( association . source . primaryKeyAttribute ) {
85- result . attributes . push ( association . source . primaryKeyAttribute ) ;
86- }
87+ let separate = includeOptions . limit && association . associationType === 'HasMany' ;
8788
88- let separate = includeOptions . limit && association . associationType === 'HasMany' ;
89+ if ( include && ( ! includeOptions . limit || separate ) ) {
90+ if ( includeOptions . order && ! separate ) {
91+ includeOptions . order . map ( function ( order ) {
92+ order . unshift ( {
93+ model : association . target ,
94+ as : association . options . as
95+ } ) ;
8996
90- if ( include && ( ! includeOptions . limit || separate ) ) {
91- if ( includeOptions . order && ! separate ) {
92- includeOptions . order . map ( function ( order ) {
93- order . unshift ( {
94- model : association . target ,
95- as : association . options . as
97+ return order ;
9698 } ) ;
9799
98- return order ;
99- } ) ;
100-
101- result . order = ( result . order || [ ] ) . concat ( includeOptions . order ) ;
102- delete includeOptions . order ;
103- }
100+ result . order = ( result . order || [ ] ) . concat ( includeOptions . order ) ;
101+ delete includeOptions . order ;
102+ }
104103
105- if ( association . target . primaryKeyAttribute ) {
106- includeOptions . attributes . push ( association . target . primaryKeyAttribute ) ;
107- }
108-
109- if ( association . associationType === 'HasMany' ) {
110- includeOptions . attributes . push ( association . foreignKey ) ;
111- }
104+ if ( association . target . primaryKeyAttribute ) {
105+ includeOptions . attributes . push ( association . target . primaryKeyAttribute ) ;
106+ }
112107
113- nestedResult = generateIncludes (
114- fieldAST ,
115- fieldType ,
116- context ,
117- includeResolver . $options
118- ) ;
108+ if ( association . associationType === 'HasMany' ) {
109+ includeOptions . attributes . push ( association . foreignKey ) ;
110+ }
119111
120- includeOptions . include = ( includeOptions . include || [ ] ) . concat ( nestedResult . include ) ;
121- includeOptions . attributes = _ . uniq ( includeOptions . attributes . concat ( nestedResult . attributes ) ) ;
112+ return generateIncludes (
113+ fieldAST ,
114+ fieldType ,
115+ context ,
116+ includeResolver . $options
117+ ) . then ( function ( nestedResult ) {
118+ includeOptions . include = ( includeOptions . include || [ ] ) . concat ( nestedResult . include ) ;
119+ includeOptions . attributes = _ . uniq ( includeOptions . attributes . concat ( nestedResult . attributes ) ) ;
122120
123- result . include . push ( _ . assign ( { association : association } , includeOptions ) ) ;
124- }
121+ result . include . push ( _ . assign ( { association : association } , includeOptions ) ) ;
122+ } ) ;
123+ }
124+ } ) ;
125125 }
126+ } ) ) . then ( function ( ) {
127+ return result ;
126128 } ) ;
127-
128- return result ;
129129}
0 commit comments