Skip to content

Commit 504aec0

Browse files
committed
feat(FieldConverter): now support deeply nested arrays
Will be returned type `[SomeDeepFieldPeriods]` instead of `JSON` for field `someDeepField`: ```js const UserSchema = new Schema({ someDeepField: { periods: [{ from: Number, to: Number }], }, }); ```
1 parent 0493811 commit 504aec0

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/__mocks__/userModel.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ const UserSchema = new Schema(
8585

8686
periods: [{ from: Number, to: Number }],
8787

88+
someDeep: {
89+
periods: [{ from: Number, to: Number }],
90+
},
91+
8892
// createdAt, created via option `timastamp: true` (see bottom)
8993
// updatedAt, created via option `timastamp: true` (see bottom)
9094
},

src/__tests__/__snapshots__/integration-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
exports[`integration tests projection should request all fields to rawData field: projection from all fields 1`] = `
44
Array [
5+
"someDeep",
56
"users",
67
"skills",
78
"employment",

src/__tests__/fieldConverter-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ describe('fieldConverter', () => {
195195
expect(embeddedFields.locationId).toBeTruthy();
196196
expect(embeddedFields._id).toBeTruthy();
197197
});
198+
199+
it('test object with field as array', () => {
200+
const someDeepTC = embeddedToGraphQL(fields.someDeep, '', schemaComposer);
201+
expect(someDeepTC.getTypeName()).toBe('SomeDeep');
202+
const periodsType = (someDeepTC.getField('periods'): any).type;
203+
expect(Array.isArray(periodsType)).toBeTruthy();
204+
expect(periodsType[0].getTypeName()).toBe('SomeDeepPeriods');
205+
expect(periodsType[0].hasField('from')).toBeTruthy();
206+
expect(periodsType[0].hasField('to')).toBeTruthy();
207+
});
198208
});
199209

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

src/fieldsConverter.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ export function deriveComplexType(field: MongooseFieldT): $Keys<typeof ComplexTy
226226
}
227227

228228
const fieldType = _getFieldType(field);
229-
230-
if (field instanceof mongoose.Schema.Types.DocumentArray) {
229+
if (
230+
field instanceof mongoose.Schema.Types.DocumentArray ||
231+
(fieldType === 'Array' && objectPath.has(field, 'schema.paths'))
232+
) {
231233
return ComplexTypes.DOCUMENT_ARRAY;
232234
} else if (field instanceof mongoose.Schema.Types.Embedded || fieldType === 'Embedded') {
233235
return ComplexTypes.EMBEDDED;
@@ -346,7 +348,10 @@ export function documentArrayToGraphQL(
346348
prefix?: string = '',
347349
schemaComposer: SchemaComposer<any>
348350
): [TypeComposer] {
349-
if (!(field instanceof mongoose.Schema.Types.DocumentArray)) {
351+
if (
352+
!(field instanceof mongoose.Schema.Types.DocumentArray) &&
353+
!objectPath.has(field, 'schema.paths')
354+
) {
350355
throw new Error(
351356
'You provide incorrect mongoose field to `documentArrayToGraphQL()`. ' +
352357
'Correct field should be instance of `mongoose.Schema.Types.DocumentArray`'
@@ -355,7 +360,7 @@ export function documentArrayToGraphQL(
355360

356361
const typeName = `${prefix}${upperFirst(_getFieldName(field))}`;
357362

358-
const tc = convertModelToGraphQL(field, typeName, schemaComposer);
363+
const tc = convertModelToGraphQL((field: any), typeName, schemaComposer);
359364

360365
return [tc];
361366
}

0 commit comments

Comments
 (0)