-
Notifications
You must be signed in to change notification settings - Fork 38
/
Fields.ts
53 lines (46 loc) · 1.44 KB
/
Fields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as graphqlFields from 'graphql-fields';
import { createParamDecorator } from 'type-graphql';
export function Fields(): ParameterDecorator {
return createParamDecorator(({ info }) => {
// This object will be of the form:
// rawFields {
// __objectType
// baseField: {},
// association: { subField: "foo"}
// }
// We pull out items with subFields
const rawFields = graphqlFields(info);
const scalars = Object.keys(rawFields).filter(item => {
return !item.startsWith('__');
});
return scalars;
});
}
export function RawFields(): ParameterDecorator {
return createParamDecorator(({ info }) => {
return graphqlFields(info);
});
}
export function NestedFields(): ParameterDecorator {
return createParamDecorator(({ info }) => {
// This object will be of the form:
// rawFields {
// baseField: {},
// association: { subField: "foo"}
// }
// We need to pull out items with subFields
const rawFields = graphqlFields(info);
const output: any = { scalars: [] };
for (const fieldKey in rawFields) {
if (Object.keys(rawFields[fieldKey]).length === 0) {
output.scalars.push(fieldKey);
} else {
const subFields = rawFields[fieldKey];
output[fieldKey] = Object.keys(subFields).filter(subKey => {
return Object.keys(subFields[subKey]).length === 0;
});
}
}
return output;
});
}