Skip to content

Commit e2e8245

Browse files
committed
fix issue
1 parent 2e2d6dc commit e2e8245

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLInputProcessor.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -52,18 +52,26 @@ public GraphQLInputProcessor() {
5252
*/
5353

5454
public GraphQLInputType getInputType(Class<?> object, ProcessingElementsContainer container) {
55-
String typeName = DEFAULT_INPUT_PREFIX + graphQLObjectInfoRetriever.getTypeName(object);
55+
boolean considerAsInput = true;
56+
if (Enum.class.isAssignableFrom(object)) {
57+
considerAsInput = false;
58+
}
59+
60+
String typeName = graphQLObjectInfoRetriever.getTypeName(object);
61+
62+
if (considerAsInput)
63+
typeName = DEFAULT_INPUT_PREFIX + typeName;
64+
5665
Map<String, GraphQLType> typeRegistry = container.getTypeRegistry();
5766

5867
if (typeRegistry.containsKey(typeName)) {
59-
return (GraphQLInputObjectType) container.getTypeRegistry().get(typeName);
68+
return (GraphQLInputType) container.getTypeRegistry().get(typeName);
6069
} else {
61-
graphql.schema.GraphQLType graphQLType = graphQLOutputObjectRetriever.getGraphQLType(object, container, true);
70+
71+
graphql.schema.GraphQLType graphQLType = graphQLOutputObjectRetriever.getGraphQLType(object, container, considerAsInput);
6272
GraphQLInputType inputObject = graphQLInputObjectRetriever.getInputObject(graphQLType, DEFAULT_INPUT_PREFIX, typeRegistry);
6373
typeRegistry.put(inputObject.getName(), inputObject);
6474
return inputObject;
6575
}
6676
}
67-
68-
6977
}

src/main/java/graphql/annotations/processor/retrievers/GraphQLTypeRetriever.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ public GraphQLTypeRetriever() {
5454
* @throws graphql.annotations.processor.exceptions.GraphQLAnnotationsException if the object class cannot be examined
5555
* @throws graphql.annotations.processor.exceptions.CannotCastMemberException if the object class cannot be examined
5656
*/
57-
public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer container, boolean isInputHirerarchy) throws GraphQLAnnotationsException, CannotCastMemberException {
57+
public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer container, boolean isInput) throws GraphQLAnnotationsException, CannotCastMemberException {
5858
// because the TypeFunction can call back to this processor and
5959
// Java classes can be circular, we need to protect against
6060
// building the same type twice because graphql-java 3.x requires
6161
// all type instances to be unique singletons
6262
String typeName = graphQLObjectInfoRetriever.getTypeName(object);
6363
GraphQLType type;
64-
if (isInputHirerarchy) {
64+
if (isInput) {
6565
String typeNameWithPrefix = "Input" + typeName;
6666
type = container.getTypeRegistry().get(typeNameWithPrefix);
6767
if (type != null) return type;
@@ -83,7 +83,7 @@ public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer c
8383
type = new ObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), graphQLFieldRetriever, new GraphQLInterfaceRetriever()).getObjectBuilder(object, container).build();
8484
}
8585

86-
if (isInputHirerarchy) typeName = "Input" + typeName;
86+
if (isInput) typeName = "Input" + typeName;
8787
container.getTypeRegistry().put(typeName, type);
8888
container.getProcessing().pop();
8989

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/ArgumentBuilder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ public ArgumentBuilder(Method method, TypeFunction typeFunction, GraphQLInputObj
5757
@Override
5858
public List<GraphQLArgument> build() {
5959
TypeFunction finalTypeFunction = typeFunction;
60-
List<GraphQLArgument> args = Arrays.asList(method.getParameters()).stream().
60+
List<GraphQLArgument> args = Arrays.stream(method.getParameters()).
6161
filter(p -> !DataFetchingEnvironment.class.isAssignableFrom(p.getType())).
6262
map(parameter -> {
6363
Class<?> t = parameter.getType();
64-
graphql.schema.GraphQLInputType graphQLType = graphQLInputObjectRetriever.getInputObject(finalTypeFunction.buildType(true, t, parameter.getAnnotatedType(), container)
65-
, DEFAULT_INPUT_PREFIX, container.getTypeRegistry());
64+
graphql.schema.GraphQLInputType graphQLType = (GraphQLInputType) finalTypeFunction.buildType(true, t, parameter.getAnnotatedType(), container);
6665
return getArgument(parameter, graphQLType);
6766
}).collect(Collectors.toList());
6867

src/test/java/graphql/annotations/GraphQLInputTest.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.testng.annotations.Test;
2727

2828
import java.util.*;
29+
import java.util.stream.Collectors;
2930

3031
import static graphql.schema.GraphQLSchema.newSchema;
3132
import static org.testng.Assert.assertEquals;
@@ -226,41 +227,74 @@ public void queryWithInterface() {
226227
}
227228

228229
@GraphQLName("hero")
229-
public class Hero {
230+
public static class Hero {
230231
@GraphQLField
231232
int a;
232233
}
233234

234235
@GraphQLName("hero")
235-
public class HeroInput {
236+
public static class HeroInput {
236237
@GraphQLField
237238
String b;
238239

239240
@GraphQLField
240241
Skill skill;
241242
}
242243

243-
public class Skill{
244+
public static class Skill {
244245
@GraphQLField
245246
String c;
246247
}
247248

248-
public class QueryInputAndOutput {
249+
public static class QueryInputAndOutput {
249250
@GraphQLField
250251
public Hero getHero() {
251252
return null;
252253
}
253254

254255
@GraphQLField
255-
public String getString(HeroInput input) {
256+
public String getString(@GraphQLName("input") HeroInput input) {
256257
return "asdf";
257258
}
259+
260+
// todo: if another method with input argument with type Hero and not HeroInput, it will consider HeroInput as its type because its defined before
261+
/*
262+
public String getString2(Hero input) {return "Asdfasdf";}
263+
*/
264+
}
265+
266+
267+
public static class QueryInputAndOutput2 {
268+
@GraphQLField
269+
public String getA(@GraphQLName("skill") Skill skill) {
270+
return "asdfasdf";
271+
}
272+
273+
@GraphQLField
274+
public Skill getSkill() {
275+
return null;
276+
}
258277
}
259278

279+
260280
@Test
261281
public void testInputAndOutputWithSameName() {
282+
// arrange + act
262283
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput.class)).build();
263-
int x =5;
284+
// assert
285+
assertEquals(schema.getQueryType().getFieldDefinition("hero").getType().getName(), "hero");
286+
assertEquals(schema.getQueryType().getFieldDefinition("string").getArgument("input").getType().getName(), "Inputhero");
287+
assertEquals(((GraphQLInputObjectType) schema.getQueryType().getFieldDefinition("string")
288+
.getArgument("input").getType()).getField("skill").getType().getName(), "InputSkill");
289+
}
290+
291+
@Test
292+
public void testInputAndOutputSameClass(){
293+
// arrange + act
294+
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput2.class)).build();
295+
// assert
296+
assertEquals(schema.getQueryType().getFieldDefinition("skill").getType().getName(), "Skill");
297+
assertEquals(schema.getQueryType().getFieldDefinition("a").getArgument("skill").getType().getName(), "InputSkill");
264298
}
265299

266300
}

0 commit comments

Comments
 (0)