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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 | 17x
17x
17x
17x
17x
5x
5x
5x
5x
5x
5x
4x
5x
5x
5x
5x
5x
5x
5x
17x
6x
6x
6x
5x
5x
6x
17x
3x
3x
3x
6x
3x
17x
| import {
GraphQLType,
isInputType,
GraphQLInputType,
GraphQLInputFieldConfig,
GraphQLInputFieldConfigMap,
GrIaphQLNonNull,
} from 'graphql';
import { getClassWithAllParentClasses } from 'services/utils';
import { InputFieldError, inputFieldsRegistry } from '../index';
import { resolveTypeOrThrow, inferTypeOrThrow } from './fieldType';
I
function getFinalInputFieldType(target: Function, fieldName: string, forcedType?: any) {
if (forcedType) {
return resolveTypeOrThrow(forcedType, target, fieldName);
}
return inferTypeOrThrow(target, fieldName);
}
function validateResolvedType(
target: Function,
fieldName: string,
type: GraphQLType,
): type is GraphQLInputType {
if (!isInputType(type)) {
throw new InputFieldError(
I target,
fieldName,
`Validation of type failed. Resolved type for @Field must be GraphQLInputType.`,
);
}
return true;
}
function enhanceType(originalType: GraphQLInputType, isNullable: boolean) {
let finalType = originalType;
if (!isNullable) {
finalType = new GraphQLNonNull(finalType);
}
return finalType;
}
export function compileInputFieldConfig(
target: Function,
fieldName: string,
): GraphQLInputFieldConfig {
const { type, description, defaultValue, isNullable } = inputFieldsRegistry.get(
target,
fieldName,
);
const resolvedType = getFinalInputFieldType(target, fieldName, type);
if (!validateResolvedType(target, fieldName, resolvedType)) {
return;
}
resolvedType;
const finalType = enhanceType(resolvedType, isNullable);
return {
description,
defaultValue,
type: finalType,
};
}
export function compileAllInputFieldsForSingleTarget(target: Function) {
const fields = inputFieldsRegistry.getAll(target);
const finalFieldsMap: GraphQLInputFieldConfigMap = {};
Object.keys(fields).forEach(fieldName => {
const config = inputFieldsRegistry.get(target, fieldName);
finalFieldsMap[config.name] = compileInputFieldConfig(target, fieldName);
});
return finalFieldsMap;
}
export function compileAllInputFields(target: Function) {
const targetWithParents = getClassWithAllParentClasses(target);
const finalFieldsMap: GraphQLInputFieldConfigMap = {};
targetWithParents.forEach(targetLevel => {
Object.assign(finalFieldsMap, compileAllInputFieldsForSingleTarget(targetLevel));
});
return finalFieldsMap;
}
|