@@ -32,7 +32,7 @@ interface IUser extends Document {
3232}
3333
3434const UserModel = mongoose . model < IUser > ( 'User' , UserSchema ) ;
35- const UserTC = composeMongoose ( UserModel , { schemaComposer } ) ;
35+ const UserTC = composeMongoose ( UserModel , { schemaComposer, defaultsAsNonNull : true } ) ;
3636
3737schemaComposer . Query . addFields ( {
3838 userById : UserTC . mongooseResolvers . findById ( ) ,
@@ -61,7 +61,7 @@ describe('issue #261 - Non-nullability for mongoose fields that have a default v
6161 ) ;
6262 } ) ;
6363
64- it ( 'UserTC should have non-null fields if default value is provided' , ( ) => {
64+ it ( 'UserTC should have non-null fields if default value is provided and option `defaultsAsNonNull` ' , ( ) => {
6565 expect ( UserTC . toSDL ( { deep : true , omitScalars : true } ) ) . toBe ( dedent `
6666 type User {
6767 _id: Int!
@@ -83,6 +83,32 @@ describe('issue #261 - Non-nullability for mongoose fields that have a default v
8383 ` ) ;
8484 } ) ;
8585
86+ it ( 'UserTC should not have non-null fields which have default values' , ( ) => {
87+ const UserWithoutDefaultsTC = composeMongoose ( UserModel , {
88+ schemaComposer : new SchemaComposer ( ) ,
89+ name : 'UserWithoutDefaults' ,
90+ } ) ;
91+ expect ( UserWithoutDefaultsTC . toSDL ( { deep : true , omitScalars : true } ) ) . toBe ( dedent `
92+ type UserWithoutDefaults {
93+ _id: Int!
94+ name: String
95+ age: Float
96+ isActive: Boolean
97+ analytics: UserWithoutDefaultsAnalytics
98+ periods: [UserWithoutDefaultsPeriods]
99+ }
100+
101+ type UserWithoutDefaultsAnalytics {
102+ isEnabled: Boolean
103+ }
104+
105+ type UserWithoutDefaultsPeriods {
106+ from: Float
107+ to: Float
108+ }
109+ ` ) ;
110+ } ) ;
111+
86112 it ( 'check that graphql gets all default values' , async ( ) => {
87113 expect (
88114 await testFieldConfig ( {
0 commit comments