Skip to content

Commit 653f805

Browse files
Randy Hejtmanekyrashk
authored andcommitted
EnumFunction doesn't create a new instance for every enum with the same typeName. (#60)
1 parent af856d2 commit 653f805

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

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

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -209,35 +209,50 @@ public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
209209
}
210210

211211
private class EnumFunction implements TypeFunction {
212+
private final Map<String, GraphQLTypeReference> processing = new ConcurrentHashMap<>();
213+
private final Map<String, GraphQLType> types = new ConcurrentHashMap<>();
212214

213215
@Override
214216
public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
215-
Class<? extends Enum> enumClass = (Class<? extends Enum>) aClass;
216-
GraphQLEnumType.Builder builder = newEnum();
217-
218217
GraphQLName name = aClass.getAnnotation(GraphQLName.class);
219-
builder.name(name == null ? aClass.getSimpleName() : name.value());
218+
String typeName = name == null ? aClass.getSimpleName() : name.value();
220219

221-
GraphQLDescription description = aClass.getAnnotation(GraphQLDescription.class);
222-
if (description != null) {
223-
builder.description(description.value());
224-
}
220+
if (types.containsKey(typeName)) {
221+
return types.get(typeName);
222+
} else if(processing.containsKey(typeName)) {
223+
return processing.getOrDefault(typeName, new GraphQLTypeReference(typeName));
224+
} else {
225+
226+
processing.put(typeName, new GraphQLTypeReference(typeName));
227+
228+
Class<? extends Enum> enumClass = (Class<? extends Enum>) aClass;
229+
GraphQLEnumType.Builder builder = newEnum();
230+
builder.name(typeName);
225231

226-
List<Enum> constants = Arrays.asList(enumClass.getEnumConstants());
227-
228-
Arrays.stream(enumClass.getEnumConstants()).map(Enum::name).forEachOrdered(n -> {
229-
try {
230-
Field field = aClass.getField(n);
231-
GraphQLName fieldName = field.getAnnotation(GraphQLName.class);
232-
GraphQLDescription fieldDescription = field.getAnnotation(GraphQLDescription.class);
233-
Enum constant = constants.stream().filter(c -> c.name().contentEquals(n)).findFirst().get();
234-
String name_ = fieldName == null ? n : fieldName.value();
235-
builder.value(name_, constant, fieldDescription == null ? name_ : fieldDescription.value());
236-
} catch (NoSuchFieldException e) {
232+
GraphQLDescription description = aClass.getAnnotation(GraphQLDescription.class);
233+
if (description != null) {
234+
builder.description(description.value());
237235
}
238-
});
239236

240-
return builder.build();
237+
List<Enum> constants = Arrays.asList(enumClass.getEnumConstants());
238+
239+
Arrays.stream(enumClass.getEnumConstants()).map(Enum::name).forEachOrdered(n -> {
240+
try {
241+
Field field = aClass.getField(n);
242+
GraphQLName fieldName = field.getAnnotation(GraphQLName.class);
243+
GraphQLDescription fieldDescription = field.getAnnotation(GraphQLDescription.class);
244+
Enum constant = constants.stream().filter(c -> c.name().contentEquals(n)).findFirst().get();
245+
String name_ = fieldName == null ? n : fieldName.value();
246+
builder.value(name_, constant, fieldDescription == null ? name_ : fieldDescription.value());
247+
} catch (NoSuchFieldException e) {
248+
}
249+
});
250+
251+
final GraphQLEnumType type = builder.build();
252+
types.put(typeName, type);
253+
processing.remove(type);
254+
return type;
255+
}
241256
}
242257

243258
@Override public Collection<Class<?>> getAcceptedTypes() {

0 commit comments

Comments
 (0)