Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/graphql/annotations/BatchedTypeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public boolean canBuildType(final Class<?> aClass, final AnnotatedType type) {
}

@Override
public graphql.schema.GraphQLType buildType(String typeName, final Class<?> aClass, final AnnotatedType annotatedType) {
public graphql.schema.GraphQLType buildType(final boolean inputType, final Class<?> aClass, final AnnotatedType annotatedType) {
if (!aClass.isAssignableFrom(List.class)) {
throw new IllegalArgumentException("Batched method should return a List");
}
Expand All @@ -52,7 +52,6 @@ public graphql.schema.GraphQLType buildType(String typeName, final Class<?> aCla
} else {
klass = (Class<?>) arg.getType();
}
typeName = klass.getSimpleName();
return defaultTypeFunction.buildType(typeName, klass, arg);
return defaultTypeFunction.buildType(inputType, klass, arg);
}
}
126 changes: 22 additions & 104 deletions src/main/java/graphql/annotations/DefaultTypeFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@
package graphql.annotations;

import graphql.Scalars;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLTypeReference;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.*;

import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

import static graphql.annotations.util.NamingKit.toGraphqlName;
import static graphql.schema.GraphQLEnumType.newEnum;

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

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return Scalars.GraphQLID;
}
}
Expand All @@ -87,7 +80,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return Scalars.GraphQLString;
}
}
Expand All @@ -105,7 +98,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return Scalars.GraphQLBoolean;
}
}
Expand All @@ -123,7 +116,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return Scalars.GraphQLFloat;
}
}
Expand All @@ -141,7 +134,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return Scalars.GraphQLInt;
}
}
Expand All @@ -159,7 +152,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
return Scalars.GraphQLLong;
}
}
Expand All @@ -175,7 +168,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
throw new IllegalArgumentException("List type parameter should be specified");
}
Expand All @@ -187,7 +180,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
} else {
klass = (Class<?>) arg.getType();
}
return new GraphQLList(DefaultTypeFunction.this.buildType(klass, arg));
return new GraphQLList(DefaultTypeFunction.this.buildType(inputType, klass, arg));
}
}

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

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
throw new IllegalArgumentException("Stream type parameter should be specified");
}
Expand All @@ -211,7 +204,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
} else {
klass = (Class<?>) arg.getType();
}
return new GraphQLList(DefaultTypeFunction.this.buildType(klass, arg));
return new GraphQLList(DefaultTypeFunction.this.buildType(inputType, klass, arg));
}
}

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

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
AnnotatedType arg = getAnnotatedType(annotatedType);
return DefaultTypeFunction.this.buildType(typeName, getClass(annotatedType), arg);
return DefaultTypeFunction.this.buildType(inputType, getClass(annotatedType), arg);
}

private AnnotatedType getAnnotatedType(AnnotatedType annotatedType) {
Expand All @@ -253,69 +246,7 @@ private Class<?> getClass(AnnotatedType annotatedType) {
}
}

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

@Override
public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
GraphQLName name = aClass.getAnnotation(GraphQLName.class);
return toGraphqlName(name == null ? aClass.getSimpleName() : name.value());
}

@Override
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
return Enum.class.isAssignableFrom(aClass);
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
if (types.containsKey(typeName)) {
return types.get(typeName);
} else if (processing.containsKey(typeName)) {
return processing.getOrDefault(typeName, new GraphQLTypeReference(typeName));
} else {

processing.put(typeName, new GraphQLTypeReference(typeName));

//noinspection unchecked
Class<? extends Enum> enumClass = (Class<? extends Enum>) aClass;
GraphQLEnumType.Builder builder = newEnum();
builder.name(typeName);

GraphQLDescription description = aClass.getAnnotation(GraphQLDescription.class);
if (description != null) {
builder.description(description.value());
}

List<Enum> constants = Arrays.asList(enumClass.getEnumConstants());

Arrays.stream(enumClass.getEnumConstants()).map(Enum::name).forEachOrdered(n -> {
try {
Field field = aClass.getField(n);
GraphQLName fieldName = field.getAnnotation(GraphQLName.class);
GraphQLDescription fieldDescription = field.getAnnotation(GraphQLDescription.class);
Enum constant = constants.stream().filter(c -> c.name().contentEquals(n)).findFirst().get();
String name_ = fieldName == null ? n : fieldName.value();
builder.value(name_, constant, fieldDescription == null ? name_ : fieldDescription.value());
} catch (NoSuchFieldException ignore) {
}
});

final GraphQLEnumType type = builder.build();
types.put(typeName, type);
//noinspection SuspiciousMethodCalls
processing.remove(type);
return type;
}
}
}

private class ObjectFunction implements TypeFunction {

private final Map<String, GraphQLTypeReference> processing = new ConcurrentHashMap<>();
private final Map<String, GraphQLType> types = new ConcurrentHashMap<>();

@Override
public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
GraphQLName name = aClass.getAnnotation(GraphQLName.class);
Expand All @@ -328,22 +259,11 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
}

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
if (types.containsKey(typeName)) {
return types.get(typeName);
} else if (processing.containsKey(typeName)) {
return processing.getOrDefault(typeName, new GraphQLTypeReference(typeName));
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
if (inputType) {
return annotationsProcessor.getInputObject(aClass);
} else {
processing.put(typeName, new GraphQLTypeReference(typeName));
GraphQLType type;
if (aClass.isInterface()) {
type = annotationsProcessor.getInterface(aClass);
} else {
type = annotationsProcessor.getObjectOrRef(aClass);
}
types.put(typeName, type);
processing.remove(typeName);
return type;
return annotationsProcessor.getOutputTypeOrRef(aClass);
}
}
}
Expand All @@ -362,8 +282,6 @@ public DefaultTypeFunction() {
typeFunctions.add(new IterableFunction());
typeFunctions.add(new StreamFunction());

typeFunctions.add(new EnumFunction());

typeFunctions.add(new OptionalFunction());

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

@Override
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
TypeFunction typeFunction = getTypeFunction(aClass, annotatedType);
if (typeFunction == null) {
throw new IllegalArgumentException("unsupported type");
}

GraphQLType result = typeFunction.buildType(typeName, aClass, annotatedType);
GraphQLType result = typeFunction.buildType(inputType, aClass, annotatedType);
if (aClass.getAnnotation(GraphQLNonNull.class) != null ||
(annotatedType != null && annotatedType.getAnnotation(GraphQLNonNull.class) != null)) {
result = new graphql.schema.GraphQLNonNull(result);
Expand Down
Loading