15
15
package graphql .annotations ;
16
16
17
17
import graphql .Scalars ;
18
- import graphql .schema .GraphQLEnumType ;
19
18
import graphql .schema .GraphQLList ;
20
19
import graphql .schema .GraphQLType ;
21
- import graphql .schema .GraphQLTypeReference ;
22
- import org .osgi .service .component .annotations .Activate ;
23
- import org .osgi .service .component .annotations .Component ;
24
- import org .osgi .service .component .annotations .Reference ;
25
- import org .osgi .service .component .annotations .ReferenceCardinality ;
26
- import org .osgi .service .component .annotations .ReferencePolicy ;
20
+ import org .osgi .service .component .annotations .*;
27
21
28
22
import java .lang .reflect .AnnotatedParameterizedType ;
29
23
import java .lang .reflect .AnnotatedType ;
30
- import java .lang .reflect .Field ;
31
24
import java .lang .reflect .ParameterizedType ;
32
- import java .util .*;
33
- import java .util .concurrent .ConcurrentHashMap ;
25
+ import java .util .ArrayList ;
26
+ import java .util .List ;
27
+ import java .util .Optional ;
34
28
import java .util .concurrent .CopyOnWriteArrayList ;
35
29
import java .util .stream .Stream ;
36
30
37
31
import static graphql .annotations .util .NamingKit .toGraphqlName ;
38
- import static graphql .schema .GraphQLEnumType .newEnum ;
39
32
40
33
@ Component (property = "type=default" )
41
34
public class DefaultTypeFunction implements TypeFunction {
@@ -69,7 +62,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
69
62
}
70
63
71
64
@ Override
72
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
65
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
73
66
return Scalars .GraphQLID ;
74
67
}
75
68
}
@@ -87,7 +80,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
87
80
}
88
81
89
82
@ Override
90
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
83
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
91
84
return Scalars .GraphQLString ;
92
85
}
93
86
}
@@ -105,7 +98,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
105
98
}
106
99
107
100
@ Override
108
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
101
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
109
102
return Scalars .GraphQLBoolean ;
110
103
}
111
104
}
@@ -123,7 +116,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
123
116
}
124
117
125
118
@ Override
126
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
119
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
127
120
return Scalars .GraphQLFloat ;
128
121
}
129
122
}
@@ -141,7 +134,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
141
134
}
142
135
143
136
@ Override
144
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
137
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
145
138
return Scalars .GraphQLInt ;
146
139
}
147
140
}
@@ -159,7 +152,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
159
152
}
160
153
161
154
@ Override
162
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
155
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
163
156
return Scalars .GraphQLLong ;
164
157
}
165
158
}
@@ -175,7 +168,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
175
168
}
176
169
177
170
@ Override
178
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
171
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
179
172
if (!(annotatedType instanceof AnnotatedParameterizedType )) {
180
173
throw new IllegalArgumentException ("List type parameter should be specified" );
181
174
}
@@ -187,7 +180,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
187
180
} else {
188
181
klass = (Class <?>) arg .getType ();
189
182
}
190
- return new GraphQLList (DefaultTypeFunction .this .buildType (klass , arg ));
183
+ return new GraphQLList (DefaultTypeFunction .this .buildType (inputType , klass , arg ));
191
184
}
192
185
}
193
186
@@ -199,7 +192,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
199
192
}
200
193
201
194
@ Override
202
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
195
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
203
196
if (!(annotatedType instanceof AnnotatedParameterizedType )) {
204
197
throw new IllegalArgumentException ("Stream type parameter should be specified" );
205
198
}
@@ -211,7 +204,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
211
204
} else {
212
205
klass = (Class <?>) arg .getType ();
213
206
}
214
- return new GraphQLList (DefaultTypeFunction .this .buildType (klass , arg ));
207
+ return new GraphQLList (DefaultTypeFunction .this .buildType (inputType , klass , arg ));
215
208
}
216
209
}
217
210
@@ -229,9 +222,9 @@ public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
229
222
}
230
223
231
224
@ Override
232
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
225
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
233
226
AnnotatedType arg = getAnnotatedType (annotatedType );
234
- return DefaultTypeFunction .this .buildType (typeName , getClass (annotatedType ), arg );
227
+ return DefaultTypeFunction .this .buildType (inputType , getClass (annotatedType ), arg );
235
228
}
236
229
237
230
private AnnotatedType getAnnotatedType (AnnotatedType annotatedType ) {
@@ -253,69 +246,7 @@ private Class<?> getClass(AnnotatedType annotatedType) {
253
246
}
254
247
}
255
248
256
- private class EnumFunction implements TypeFunction {
257
- private final Map <String , GraphQLTypeReference > processing = new ConcurrentHashMap <>();
258
- private final Map <String , GraphQLType > types = new ConcurrentHashMap <>();
259
-
260
- @ Override
261
- public String getTypeName (Class <?> aClass , AnnotatedType annotatedType ) {
262
- GraphQLName name = aClass .getAnnotation (GraphQLName .class );
263
- return toGraphqlName (name == null ? aClass .getSimpleName () : name .value ());
264
- }
265
-
266
- @ Override
267
- public boolean canBuildType (Class <?> aClass , AnnotatedType annotatedType ) {
268
- return Enum .class .isAssignableFrom (aClass );
269
- }
270
-
271
- @ Override
272
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
273
- if (types .containsKey (typeName )) {
274
- return types .get (typeName );
275
- } else if (processing .containsKey (typeName )) {
276
- return processing .getOrDefault (typeName , new GraphQLTypeReference (typeName ));
277
- } else {
278
-
279
- processing .put (typeName , new GraphQLTypeReference (typeName ));
280
-
281
- //noinspection unchecked
282
- Class <? extends Enum > enumClass = (Class <? extends Enum >) aClass ;
283
- GraphQLEnumType .Builder builder = newEnum ();
284
- builder .name (typeName );
285
-
286
- GraphQLDescription description = aClass .getAnnotation (GraphQLDescription .class );
287
- if (description != null ) {
288
- builder .description (description .value ());
289
- }
290
-
291
- List <Enum > constants = Arrays .asList (enumClass .getEnumConstants ());
292
-
293
- Arrays .stream (enumClass .getEnumConstants ()).map (Enum ::name ).forEachOrdered (n -> {
294
- try {
295
- Field field = aClass .getField (n );
296
- GraphQLName fieldName = field .getAnnotation (GraphQLName .class );
297
- GraphQLDescription fieldDescription = field .getAnnotation (GraphQLDescription .class );
298
- Enum constant = constants .stream ().filter (c -> c .name ().contentEquals (n )).findFirst ().get ();
299
- String name_ = fieldName == null ? n : fieldName .value ();
300
- builder .value (name_ , constant , fieldDescription == null ? name_ : fieldDescription .value ());
301
- } catch (NoSuchFieldException ignore ) {
302
- }
303
- });
304
-
305
- final GraphQLEnumType type = builder .build ();
306
- types .put (typeName , type );
307
- //noinspection SuspiciousMethodCalls
308
- processing .remove (type );
309
- return type ;
310
- }
311
- }
312
- }
313
-
314
249
private class ObjectFunction implements TypeFunction {
315
-
316
- private final Map <String , GraphQLTypeReference > processing = new ConcurrentHashMap <>();
317
- private final Map <String , GraphQLType > types = new ConcurrentHashMap <>();
318
-
319
250
@ Override
320
251
public String getTypeName (Class <?> aClass , AnnotatedType annotatedType ) {
321
252
GraphQLName name = aClass .getAnnotation (GraphQLName .class );
@@ -328,22 +259,11 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
328
259
}
329
260
330
261
@ Override
331
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
332
- if (types .containsKey (typeName )) {
333
- return types .get (typeName );
334
- } else if (processing .containsKey (typeName )) {
335
- return processing .getOrDefault (typeName , new GraphQLTypeReference (typeName ));
262
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
263
+ if (inputType ) {
264
+ return annotationsProcessor .getInputObject (aClass );
336
265
} else {
337
- processing .put (typeName , new GraphQLTypeReference (typeName ));
338
- GraphQLType type ;
339
- if (aClass .isInterface ()) {
340
- type = annotationsProcessor .getInterface (aClass );
341
- } else {
342
- type = annotationsProcessor .getObjectOrRef (aClass );
343
- }
344
- types .put (typeName , type );
345
- processing .remove (typeName );
346
- return type ;
266
+ return annotationsProcessor .getOutputTypeOrRef (aClass );
347
267
}
348
268
}
349
269
}
@@ -362,8 +282,6 @@ public DefaultTypeFunction() {
362
282
typeFunctions .add (new IterableFunction ());
363
283
typeFunctions .add (new StreamFunction ());
364
284
365
- typeFunctions .add (new EnumFunction ());
366
-
367
285
typeFunctions .add (new OptionalFunction ());
368
286
369
287
typeFunctions .add (new ObjectFunction ());
@@ -403,13 +321,13 @@ public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
403
321
}
404
322
405
323
@ Override
406
- public GraphQLType buildType (String typeName , Class <?> aClass , AnnotatedType annotatedType ) {
324
+ public GraphQLType buildType (boolean inputType , Class <?> aClass , AnnotatedType annotatedType ) {
407
325
TypeFunction typeFunction = getTypeFunction (aClass , annotatedType );
408
326
if (typeFunction == null ) {
409
327
throw new IllegalArgumentException ("unsupported type" );
410
328
}
411
329
412
- GraphQLType result = typeFunction .buildType (typeName , aClass , annotatedType );
330
+ GraphQLType result = typeFunction .buildType (inputType , aClass , annotatedType );
413
331
if (aClass .getAnnotation (GraphQLNonNull .class ) != null ||
414
332
(annotatedType != null && annotatedType .getAnnotation (GraphQLNonNull .class ) != null )) {
415
333
result = new graphql .schema .GraphQLNonNull (result );
0 commit comments