Skip to content

Commit fe80a68

Browse files
author
Thomas Draier
committed
Fixed input types
1 parent 05e3e95 commit fe80a68

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
260260

261261
@Override
262262
public GraphQLType buildType(String typeName, Class<?> aClass, AnnotatedType annotatedType) {
263-
return annotationsProcessor.getOutputTypeOrRef(aClass);
263+
try {
264+
return annotationsProcessor.getOutputTypeOrRef(aClass);
265+
} catch (ClassCastException e) {
266+
// Also try to resolve to input object
267+
return annotationsProcessor.getInputObject(aClass);
268+
}
264269
}
265270
}
266271

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
630630
filter(p -> !DataFetchingEnvironment.class.isAssignableFrom(p.getType())).
631631
map(parameter -> {
632632
Class<?> t = parameter.getType();
633-
graphql.schema.GraphQLType graphQLType = getInputObject(finalTypeFunction.buildType(t, parameter.getAnnotatedType()), "Input");
633+
graphql.schema.GraphQLType graphQLType = getInputObject(finalTypeFunction.buildType(t, parameter.getAnnotatedType()), "");
634634
return getArgument(parameter, graphQLType);
635635
}).collect(Collectors.toList());
636636

@@ -696,11 +696,23 @@ protected static GraphQLFieldDefinition field(Method method) throws Instantiatio
696696

697697
}
698698

699+
public GraphQLInputObjectType getInputObject(Class<?> object) {
700+
String typeName = getTypeName(object);
701+
if (typeRegistry.containsKey(typeName)) {
702+
return (GraphQLInputObjectType) typeRegistry.get(typeName);
703+
} else {
704+
graphql.schema.GraphQLType graphQLType = getObject(object);
705+
GraphQLInputObjectType inputObject = (GraphQLInputObjectType) getInputObject(graphQLType, "");
706+
typeRegistry.put(inputObject.getName(), inputObject);
707+
return inputObject;
708+
}
709+
}
710+
699711
@Override
700712
public GraphQLInputType getInputObject(graphql.schema.GraphQLType graphQLType, String newNamePrefix) {
701713
if (graphQLType instanceof GraphQLObjectType) {
702714
GraphQLObjectType object = (GraphQLObjectType) graphQLType;
703-
if (typeRegistry.containsKey(newNamePrefix + object.getName())) {
715+
if (typeRegistry.containsKey(newNamePrefix + object.getName()) && typeRegistry.get(newNamePrefix + object.getName()) instanceof GraphQLInputType) {
704716
return (GraphQLInputType) typeRegistry.get(newNamePrefix + object.getName());
705717
}
706718
GraphQLInputObjectType inputObjectType = new GraphQLInputObjectType(newNamePrefix + object.getName(), object.getDescription(),

src/main/java/graphql/annotations/GraphQLAnnotationsProcessor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ public interface GraphQLAnnotationsProcessor {
106106
*/
107107
GraphQLObjectType.Builder getObjectBuilder(Class<?> object) throws GraphQLAnnotationsException;
108108

109+
/**
110+
* This will examine the object class and return a {@link GraphQLInputType} representation
111+
*
112+
* @param object the object class to examine
113+
*
114+
* @return a {@link GraphQLInputType} that represents that object class
115+
*
116+
* @throws GraphQLAnnotationsException if the object class cannot be examined
117+
*/
118+
GraphQLInputObjectType getInputObject(Class<?> object) throws GraphQLAnnotationsException;
119+
109120
/**
110121
* This will turn a {@link GraphQLObjectType} into a corresponding {@link GraphQLInputObjectType}
111122
*

0 commit comments

Comments
 (0)