Skip to content

Commit bc08c66

Browse files
natac13nodkz
authored andcommitted
Created Projection Dotify function
1 parent 074e676 commit bc08c66

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/__tests__/github_issues/271-test.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,25 @@ describe('nested projection - issue #271', () => {
7777
contextValue: {},
7878
});
7979
console.log(JSON.stringify(result));
80-
// expect(BookModel.find).toHaveBeenCalledTimes(1);
81-
// expect(BookModel.find).toHaveBeenCalledWith(
82-
// {},
83-
// { limit: 1000, projection: { title: true, pageCount: true, 'author.name': true } }
84-
// );
80+
});
81+
82+
it('Handles a Fragment', async () => {
83+
const result = await graphql.graphql({
84+
schema,
85+
source: `
86+
fragment Auth on Author {
87+
name
88+
}
89+
90+
query {
91+
booksMany {
92+
title
93+
pageCount
94+
author { ...Auth }
95+
}
96+
}`,
97+
contextValue: {},
98+
});
99+
console.log(JSON.stringify(result));
85100
});
86101
});

src/resolvers/helpers/dotify.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const dotify = (object: Record<string, any>) => {
2+
const res: Record<string, number> = {};
3+
4+
function recurse(obj: Record<string, any>, current?: string) {
5+
const objKeys = Object.keys(obj);
6+
7+
objKeys.forEach((key: string) => {
8+
const value = obj[key];
9+
10+
const newKey = current ? `${current}.${key}` : key;
11+
if (value && (value.$meta || value.$slice || value.$elemMatch)) {
12+
// pass MongoDB projection operators https://docs.mongodb.com/v3.2/reference/operator/projection/meta/
13+
res[newKey] = 1;
14+
} else if (value && typeof value === 'object' && Object.keys(value).length > 0) {
15+
recurse(value, newKey);
16+
} else {
17+
res[newKey] = 1;
18+
}
19+
});
20+
}
21+
22+
recurse(object);
23+
return res;
24+
};
25+
26+
export default dotify;

src/resolvers/helpers/projection.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ExtendedResolveParams } from '../index';
22
import type { AliasesMap } from './aliases';
3+
import dotify from './dotify';
34

45
export function projectionHelper(
56
resolveParams: ExtendedResolveParams,
@@ -11,17 +12,7 @@ export function projectionHelper(
1112
if (projection['*']) {
1213
return;
1314
}
14-
const flatProjection: Record<string, any> = {};
15-
Object.keys(projection).forEach((key) => {
16-
const val = projection[key];
17-
if (val && (val.$meta || val.$slice || val.$elemMatch)) {
18-
// pass MongoDB projection operators https://docs.mongodb.com/v3.2/reference/operator/projection/meta/
19-
flatProjection[key] = val;
20-
} else {
21-
// if not projection operator, then flatten projection
22-
flatProjection[key] = !!val;
23-
}
24-
});
15+
const flatProjection: Record<string, any> = dotify(projection);
2516

2617
if (aliases) {
2718
Object.keys(flatProjection).forEach((k) => {

0 commit comments

Comments
 (0)