Skip to content

Commit 1cedd33

Browse files
committed
feat: add defaultsAsNonNull options to composeMongoose method which makes all fields with default values as NonNull in GraphQL Schema
closes #261
1 parent 892a92f commit 1cedd33

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/__tests__/github_issues/261-test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ interface IUser extends Document {
3232
}
3333

3434
const UserModel = mongoose.model<IUser>('User', UserSchema);
35-
const UserTC = composeMongoose(UserModel, { schemaComposer });
35+
const UserTC = composeMongoose(UserModel, { schemaComposer, defaultsAsNonNull: true });
3636

3737
schemaComposer.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({

src/composeMongoose.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export type ComposeMongooseOpts<TContext> = {
2525
remove?: string[];
2626
};
2727
inputType?: TypeConverterInputTypeOpts;
28+
/**
29+
* You can make fields as NonNull if they have default value in mongoose model.
30+
*/
31+
defaultsAsNonNull?: boolean;
2832
};
2933

3034
export type GenerateResolverType<TDoc extends Document, TContext = any> = {
@@ -84,7 +88,9 @@ export function composeMongoose<TDoc extends Document, TContext = any>(
8488
// but do it AFTER input object type generation
8589
// NonNull fields !== Required field
8690
// default values should not affect on that input fields became required
87-
makeFieldsNonNullWithDefaultValues(tc);
91+
if (opts.defaultsAsNonNull) {
92+
makeFieldsNonNullWithDefaultValues(tc);
93+
}
8894

8995
tc.makeFieldNonNull('_id');
9096

0 commit comments

Comments
 (0)