Skip to content

Commit c20332f

Browse files
authored
Merge pull request #94 from Jahia/registry-fixes
Keep all output types in registry, move type resolution from TypeFunctions to GraphQLAnnotations
2 parents 289a711 + 7369cb0 commit c20332f

19 files changed

+588
-215
lines changed

src/main/java/graphql/annotations/BatchedTypeFunction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public boolean canBuildType(final Class<?> aClass, final AnnotatedType type) {
3737
}
3838

3939
@Override
40-
public graphql.schema.GraphQLType buildType(String typeName, final Class<?> aClass, final AnnotatedType annotatedType) {
40+
public graphql.schema.GraphQLType buildType(final boolean inputType, final Class<?> aClass, final AnnotatedType annotatedType) {
4141
if (!aClass.isAssignableFrom(List.class)) {
4242
throw new IllegalArgumentException("Batched method should return a List");
4343
}
@@ -52,7 +52,6 @@ public graphql.schema.GraphQLType buildType(String typeName, final Class<?> aCla
5252
} else {
5353
klass = (Class<?>) arg.getType();
5454
}
55-
typeName = klass.getSimpleName();
56-
return defaultTypeFunction.buildType(typeName, klass, arg);
55+
return defaultTypeFunction.buildType(inputType, klass, arg);
5756
}
5857
}

src/main/java/graphql/annotations/DefaultTypeFunction.java

Lines changed: 22 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,20 @@
1515
package graphql.annotations;
1616

1717
import graphql.Scalars;
18-
import graphql.schema.GraphQLEnumType;
1918
import graphql.schema.GraphQLList;
2019
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.*;
2721

2822
import java.lang.reflect.AnnotatedParameterizedType;
2923
import java.lang.reflect.AnnotatedType;
30-
import java.lang.reflect.Field;
3124
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;
3428
import java.util.concurrent.CopyOnWriteArrayList;
3529
import java.util.stream.Stream;
3630

3731
import static graphql.annotations.util.NamingKit.toGraphqlName;
38-
import static graphql.schema.GraphQLEnumType.newEnum;
3932

4033
@Component(property = "type=default")
4134
public class DefaultTypeFunction implements TypeFunction {
@@ -69,7 +62,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
6962
}
7063

7164
@Override
72-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
65+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
7366
return Scalars.GraphQLID;
7467
}
7568
}
@@ -87,7 +80,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
8780
}
8881

8982
@Override
90-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
83+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
9184
return Scalars.GraphQLString;
9285
}
9386
}
@@ -105,7 +98,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
10598
}
10699

107100
@Override
108-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
101+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
109102
return Scalars.GraphQLBoolean;
110103
}
111104
}
@@ -123,7 +116,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
123116
}
124117

125118
@Override
126-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
119+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
127120
return Scalars.GraphQLFloat;
128121
}
129122
}
@@ -141,7 +134,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
141134
}
142135

143136
@Override
144-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
137+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
145138
return Scalars.GraphQLInt;
146139
}
147140
}
@@ -159,7 +152,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
159152
}
160153

161154
@Override
162-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
155+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
163156
return Scalars.GraphQLLong;
164157
}
165158
}
@@ -175,7 +168,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
175168
}
176169

177170
@Override
178-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
171+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
179172
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
180173
throw new IllegalArgumentException("List type parameter should be specified");
181174
}
@@ -187,7 +180,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
187180
} else {
188181
klass = (Class<?>) arg.getType();
189182
}
190-
return new GraphQLList(DefaultTypeFunction.this.buildType(klass, arg));
183+
return new GraphQLList(DefaultTypeFunction.this.buildType(inputType, klass, arg));
191184
}
192185
}
193186

@@ -199,7 +192,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
199192
}
200193

201194
@Override
202-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
195+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
203196
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
204197
throw new IllegalArgumentException("Stream type parameter should be specified");
205198
}
@@ -211,7 +204,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
211204
} else {
212205
klass = (Class<?>) arg.getType();
213206
}
214-
return new GraphQLList(DefaultTypeFunction.this.buildType(klass, arg));
207+
return new GraphQLList(DefaultTypeFunction.this.buildType(inputType, klass, arg));
215208
}
216209
}
217210

@@ -229,9 +222,9 @@ public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
229222
}
230223

231224
@Override
232-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
225+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
233226
AnnotatedType arg = getAnnotatedType(annotatedType);
234-
return DefaultTypeFunction.this.buildType(typeName, getClass(annotatedType), arg);
227+
return DefaultTypeFunction.this.buildType(inputType, getClass(annotatedType), arg);
235228
}
236229

237230
private AnnotatedType getAnnotatedType(AnnotatedType annotatedType) {
@@ -253,69 +246,7 @@ private Class<?> getClass(AnnotatedType annotatedType) {
253246
}
254247
}
255248

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-
314249
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-
319250
@Override
320251
public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
321252
GraphQLName name = aClass.getAnnotation(GraphQLName.class);
@@ -328,22 +259,11 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
328259
}
329260

330261
@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);
336265
} 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);
347267
}
348268
}
349269
}
@@ -362,8 +282,6 @@ public DefaultTypeFunction() {
362282
typeFunctions.add(new IterableFunction());
363283
typeFunctions.add(new StreamFunction());
364284

365-
typeFunctions.add(new EnumFunction());
366-
367285
typeFunctions.add(new OptionalFunction());
368286

369287
typeFunctions.add(new ObjectFunction());
@@ -403,13 +321,13 @@ public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
403321
}
404322

405323
@Override
406-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
324+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
407325
TypeFunction typeFunction = getTypeFunction(aClass, annotatedType);
408326
if (typeFunction == null) {
409327
throw new IllegalArgumentException("unsupported type");
410328
}
411329

412-
GraphQLType result = typeFunction.buildType(typeName, aClass, annotatedType);
330+
GraphQLType result = typeFunction.buildType(inputType, aClass, annotatedType);
413331
if (aClass.getAnnotation(GraphQLNonNull.class) != null ||
414332
(annotatedType != null && annotatedType.getAnnotation(GraphQLNonNull.class) != null)) {
415333
result = new graphql.schema.GraphQLNonNull(result);

0 commit comments

Comments
 (0)