Skip to content

Commit

Permalink
fix: defaultsAsNonNull option now skips isSingleNested mongoose s…
Browse files Browse the repository at this point in the history
…chemas (because such sub-schemas break the "recursive default value assignation" behavior)

see #358

closes #358
  • Loading branch information
nodkz committed Jul 25, 2021
1 parent c18e675 commit 8bb278e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/__tests__/github_issues/358-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { composeMongoose } from '../../index';
import { mongoose } from '../../__mocks__/mongooseCommon';

// mongoose.set('debug', true);

const WithSubSchema = new mongoose.Schema({
subDocument: new mongoose.Schema({
field: { type: String, default: 'Hey' },
}),
});

const WithoutSubSchema = new mongoose.Schema({
subDocument: {
field: { type: String, default: 'Hey' },
},
});

const WithSubModel = mongoose.model('WithSubModel', WithSubSchema);
const WithoutSubModel = mongoose.model('WithoutSubModel', WithoutSubSchema);

describe('defaultsAsNonNull falsely reports non-nullability for subdocuments that have a Schema - issue #358', () => {
it('with sub schema', async () => {
const WithSubTC = composeMongoose(WithSubModel, { defaultsAsNonNull: true });

// sub-Schema breaks the "recursive default value assignation" behavior
const data = new WithSubModel().subDocument;
expect(data).toEqual(undefined);

// so field should not be non-null
expect(WithSubTC.getFieldTypeName('subDocument')).toBe('WithSubModelSubDocument');
});

it('as nested fields', async () => {
const WithoutSubTC = composeMongoose(WithoutSubModel, { defaultsAsNonNull: true });

const data = new WithoutSubModel().subDocument;
expect(data).toEqual({ field: 'Hey' });

// should be non-null!
expect(WithoutSubTC.getFieldTypeName('subDocument')).toBe('WithoutSubModelSubDocument!');
});
});
6 changes: 6 additions & 0 deletions src/composeMongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ function makeFieldsNonNullWithDefaultValues(
const fc = tc.getField(fieldName);
// traverse nested Object types
if (fc.type instanceof ObjectTypeComposer) {
if (fc.extensions?.isSingleNestedMongooseSchema) {
// sub-Schema breaks the "recursive default value assignation" behavior
// @see https://github.com/graphql-compose/graphql-compose-mongoose/issues/358
return;
}

makeFieldsNonNullWithDefaultValues(fc.type);
if (fc.type.getExtension('hasFieldsWithDefaultValue')) {
tc.makeFieldNonNull(fieldName);
Expand Down
6 changes: 6 additions & 0 deletions src/fieldsConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ export function convertModelToGraphQL<TDoc extends Document, TContext>(
description: _getFieldDescription(mongooseField),
};

if (mongooseField?.schema && (mongooseField as any)?.$isSingleNested) {
graphqlFields[fieldName].extensions = {
isSingleNestedMongooseSchema: true,
};
}

if (mongooseField?.defaultValue !== null && mongooseField?.defaultValue !== undefined) {
if (!graphqlFields[fieldName].extensions) graphqlFields[fieldName].extensions = {};
(graphqlFields as any)[fieldName].extensions.defaultValue = mongooseField?.defaultValue;
Expand Down

0 comments on commit 8bb278e

Please sign in to comment.