Skip to content

Commit 091e3d2

Browse files
committed
refactor: make defaultValue 'recursive' – if embedded objct has defaultValue then this field became NonNull
see details here: #261 (comment) closes #261
1 parent 93f265d commit 091e3d2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/__tests__/github_issues/261-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('issue #261 - Non-nullability for mongoose fields that have a default v
6868
name: String!
6969
age: Float
7070
isActive: Boolean!
71-
analytics: UserAnalytics
71+
analytics: UserAnalytics!
7272
periods: [UserPeriods]!
7373
}
7474

src/composeMongoose.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,25 @@ function makeFieldsNonNullWithDefaultValues(
102102
if (alreadyWorked.has(tc)) return;
103103
alreadyWorked.add(tc);
104104

105+
let hasFieldsWithDefaultValues = false;
105106
tc.getFieldNames().forEach((fieldName) => {
106107
const fc = tc.getField(fieldName);
107-
// traverse nested Objects
108+
// traverse nested Object types
108109
if (fc.type instanceof ObjectTypeComposer) {
109110
makeFieldsNonNullWithDefaultValues(fc.type);
111+
if (fc.type.getExtension('hasFieldsWithDefaultValue')) {
112+
tc.makeFieldNonNull(fieldName);
113+
}
110114
}
115+
111116
const defaultValue = fc?.extensions?.defaultValue;
112117
if (defaultValue !== null && defaultValue !== undefined) {
118+
hasFieldsWithDefaultValues = true;
113119
tc.makeFieldNonNull(fieldName);
114120
}
115121
});
122+
123+
if (hasFieldsWithDefaultValues) {
124+
tc.setExtension('hasFieldsWithDefaultValue', true);
125+
}
116126
}

0 commit comments

Comments
 (0)