| 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 |
10x
10x
10x
10x
10x
10x
10x
10x
10x
10x
15x
2x
13x
13x
10x
132x
13x
119x
11x
108x
108x
107x
1x
10x
77x
76x
1x
75x
10x
72x
27x
27x
45x
72x
25x
19x
19x
50x
50x
1x
49x
19x
10x
26x
1x
10x
7x
4x
4x
3x
7x
10x
1x
1x
1x
1x
10x
4x
1x
10x
9x
9x
9x
9x
10x
21x
7x
14x
10x
85x
56x
29x
10x
2x
10x
53x
18x
18x
12x
12x
6x
6x
10x
2x
1x
1x
1x
| /**
* Copyright (c) 2017-present, Graphene.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import "reflect-metadata";
import {
GraphQLType,
GraphQLString,
GraphQLFloat,
GraphQLList,
GraphQLBoolean,
assertType,
isType,
GraphQLFieldConfig,
GraphQLInputFieldConfig
} from "graphql";
import { GraphQLDateTime } from "graphql-iso-date";
export const GRAPHENE_TYPE_METADATA_KEY = "graphene:type";
export const GRAPHENE_FIELDS_METADATA_KEY = "graphene:fields";
export const GRAPHENE_INPUTFIELDS_METADATA_KEY = "graphene:inputfields";
export const GRAPHENE_DESCRIPTION_KEY = "graphene:description";
export const GRAPHENE_DEPRECATED_KEY = "graphene:deprecated";
// A utility funciton to convert values like:
// [String]
// To:
// GraphQLList(GraphQLString)
export const convertArrayToGraphQLList = (
target: Array<any>
): GraphQLList<GraphQLType> => {
if (target.length !== 1) {
throw new Error(
`Graphene Array definitions should contain exactly one element. Received ${
target.length
} elements.`
);
}
const innerType = getGraphQLType(target[0]);
return new GraphQLList(innerType);
};
// A utility funciton to convert values like:
// String
// To:
// GraphQLString
// Note: for String -> GraphQLString conversion to work we have to call
// setupNativeTypes
export const getGraphQLType = (target: any): GraphQLType => {
if (Array.isArray(target)) {
return convertArrayToGraphQLList(target);
}
if (isType(target)) {
return target;
}
const metadataType: GraphQLType = Reflect.getMetadata(
GRAPHENE_TYPE_METADATA_KEY,
target
);
if (metadataType) {
return metadataType;
}
throw new Error(
`The target ${target} have no GraphQL type associated to it.`
);
};
// An utility function to associate a GraphQL type
// with the specified target.
// This method takes full advantage of the Reflection API.
export const setGraphQLType = (target: any, type: GraphQLType): void => {
// Is the provided type a GraphQLType?
// Will fail if not.
assertType(type);
// First we check this type have no other type associated with it.
if (Reflect.hasMetadata(GRAPHENE_TYPE_METADATA_KEY, target)) {
throw new Error(
`Type ${String(target)} already have a Graphene type attached.`
);
}
// We define the type metadata through reflection
Reflect.defineMetadata(GRAPHENE_TYPE_METADATA_KEY, type, target);
};
// Field definitions
export type UnmountedFieldMap = {
[key: string]: () => GraphQLFieldConfig<any, any> | null;
};
export type MountedFieldMap = {
[key: string]: GraphQLFieldConfig<any, any>;
};
// Get the fields given a constructor
export const getFields = (target: any): UnmountedFieldMap => {
let fields: UnmountedFieldMap;
if (!Reflect.hasMetadata(GRAPHENE_FIELDS_METADATA_KEY, target)) {
fields = {};
Reflect.defineMetadata(GRAPHENE_FIELDS_METADATA_KEY, fields, target);
} else {
fields = Reflect.getMetadata(GRAPHENE_FIELDS_METADATA_KEY, target);
}
return fields;
};
// mountFields
export const mountFields = (
fields: UnmountedFieldMap
) => (): MountedFieldMap => {
let finalFields: MountedFieldMap = {};
for (let key in fields) {
let field = fields[key]();
if (!field) {
continue;
}
finalFields[key] = field;
}
return finalFields;
};
export const assertFields = (target: any, fields: UnmountedFieldMap) => {
if (Object.keys(fields).length === 0) {
throw new Error(`Type ${target} must have fields defined on it.`);
}
};
// InputFieldDefinitions
export type UnmountedInputFieldMap = {
[key: string]: () => GraphQLInputFieldConfig;
};
export type MountedInputFieldMap = {
[key: string]: GraphQLInputFieldConfig;
};
// Get the fields given a constructor
export const getInputFields = (target: any): UnmountedInputFieldMap => {
let fields: UnmountedInputFieldMap;
if (!Reflect.hasMetadata(GRAPHENE_INPUTFIELDS_METADATA_KEY, target)) {
fields = {};
Reflect.defineMetadata(GRAPHENE_INPUTFIELDS_METADATA_KEY, fields, target);
} else {
fields = Reflect.getMetadata(GRAPHENE_INPUTFIELDS_METADATA_KEY, target);
}
return fields;
};
// mountInputFields
export const mountInputFields = (
fields: UnmountedInputFieldMap
) => (): MountedInputFieldMap => {
let finalFields: MountedInputFieldMap = {};
for (let key in fields) {
finalFields[key] = fields[key]();
}
return finalFields;
};
export const assertInputFields = (
target: any,
fields: UnmountedInputFieldMap
) => {
if (Object.keys(fields).length === 0) {
throw new Error(`Type ${target} must have fields defined on it.`);
}
};
// The setup function that permit us to use
// the native String and Number types.
export const setupNativeTypes = () => {
setGraphQLType(String, GraphQLString);
setGraphQLType(Boolean, GraphQLBoolean);
setGraphQLType(Number, GraphQLFloat);
setGraphQLType(Date, GraphQLDateTime);
};
// Description setter
// Usage:
// setDescription(MyClass, 'the description')
// Or:
// setDescription(MyClass, 'methodName', 'the description')
export const setDescription = (
target: any,
keyOrDescription: string,
description?: string
) => {
if (typeof description === "undefined") {
Reflect.defineMetadata(GRAPHENE_DESCRIPTION_KEY, keyOrDescription, target);
} else {
Reflect.defineMetadata(
GRAPHENE_DESCRIPTION_KEY,
description,
target,
keyOrDescription
);
}
};
// Description getter
// Usage:
// getDescription(MyClass)
// Or:
// setDescription(MyClass, 'methodName')
export const getDescription = (target: any, key?: string) => {
if (key) {
return Reflect.getMetadata(GRAPHENE_DESCRIPTION_KEY, target, key);
}
return Reflect.getMetadata(GRAPHENE_DESCRIPTION_KEY, target);
};
// Deprecation reason setter
// Usage:
// setDeprecationReason(MyClass, 'methodName', 'this method is now deprecated')
export const setDeprecationReason = (
target: any,
key: string,
reason: string
) => {
Reflect.defineMetadata(GRAPHENE_DEPRECATED_KEY, reason, target, key);
};
// Deprecation reason getter
// Usage:
// getDeprecationReason(MyClass, 'methodName')
export const getDeprecationReason = (target: any, key: string) => {
return Reflect.getMetadata(GRAPHENE_DEPRECATED_KEY, target, key);
};
// Setup the decorators
// Description decorator.
// This decorator permits us to decorate classes, like:
//
// @description('The class description')
// class X {}
//
// Or the class methods, like:
//
// class X{
// @description('The method description')
// myMethod() {}
// }
export const description = (description: string) => (
target: any,
key?: string,
descriptor?: PropertyDescriptor
) => {
if (typeof key !== "undefined") {
// It's a decorated method
setDescription(target, key, description);
return descriptor;
}
// It's a decorated class
setDescription(target, description);
return target;
};
// This decorator permits us to mark fields as deprecated, like:
//
// class X{
// @deprecated('This method is deprecated')
// myMethod() {}
// }
export const deprecated = (reason: string) => <MethodDecorator>(
target: any,
key?: string,
descriptor?: any
) => {
if (typeof key !== "undefined") {
// It's a decorated method
setDeprecationReason(target, key, reason);
return descriptor;
}
// It's a decorated class
throw new Error(
`Classes can't be decorated with the @deprecated decorator. Received ${target}.`
);
};
|