Skip to content

Commit 3883e7a

Browse files
committed
fix(FieldsConverter): Translate embedded docs value to vanilla object for proper isNullish graphql
Thanks @yoadsn for deep analysis Closes #7
1 parent 1bf65f7 commit 3883e7a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/__tests__/fieldConverter-test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,81 @@ describe('fieldConverter', () => {
178178
it('should skip pseudo mongoose _id field', () => {
179179
expect(embeddedFields._id).to.be.undefined;
180180
});
181+
182+
it('should return null if subdocument is empty', async () => {
183+
const UserTC = composeWithMongoose(UserModel);
184+
const schema = new GraphQLSchema({
185+
query: new GraphQLObjectType({
186+
name: 'Query',
187+
fields: {
188+
user: UserTC.getResolver('findById').getFieldConfig(),
189+
},
190+
}),
191+
});
192+
193+
194+
const user = new UserModel({
195+
name: 'Test empty subDoc',
196+
});
197+
await user.save();
198+
const result = await graphql(schema, `{
199+
user(_id: "${user._id}") {
200+
name
201+
subDoc {
202+
field1
203+
field2 {
204+
field21
205+
}
206+
}
207+
}
208+
}`);
209+
expect(result).deep.property('data.user').to.deep.equal({
210+
name: 'Test empty subDoc',
211+
subDoc: null,
212+
});
213+
});
214+
215+
it('should return subdocument if it is non-empty', async () => {
216+
const UserTC = composeWithMongoose(UserModel);
217+
// UserTC.get('$findById.subDoc').extendField('field2', {
218+
// resolve: (source) => {
219+
// console.log('$findById.subDoc.field2 source:', source)
220+
// return source.field2;
221+
// }
222+
// })
223+
const schema = new GraphQLSchema({
224+
query: new GraphQLObjectType({
225+
name: 'Query',
226+
fields: {
227+
user: UserTC.getResolver('findById').getFieldConfig(),
228+
},
229+
}),
230+
});
231+
232+
const user2 = new UserModel({
233+
name: 'Test non empty subDoc',
234+
subDoc: { field2: { field21: 'ok' } },
235+
});
236+
await user2.save();
237+
const result2 = await graphql(schema, `{
238+
user(_id: "${user2._id}") {
239+
name
240+
subDoc {
241+
field1
242+
field2 {
243+
field21
244+
}
245+
}
246+
}
247+
}`);
248+
expect(result2).deep.property('data.user').to.deep.equal({
249+
name: 'Test non empty subDoc',
250+
subDoc: {
251+
field1: null,
252+
field2: { field21: 'ok' },
253+
},
254+
});
255+
});
181256
});
182257

183258
describe('documentArrayToGraphQL()', () => {

src/fieldsConverter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ export function convertModelToGraphQL(
153153
type: convertFieldToGraphQL(mongooseField, typeName),
154154
description: _getFieldDescription(mongooseField),
155155
};
156+
157+
if (deriveComplexType(mongooseField) === ComplexTypes.EMBEDDED) {
158+
// https://github.com/nodkz/graphql-compose-mongoose/issues/7
159+
graphqlFields[fieldName].resolve = (source) => {
160+
if (source) {
161+
if (source.toObject) {
162+
const obj = source.toObject();
163+
return obj[fieldName];
164+
}
165+
return source[fieldName];
166+
}
167+
return null;
168+
};
169+
}
156170
});
157171

158172
typeComposer.addFields(graphqlFields);

0 commit comments

Comments
 (0)