Skip to content

Commit a198c70

Browse files
natac13nodkz
authored andcommitted
Fix tests for virtual reference with nested fields
All passing.
1 parent bc08c66 commit a198c70

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/resolvers/helpers/__tests__/projection-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('Resolver helper `projection` ->', () => {
2828
it('should make projection fields flat', () => {
2929
resolveParams.projection = { name: { first: 1, last: 1 } };
3030
projectionHelper(resolveParams, { name: 'n' });
31-
expect(spyFn).toBeCalledWith({ n: true });
31+
expect(spyFn).toBeCalledWith({ 'n.first': true, 'n.last': true });
3232
});
3333

3434
it('should not call query.select if projection has * key', () => {

src/resolvers/helpers/dotify.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const dotify = (object: Record<string, any>) => {
2-
const res: Record<string, number> = {};
2+
const res: Record<string, boolean> = {};
33

44
function recurse(obj: Record<string, any>, current?: string) {
55
const objKeys = Object.keys(obj);
@@ -10,11 +10,11 @@ const dotify = (object: Record<string, any>) => {
1010
const newKey = current ? `${current}.${key}` : key;
1111
if (value && (value.$meta || value.$slice || value.$elemMatch)) {
1212
// pass MongoDB projection operators https://docs.mongodb.com/v3.2/reference/operator/projection/meta/
13-
res[newKey] = 1;
13+
res[newKey] = value;
1414
} else if (value && typeof value === 'object' && Object.keys(value).length > 0) {
1515
recurse(value, newKey);
1616
} else {
17-
res[newKey] = 1;
17+
res[newKey] = !!value;
1818
}
1919
});
2020
}

src/resolvers/helpers/projection.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ export function projectionHelper(
1616

1717
if (aliases) {
1818
Object.keys(flatProjection).forEach((k) => {
19-
if (aliases[k]) {
20-
flatProjection[aliases[k]] = true;
19+
const keys = k.split('.');
20+
const parentKey = keys[0];
21+
if (aliases[parentKey]) {
22+
const otherKeys = keys.slice(1);
23+
if (otherKeys.length === 0) {
24+
flatProjection[aliases[parentKey]] = true;
25+
} else {
26+
flatProjection[aliases[parentKey] + '.' + otherKeys.join('.')] = true;
27+
}
2128
delete flatProjection[k];
2229
}
2330
});

0 commit comments

Comments
 (0)