|
| 1 | +import { SchemaComposer, dedent } from 'graphql-compose'; |
| 2 | +import { composeMongoose } from '../../index'; |
| 3 | +import { mongoose } from '../../__mocks__/mongooseCommon'; |
| 4 | +import { Document } from 'mongoose'; |
| 5 | +import { testFieldConfig } from '../../utils/testHelpers'; |
| 6 | + |
| 7 | +const schemaComposer = new SchemaComposer<{ req: any }>(); |
| 8 | + |
| 9 | +const UserSchema = new mongoose.Schema({ |
| 10 | + _id: { type: Number }, |
| 11 | + n: { |
| 12 | + type: String, |
| 13 | + alias: 'name', |
| 14 | + default: 'User', |
| 15 | + }, |
| 16 | + age: { type: Number }, |
| 17 | + isActive: { type: Boolean, default: false }, |
| 18 | + analytics: { |
| 19 | + isEnabled: { type: Boolean, default: false }, |
| 20 | + }, |
| 21 | + periods: [{ from: Number, to: Number, _id: false }], |
| 22 | +}); |
| 23 | +interface IUser extends Document { |
| 24 | + _id: number; |
| 25 | + name: string; |
| 26 | + age?: number; |
| 27 | + isActive: boolean; |
| 28 | + analytics: { |
| 29 | + isEnabled: boolean; |
| 30 | + }; |
| 31 | + periods: Array<{ from: number; to: number }>; |
| 32 | +} |
| 33 | + |
| 34 | +const UserModel = mongoose.model<IUser>('User', UserSchema); |
| 35 | +const UserTC = composeMongoose(UserModel, { schemaComposer }); |
| 36 | + |
| 37 | +schemaComposer.Query.addFields({ |
| 38 | + userById: UserTC.mongooseResolvers.findById(), |
| 39 | +}); |
| 40 | + |
| 41 | +// const schema = schemaComposer.buildSchema(); |
| 42 | +// console.log(schemaComposer.toSDL()); |
| 43 | + |
| 44 | +beforeAll(async () => { |
| 45 | + await UserModel.base.createConnection(); |
| 46 | + await UserModel.create({ _id: 1 } as any); |
| 47 | +}); |
| 48 | +afterAll(() => UserModel.base.disconnect()); |
| 49 | + |
| 50 | +describe('issue #261 - Non-nullability for mongoose fields that have a default value', () => { |
| 51 | + it('mongoose should hydrate doc with default values', async () => { |
| 52 | + const user1 = await UserModel.findById(1); |
| 53 | + expect(user1?.toObject({ virtuals: true })).toEqual( |
| 54 | + expect.objectContaining({ |
| 55 | + _id: 1, |
| 56 | + name: 'User', |
| 57 | + isActive: false, |
| 58 | + analytics: { isEnabled: false }, |
| 59 | + periods: [], |
| 60 | + }) |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('UserTC should have non-null fields if default value is provided', () => { |
| 65 | + expect(UserTC.toSDL({ deep: true, omitScalars: true })).toBe(dedent` |
| 66 | + type User { |
| 67 | + _id: Int! |
| 68 | + name: String! |
| 69 | + age: Float |
| 70 | + isActive: Boolean! |
| 71 | + analytics: UserAnalytics |
| 72 | + periods: [UserPeriods]! |
| 73 | + } |
| 74 | + |
| 75 | + type UserAnalytics { |
| 76 | + isEnabled: Boolean! |
| 77 | + } |
| 78 | + |
| 79 | + type UserPeriods { |
| 80 | + from: Float |
| 81 | + to: Float |
| 82 | + } |
| 83 | + `); |
| 84 | + }); |
| 85 | + |
| 86 | + it('check that graphql gets all default values', async () => { |
| 87 | + expect( |
| 88 | + await testFieldConfig({ |
| 89 | + field: UserTC.mongooseResolvers.findById(), |
| 90 | + args: { _id: 1 }, |
| 91 | + selection: `{ |
| 92 | + _id |
| 93 | + name |
| 94 | + age |
| 95 | + isActive |
| 96 | + analytics { |
| 97 | + isEnabled |
| 98 | + } |
| 99 | + periods { |
| 100 | + from |
| 101 | + to |
| 102 | + } |
| 103 | + }`, |
| 104 | + }) |
| 105 | + ).toEqual({ |
| 106 | + _id: 1, |
| 107 | + age: null, |
| 108 | + analytics: { isEnabled: false }, |
| 109 | + isActive: false, |
| 110 | + name: 'User', |
| 111 | + periods: [], |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments