@@ -178,6 +178,81 @@ describe('fieldConverter', () => {
178
178
it ( 'should skip pseudo mongoose _id field' , ( ) => {
179
179
expect ( embeddedFields . _id ) . to . be . undefined ;
180
180
} ) ;
181
+
182
+ it ( 'should return null if subdocument is empty' , async ( ) => {
183
+ const UserTC = composeWithMongoose ( UserModel ) ;
184
+ const schema = new GraphQLSchema ( {
185
+ query : new GraphQLObjectType ( {
186
+ name : 'Query' ,
187
+ fields : {
188
+ user : UserTC . getResolver ( 'findById' ) . getFieldConfig ( ) ,
189
+ } ,
190
+ } ) ,
191
+ } ) ;
192
+
193
+
194
+ const user = new UserModel ( {
195
+ name : 'Test empty subDoc' ,
196
+ } ) ;
197
+ await user . save ( ) ;
198
+ const result = await graphql ( schema , `{
199
+ user(_id: "${ user . _id } ") {
200
+ name
201
+ subDoc {
202
+ field1
203
+ field2 {
204
+ field21
205
+ }
206
+ }
207
+ }
208
+ }` ) ;
209
+ expect ( result ) . deep . property ( 'data.user' ) . to . deep . equal ( {
210
+ name : 'Test empty subDoc' ,
211
+ subDoc : null ,
212
+ } ) ;
213
+ } ) ;
214
+
215
+ it ( 'should return subdocument if it is non-empty' , async ( ) => {
216
+ const UserTC = composeWithMongoose ( UserModel ) ;
217
+ // UserTC.get('$findById.subDoc').extendField('field2', {
218
+ // resolve: (source) => {
219
+ // console.log('$findById.subDoc.field2 source:', source)
220
+ // return source.field2;
221
+ // }
222
+ // })
223
+ const schema = new GraphQLSchema ( {
224
+ query : new GraphQLObjectType ( {
225
+ name : 'Query' ,
226
+ fields : {
227
+ user : UserTC . getResolver ( 'findById' ) . getFieldConfig ( ) ,
228
+ } ,
229
+ } ) ,
230
+ } ) ;
231
+
232
+ const user2 = new UserModel ( {
233
+ name : 'Test non empty subDoc' ,
234
+ subDoc : { field2 : { field21 : 'ok' } } ,
235
+ } ) ;
236
+ await user2 . save ( ) ;
237
+ const result2 = await graphql ( schema , `{
238
+ user(_id: "${ user2 . _id } ") {
239
+ name
240
+ subDoc {
241
+ field1
242
+ field2 {
243
+ field21
244
+ }
245
+ }
246
+ }
247
+ }` ) ;
248
+ expect ( result2 ) . deep . property ( 'data.user' ) . to . deep . equal ( {
249
+ name : 'Test non empty subDoc' ,
250
+ subDoc : {
251
+ field1 : null ,
252
+ field2 : { field21 : 'ok' } ,
253
+ } ,
254
+ } ) ;
255
+ } ) ;
181
256
} ) ;
182
257
183
258
describe ( 'documentArrayToGraphQL()' , ( ) => {
0 commit comments