Skip to content

Commit 9a5cccb

Browse files
author
Thomas Draier
committed
Restored an "Input" prefix on InputTypes, added parameter in TypeFunction to choose if the returned type should be an input type
1 parent 42c646c commit 9a5cccb

File tree

6 files changed

+31
-30
lines changed

6 files changed

+31
-30
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: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
8080
}
8181

8282
@Override
83-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
83+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
8484
return Scalars.GraphQLString;
8585
}
8686
}
@@ -98,7 +98,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
9898
}
9999

100100
@Override
101-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
101+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
102102
return Scalars.GraphQLBoolean;
103103
}
104104
}
@@ -116,7 +116,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
116116
}
117117

118118
@Override
119-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
119+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
120120
return Scalars.GraphQLFloat;
121121
}
122122
}
@@ -134,7 +134,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
134134
}
135135

136136
@Override
137-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
137+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
138138
return Scalars.GraphQLInt;
139139
}
140140
}
@@ -152,7 +152,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
152152
}
153153

154154
@Override
155-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
155+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
156156
return Scalars.GraphQLLong;
157157
}
158158
}
@@ -168,7 +168,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
168168
}
169169

170170
@Override
171-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
171+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
172172
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
173173
throw new IllegalArgumentException("List type parameter should be specified");
174174
}
@@ -180,7 +180,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
180180
} else {
181181
klass = (Class<?>) arg.getType();
182182
}
183-
return new GraphQLList(DefaultTypeFunction.this.buildType(klass, arg));
183+
return new GraphQLList(DefaultTypeFunction.this.buildType(inputType, klass, arg));
184184
}
185185
}
186186

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

194194
@Override
195-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
195+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
196196
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
197197
throw new IllegalArgumentException("Stream type parameter should be specified");
198198
}
@@ -204,7 +204,7 @@ public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType ann
204204
} else {
205205
klass = (Class<?>) arg.getType();
206206
}
207-
return new GraphQLList(DefaultTypeFunction.this.buildType(klass, arg));
207+
return new GraphQLList(DefaultTypeFunction.this.buildType(inputType, klass, arg));
208208
}
209209
}
210210

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

224224
@Override
225-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
225+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
226226
AnnotatedType arg = getAnnotatedType(annotatedType);
227-
return DefaultTypeFunction.this.buildType(typeName, getClass(annotatedType), arg);
227+
return DefaultTypeFunction.this.buildType(inputType, getClass(annotatedType), arg);
228228
}
229229

230230
private AnnotatedType getAnnotatedType(AnnotatedType annotatedType) {
@@ -259,12 +259,11 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
259259
}
260260

261261
@Override
262-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
263-
try {
264-
return annotationsProcessor.getOutputTypeOrRef(aClass);
265-
} catch (ClassCastException e) {
266-
// Also try to resolve to input object
262+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
263+
if (inputType) {
267264
return annotationsProcessor.getInputObject(aClass);
265+
} else {
266+
return annotationsProcessor.getOutputTypeOrRef(aClass);
268267
}
269268
}
270269
}
@@ -322,13 +321,13 @@ public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
322321
}
323322

324323
@Override
325-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
324+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
326325
TypeFunction typeFunction = getTypeFunction(aClass, annotatedType);
327326
if (typeFunction == null) {
328327
throw new IllegalArgumentException("unsupported type");
329328
}
330329

