-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreflection.util.ts
More file actions
82 lines (68 loc) · 2.28 KB
/
Copy pathreflection.util.ts
File metadata and controls
82 lines (68 loc) · 2.28 KB
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
import 'reflect-metadata';
import { ArgumentValue, BooleanArgumentValue } from './ast.util';
import { Newable } from './class.util';
export const fieldMetadataKey = Symbol('fields');
export const argsMetadataKey = Symbol('args');
export const argsTypeMetadataKey = Symbol('argType');
/**
* Metadata stored for a field
*
*/
export type FieldMetadata<T, FieldArgs = any, QueryVars = any> = {
type: Newable<any>,
options: FieldOptions<T, FieldArgs, QueryVars>
}
/**
* Available options for field definition
*/
export type FieldOptions<Parent = any, FieldArgs = any, QueryVars = any> = {
/**
* Field alias. Should be used to map a field which is not part of the GraphQL schema to a field which is part of the schema
*/
aliasFor?: keyof Parent & string;
/**
* Whether to skip the field. If specified, a @skip() directive will be included in the query. The property value is used as the value of the if argument of the directive.
*/
skip?: BooleanArgumentValue<QueryVars>;
/**
* Whether to include the field. If specified, a @include() directive will be included in the query. The property value is used as the value of the if argument of the directive.
*/
include?: BooleanArgumentValue<QueryVars>;
/**
* Map between field arguments and query variables or actual values
*/
args?: {[key in keyof FieldArgs]: ArgumentValue<QueryVars>}
}
/**
* Metadata stored for an ObjectType class
*/
export type ObjectTypeMetadata<T> = Map<string, FieldMetadata<T>>;
/**
* Metadata stored for an argument
*/
export type ArgMetadata = {
type: Newable<any>,
options: ArgOptions
};
/**
* Metadata stored for an ArgsType class
*/
export type ArgObjectMetadata = Map<string, ArgMetadata>;
/**
* Available option for argument definition
*/
export type ArgOptions = {
/**
* Whether to mark the field as nullable
*/
nullable?: boolean;
}
export function getFieldMetadata<T>(t: Newable<T>): ObjectTypeMetadata<T> {
return Reflect.getMetadata(fieldMetadataKey, new t()) ?? new Map<String, FieldMetadata<T>>();
}
export function getArgsMetadata(target): ArgObjectMetadata {
return Reflect.getMetadata(argsMetadataKey, target) ?? new Map<String, ArgMetadata>();
}
export function getArgsTypeMetadata(target): string {
return Reflect.getMetadata(argsTypeMetadataKey, target);
}