Skip to content

Commit ae34da9

Browse files
committed
fix: revert "Null fields in mongo return sub fields". It brokes aliases and old behavior was quite expensive with .toObject call.
Also if we take a look at `toObject` implementations then we can find that it calls minimize under the hood. So if you need the old behavior, just write your custom resolver for the field which you want to minimize. Related: #7
1 parent a198c70 commit ae34da9

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

src/__tests__/integration-test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('integration tests', () => {
1212
});
1313

1414
describe('check sub-documents', () => {
15-
it('should return null if sub-document is empty', async () => {
15+
it('should return subDoc fields even subDoc is empty itself', async () => {
1616
const UserTC = composeWithMongoose(UserModel);
1717
schemaComposer.Query.addFields({
1818
user: UserTC.getResolver('findById'),
@@ -41,11 +41,14 @@ describe('integration tests', () => {
4141

4242
expect(result.data.user).toEqual({
4343
name: 'Test empty subDoc',
44-
subDoc: null,
44+
subDoc: {
45+
field1: null,
46+
field2: { field21: null },
47+
},
4548
});
4649
});
4750

48-
it('should return sub-document if it is non-empty', async () => {
51+
it('should return subDoc fields if it is non-empty', async () => {
4952
const UserTC = composeWithMongoose(UserModel);
5053
// UserTC.get('$findById.subDoc').extendField('field2', {
5154
// resolve: (source) => {

src/fieldsConverter.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,6 @@ export function convertModelToGraphQL<TDoc extends Document, TContext>(
193193
if (!graphqlFields[fieldName].extensions) graphqlFields[fieldName].extensions = {};
194194
(graphqlFields as any)[fieldName].extensions.defaultValue = mongooseField?.defaultValue;
195195
}
196-
197-
if (deriveComplexType(mongooseField) === ComplexTypes.EMBEDDED) {
198-
// https://github.com/nodkz/graphql-compose-mongoose/issues/7
199-
graphqlFields[fieldName].resolve = (source) => {
200-
if (source) {
201-
if (source.toObject) {
202-
const obj = source.toObject();
203-
return obj[fieldName];
204-
}
205-
return source[fieldName];
206-
}
207-
return null;
208-
};
209-
}
210196
});
211197

212198
typeComposer.addFields(graphqlFields);

0 commit comments

Comments
 (0)