331-
GraphQLType result = typeFunction.buildType(typeName, aClass, annotatedType);
330+
GraphQLType result = typeFunction.buildType(inputType, aClass, annotatedType);
332331
if (aClass.getAnnotation(GraphQLNonNull.class) != null ||
333332
(annotatedType != null && annotatedType.getAnnotation(GraphQLNonNull.class) != null)) {
334333
result = new graphql.schema.GraphQLNonNull(result);

src/main/java/graphql/annotations/GraphQLAnnotations.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class GraphQLAnnotations implements GraphQLAnnotationsProcessor {
5151

5252
private static final List<Class> TYPES_FOR_CONNECTION = Arrays.asList(GraphQLObjectType.class, GraphQLInterfaceType.class, GraphQLUnionType.class, GraphQLTypeReference.class);
5353

54+
private static final String DEFAULT_INPUT_PREFIX = "Input";
55+
5456
private Map<String, graphql.schema.GraphQLType> typeRegistry = new HashMap<>();
5557
private Map<Class<?>, Set<Class<?>>> extensionsTypeRegistry = new HashMap<>();
5658
private final Stack<String> processing = new Stack<>();
@@ -630,7 +632,7 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
630632
filter(p -> !DataFetchingEnvironment.class.isAssignableFrom(p.getType())).
631633
map(parameter -> {
632634
Class<?> t = parameter.getType();
633-
graphql.schema.GraphQLType graphQLType = getInputObject(finalTypeFunction.buildType(t, parameter.getAnnotatedType()), "");
635+
graphql.schema.GraphQLType graphQLType = getInputObject(finalTypeFunction.buildType(t, parameter.getAnnotatedType()), DEFAULT_INPUT_PREFIX);
634636
return getArgument(parameter, graphQLType);
635637
}).collect(Collectors.toList());
636638

@@ -697,12 +699,12 @@ protected static GraphQLFieldDefinition field(Method method) throws Instantiatio
697699
}
698700

699701
public GraphQLInputObjectType getInputObject(Class<?> object) {
700-
String typeName = getTypeName(object);
702+
String typeName = DEFAULT_INPUT_PREFIX + getTypeName(object);
701703
if (typeRegistry.containsKey(typeName)) {
702704
return (GraphQLInputObjectType) typeRegistry.get(typeName);
703705
} else {
704706
graphql.schema.GraphQLType graphQLType = getObject(object);
705-
GraphQLInputObjectType inputObject = (GraphQLInputObjectType) getInputObject(graphQLType, "");
707+
GraphQLInputObjectType inputObject = (GraphQLInputObjectType) getInputObject(graphQLType, DEFAULT_INPUT_PREFIX);
706708
typeRegistry.put(inputObject.getName(), inputObject);
707709
return inputObject;
708710
}

src/main/java/graphql/annotations/MethodDataFetcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ private Object[] invocationArgs(DataFetchingEnvironment environment) {
7070
result.add(environment);
7171
continue;
7272
}
73-
graphql.schema.GraphQLType graphQLType = typeFunction.buildType(paramType, p.getAnnotatedType());
73+
74+
graphql.schema.GraphQLType graphQLType = typeFunction.buildType(true, paramType, p.getAnnotatedType());
7475
Object arg = envArgs.next();
7576
result.add(buildArg(p.getParameterizedType(), graphQLType, arg));
7677
}

src/main/java/graphql/annotations/TypeFunction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ default String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
4343

4444
/**
4545
* Build a {@link GraphQLType} object from a java type.
46-
* This is a convenience method for calling {@link #buildType(String, Class, AnnotatedType)} without a type name.
46+
* This is a convenience method for calling {@link #buildType(boolean, Class, AnnotatedType)} without a type name.
4747
* @param aClass The java type to build the type name for
4848
* @param annotatedType The {@link AnnotatedType} of the java type, which may be a {link AnnotatedParameterizedType}
4949
* @return The built {@link GraphQLType}
5050
*/
5151
default GraphQLType buildType(Class<?> aClass, AnnotatedType annotatedType) {
52-
return buildType(getTypeName(aClass, annotatedType), aClass, annotatedType);
52+
return buildType(false, aClass, annotatedType);
5353
}
5454

5555
/**
5656
* Build a {@link GraphQLType} object from a java type.
57-
* @param typeName The name to give the graphql type
57+
* @param input is InputType
5858
* @param aClass The java type to build the type name for
5959
* @param annotatedType The {@link AnnotatedType} of the java type, which may be a {link AnnotatedParameterizedType}
6060
* @return The built {@link GraphQLType}
6161
*/
62-
GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType);
62+
GraphQLType buildType(boolean input, Class<?> aClass, AnnotatedType annotatedType);
6363
}

src/test/java/graphql/annotations/GraphQLObjectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public String getTypeName(Class<?> aClass, AnnotatedType annotatedType) {
625625
}
626626

627627
@Override
628-
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
628+
public GraphQLType buildType(boolean inputType, Class<?> aClass, AnnotatedType annotatedType) {
629629
return GraphQLString;
630630
}
631631
}

0 commit comments

Comments
 (0)