Skip to content

Commit c14db62

Browse files
authored
Merge pull request #138 from guy120494/fix-input
Fix input bug
2 parents 99ff53c + 566fe1b commit c14db62

File tree

10 files changed

+161
-43
lines changed

10 files changed

+161
-43
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616

1717

1818
import graphql.annotations.processor.retrievers.GraphQLInputObjectRetriever;
19-
import graphql.annotations.processor.retrievers.GraphQLObjectHandler;
2019
import graphql.annotations.processor.ProcessingElementsContainer;
2120
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
22-
import graphql.annotations.processor.retrievers.GraphQLOutputObjectRetriever;
21+
import graphql.annotations.processor.retrievers.GraphQLTypeRetriever;
2322
import graphql.schema.GraphQLInputObjectType;
2423
import graphql.schema.GraphQLInputType;
2524
import graphql.schema.GraphQLType;
2625

2726
import java.util.Map;
2827

28+
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
29+
2930
public class GraphQLInputProcessor {
3031

31-
private static final String DEFAULT_INPUT_PREFIX = "Input";
3232

3333
private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever;
34-
private GraphQLOutputObjectRetriever graphQLOutputObjectRetriever;
34+
private GraphQLTypeRetriever graphQLOutputObjectRetriever;
3535
private GraphQLInputObjectRetriever graphQLInputObjectRetriever;
3636

37-
public GraphQLInputProcessor(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLOutputObjectRetriever graphQLOutputObjectRetriever, GraphQLInputObjectRetriever graphQLInputObjectRetriever) {
37+
public GraphQLInputProcessor(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLTypeRetriever graphQLOutputObjectRetriever, GraphQLInputObjectRetriever graphQLInputObjectRetriever) {
3838
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
3939
this.graphQLOutputObjectRetriever = graphQLOutputObjectRetriever;
4040
this.graphQLInputObjectRetriever = graphQLInputObjectRetriever;
4141
}
4242

4343
public GraphQLInputProcessor() {
44-
this(new GraphQLObjectInfoRetriever(), new GraphQLOutputObjectRetriever(), new GraphQLInputObjectRetriever());
44+
this(new GraphQLObjectInfoRetriever(), new GraphQLTypeRetriever(), new GraphQLInputObjectRetriever());
4545
}
4646

4747
/**
@@ -53,18 +53,26 @@ public GraphQLInputProcessor() {
5353
*/
5454

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

5968
if (typeRegistry.containsKey(typeName)) {
60-
return (GraphQLInputObjectType) container.getTypeRegistry().get(typeName);
69+
return (GraphQLInputType) container.getTypeRegistry().get(typeName);
6170
} else {
62-
graphql.schema.GraphQLType graphQLType = graphQLOutputObjectRetriever.getOutputType(object, container);
71+
72+
graphql.schema.GraphQLType graphQLType = graphQLOutputObjectRetriever.getGraphQLType(object, container, considerAsInput);
6373
GraphQLInputType inputObject = graphQLInputObjectRetriever.getInputObject(graphQLType, DEFAULT_INPUT_PREFIX, typeRegistry);
6474
typeRegistry.put(inputObject.getName(), inputObject);
6575
return inputObject;
6676
}
6777
}
68-
69-
7078
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@
1818
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
1919
import graphql.annotations.processor.ProcessingElementsContainer;
2020
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
21-
import graphql.annotations.processor.retrievers.GraphQLOutputObjectRetriever;
21+
import graphql.annotations.processor.retrievers.GraphQLTypeRetriever;
2222
import graphql.schema.GraphQLOutputType;
2323
import graphql.schema.GraphQLTypeReference;
2424

2525
public class GraphQLOutputProcessor {
2626

2727
private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever;
28-
private GraphQLOutputObjectRetriever outputObjectRetriever;
28+
private GraphQLTypeRetriever outputObjectRetriever;
2929

3030

31-
public GraphQLOutputProcessor(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLOutputObjectRetriever outputObjectRetriever) {
31+
public GraphQLOutputProcessor(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLTypeRetriever outputObjectRetriever) {
3232
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
3333
this.outputObjectRetriever = outputObjectRetriever;
3434
}
3535

3636
public GraphQLOutputProcessor() {
37-
this(new GraphQLObjectInfoRetriever(), new GraphQLOutputObjectRetriever());
37+
this(new GraphQLObjectInfoRetriever(), new GraphQLTypeRetriever());
3838
}
3939

4040
/**
@@ -55,7 +55,7 @@ public GraphQLOutputType getOutputTypeOrRef(Class<?> object, ProcessingElementsC
5555
return new GraphQLTypeReference(typeName);
5656
}
5757

58-
return outputObjectRetriever.getOutputType(object, container);
58+
return (GraphQLOutputType) outputObjectRetriever.getGraphQLType(object, container, false);
5959
}
6060

6161
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class GraphQLInputObjectRetriever {
3535
*/
3636

3737
public GraphQLInputType getInputObject(graphql.schema.GraphQLType graphQLType, String newNamePrefix, Map<String, graphql.schema.GraphQLType> typeRegistry) {
38+
3839
if (graphQLType instanceof GraphQLObjectType) {
3940
return handleGraphQLObjectType((GraphQLObjectType) graphQLType, newNamePrefix, typeRegistry);
4041
} else if (graphQLType instanceof GraphQLList) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717

1818
import graphql.annotations.processor.ProcessingElementsContainer;
1919
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
20+
import graphql.schema.GraphQLOutputType;
2021

2122
public class GraphQLInterfaceRetriever {
2223

23-
private GraphQLOutputObjectRetriever graphQLOutputObjectRetriever;
24+
private GraphQLTypeRetriever graphQLOutputObjectRetriever;
2425

25-
public GraphQLInterfaceRetriever(GraphQLOutputObjectRetriever graphQLOutputObjectRetriever) {
26+
public GraphQLInterfaceRetriever(GraphQLTypeRetriever graphQLOutputObjectRetriever) {
2627
this.graphQLOutputObjectRetriever = graphQLOutputObjectRetriever;
2728
}
2829

2930
public GraphQLInterfaceRetriever() {
30-
this(new GraphQLOutputObjectRetriever());
31+
this(new GraphQLTypeRetriever());
3132
}
3233

3334
/**
@@ -39,6 +40,6 @@ public GraphQLInterfaceRetriever() {
3940
* @throws GraphQLAnnotationsException if the class cannot be examined
4041
*/
4142
public graphql.schema.GraphQLOutputType getInterface(Class<?> iface, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
42-
return graphQLOutputObjectRetriever.getOutputType(iface, container);
43+
return (GraphQLOutputType) graphQLOutputObjectRetriever.getGraphQLType(iface, container, false);
4344
}
4445
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222

2323
public class GraphQLObjectHandler {
2424

25-
private GraphQLOutputObjectRetriever outputObjectRetriever;
25+
private GraphQLTypeRetriever outputObjectRetriever;
2626

27-
public GraphQLObjectHandler(GraphQLOutputObjectRetriever outputObjectRetriever) {
27+
public GraphQLObjectHandler(GraphQLTypeRetriever outputObjectRetriever) {
2828
this.outputObjectRetriever = outputObjectRetriever;
2929
}
3030

3131
public GraphQLObjectHandler() {
32-
this(new GraphQLOutputObjectRetriever());
32+
this(new GraphQLTypeRetriever());
3333
}
3434

3535
public GraphQLObjectType getObject(Class<?> object, ProcessingElementsContainer container) throws GraphQLAnnotationsException, CannotCastMemberException {
36-
GraphQLOutputType type = outputObjectRetriever.getOutputType(object, container);
36+
GraphQLOutputType type = (GraphQLOutputType) outputObjectRetriever.getGraphQLType(object, container, false);
3737
if (type instanceof GraphQLObjectType) {
3838
return (GraphQLObjectType) type;
3939
} else {

src/main/java/graphql/annotations/processor/retrievers/GraphQLOutputObjectRetriever.java renamed to src/main/java/graphql/annotations/processor/retrievers/GraphQLTypeRetriever.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,54 @@
2525
import graphql.annotations.processor.typeBuilders.InterfaceBuilder;
2626
import graphql.annotations.processor.typeBuilders.ObjectBuilder;
2727
import graphql.annotations.processor.typeBuilders.UnionBuilder;
28+
import graphql.annotations.processor.util.InputPropertiesUtil;
2829
import graphql.schema.*;
2930

30-
public class GraphQLOutputObjectRetriever {
31+
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
32+
33+
public class GraphQLTypeRetriever {
3134

3235
private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever;
3336
private GraphQLFieldRetriever graphQLFieldRetriever;
3437

35-
public GraphQLOutputObjectRetriever(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLFieldRetriever graphQLFieldRetriever) {
38+
public GraphQLTypeRetriever(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLFieldRetriever graphQLFieldRetriever) {
3639
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
3740
this.graphQLFieldRetriever = graphQLFieldRetriever;
3841
}
3942

40-
public GraphQLOutputObjectRetriever() {
43+
public GraphQLTypeRetriever() {
4144
this(new GraphQLObjectInfoRetriever(), new GraphQLFieldRetriever());
4245
}
4346

4447
/**
45-
* This will examine the object and will return a {@link GraphQLOutputType} based on the class type and annotationTypes.
48+
* This will examine the object and will return a {@link GraphQLType} based on the class type and annotationTypes.
4649
* - If its annotated with {@link graphql.annotations.annotationTypes.GraphQLUnion} it will return a {@link GraphQLUnionType}
4750
* - If its annotated with {@link graphql.annotations.annotationTypes.GraphQLTypeResolver} it will return a {@link GraphQLInterfaceType}
4851
* - It it's an Enum it will return a {@link GraphQLEnumType},
4952
* otherwise it will return a {@link GraphQLObjectType}.
5053
*
5154
* @param object the object class to examine*
5255
* @param container a class that hold several members that are required in order to build schema
53-
* @return a {@link GraphQLOutputType} that represents that object class
56+
* @return a {@link GraphQLType} that represents that object class
5457
* @throws graphql.annotations.processor.exceptions.GraphQLAnnotationsException if the object class cannot be examined
5558
* @throws graphql.annotations.processor.exceptions.CannotCastMemberException if the object class cannot be examined
5659
*/
57-
public GraphQLOutputType getOutputType(Class<?> object, ProcessingElementsContainer container) throws GraphQLAnnotationsException, CannotCastMemberException {
60+
public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer container, boolean isInput) throws GraphQLAnnotationsException, CannotCastMemberException {
5861
// because the TypeFunction can call back to this processor and
5962
// Java classes can be circular, we need to protect against
6063
// building the same type twice because graphql-java 3.x requires
6164
// all type instances to be unique singletons
6265
String typeName = graphQLObjectInfoRetriever.getTypeName(object);
63-
64-
GraphQLOutputType type = (GraphQLOutputType) container.getTypeRegistry().get(typeName);
65-
if (type != null) { // type already exists, do not build a new new one
66-
return type;
66+
GraphQLType type;
67+
if (isInput) {
68+
String typeNameWithPrefix = DEFAULT_INPUT_PREFIX + typeName;
69+
type = container.getTypeRegistry().get(typeNameWithPrefix);
70+
if (type != null) return type;
71+
} else {
72+
type = container.getTypeRegistry().get(typeName);
73+
if (type != null) { // type already exists, do not build a new new one
74+
return type;
75+
}
6776
}
6877

6978
container.getProcessing().push(typeName);
@@ -77,6 +86,7 @@ public GraphQLOutputType getOutputType(Class<?> object, ProcessingElementsContai
7786
type = new ObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), graphQLFieldRetriever, new GraphQLInterfaceRetriever()).getObjectBuilder(object, container).build();
7887
}
7988

89+
if (isInput) typeName = DEFAULT_INPUT_PREFIX + typeName;
8090
container.getTypeRegistry().put(typeName, type);
8191
container.getProcessing().pop();
8292

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +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), DEFAULT_INPUT_PREFIX, container.getTypeRegistry());
64+
graphql.schema.GraphQLInputType graphQLType = (GraphQLInputType) finalTypeFunction.buildType(true, t, parameter.getAnnotatedType(), container);
6565
return getArgument(parameter, graphQLType);
6666
}).collect(Collectors.toList());
6767

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.processor.util;
16+
17+
public class InputPropertiesUtil {
18+
public static final String DEFAULT_INPUT_PREFIX = "Input";
19+
}

0 commit comments

Comments
 (0)