Skip to content

Commit be463b5

Browse files
committed
Refactoring input handling - Creating input builder instead of ineffective casting
1 parent ced7548 commit be463b5

File tree

12 files changed

+240
-86
lines changed

12 files changed

+240
-86
lines changed

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import graphql.annotations.processor.ProcessingElementsContainer;
2020
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
2121
import graphql.annotations.processor.retrievers.GraphQLTypeRetriever;
22-
import graphql.schema.GraphQLInputObjectType;
2322
import graphql.schema.GraphQLInputType;
2423
import graphql.schema.GraphQLType;
24+
import graphql.schema.GraphQLTypeReference;
2525

2626
import java.util.Map;
2727

@@ -31,13 +31,11 @@ public class GraphQLInputProcessor {
3131

3232

3333
private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever;
34-
private GraphQLTypeRetriever graphQLOutputObjectRetriever;
35-
private GraphQLInputObjectRetriever graphQLInputObjectRetriever;
34+
private GraphQLTypeRetriever graphQLTypeRetriever;
3635

37-
public GraphQLInputProcessor(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLTypeRetriever graphQLOutputObjectRetriever, GraphQLInputObjectRetriever graphQLInputObjectRetriever) {
36+
public GraphQLInputProcessor(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLTypeRetriever graphQLTypeRetriever, GraphQLInputObjectRetriever graphQLInputObjectRetriever) {
3837
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
39-
this.graphQLOutputObjectRetriever = graphQLOutputObjectRetriever;
40-
this.graphQLInputObjectRetriever = graphQLInputObjectRetriever;
38+
this.graphQLTypeRetriever = graphQLTypeRetriever;
4139
}
4240

4341
public GraphQLInputProcessor() {
@@ -52,27 +50,27 @@ public GraphQLInputProcessor() {
5250
* @return a {@link GraphQLInputType} that represents that object class
5351
*/
5452

55-
public GraphQLInputType getInputType(Class<?> object, ProcessingElementsContainer container) {
53+
public GraphQLInputType getInputTypeOrRef(Class<?> object, ProcessingElementsContainer container) {
5654
boolean considerAsInput = true;
5755
if (Enum.class.isAssignableFrom(object)) {
5856
considerAsInput = false;
5957
}
6058

61-
String typeName = graphQLObjectInfoRetriever.getTypeName(object);
59+
String typeName = DEFAULT_INPUT_PREFIX +graphQLObjectInfoRetriever.getTypeName(object);
6260

63-
if (considerAsInput)
64-
typeName = DEFAULT_INPUT_PREFIX + typeName;
61+
if (container.getProcessing().contains(typeName)){
62+
return new GraphQLTypeReference(typeName);
63+
}
6564

6665
Map<String, GraphQLType> typeRegistry = container.getTypeRegistry();
6766

6867
if (typeRegistry.containsKey(typeName)) {
6968
return (GraphQLInputType) container.getTypeRegistry().get(typeName);
7069
} else {
7170

72-
graphql.schema.GraphQLType graphQLType = graphQLOutputObjectRetriever.getGraphQLType(object, container, considerAsInput);
73-
GraphQLInputType inputObject = graphQLInputObjectRetriever.getInputObject(graphQLType, DEFAULT_INPUT_PREFIX, typeRegistry);
74-
typeRegistry.put(inputObject.getName(), inputObject);
75-
return inputObject;
71+
GraphQLInputType graphQLType = (GraphQLInputType) graphQLTypeRetriever.getGraphQLType(object, container, considerAsInput);
72+
typeRegistry.put(graphQLType.getName(), graphQLType);
73+
return graphQLType;
7674
}
7775
}
7876
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
public class GraphQLOutputProcessor {
2626

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

3030

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

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

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

6161
}

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

Lines changed: 42 additions & 27 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.
@@ -104,7 +104,7 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine
104104
GraphQLFieldDefinition.Builder builder = newFieldDefinition();
105105
TypeFunction typeFunction = getTypeFunction(method, container);
106106
builder.name(new MethodNameBuilder(method).build());
107-
GraphQLOutputType outputType = new MethodTypeBuilder(method, typeFunction, container).build();
107+
GraphQLOutputType outputType = (GraphQLOutputType) new MethodTypeBuilder(method, typeFunction, container, false).build();
108108

109109
boolean isConnection = ConnectionUtil.isConnection(method, outputType);
110110
if (isConnection) {
@@ -120,12 +120,46 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine
120120
return new GraphQLFieldDefinitionWrapper(builder.build());
121121
}
122122

123+
public GraphQLFieldDefinition getField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
124+
GraphQLFieldDefinition.Builder builder = newFieldDefinition();
125+
builder.name(new FieldNameBuilder(field).build());
126+
TypeFunction typeFunction = getTypeFunction(field, container);
127+
128+
graphql.schema.GraphQLType outputType = typeFunction.buildType(field.getType(), field.getAnnotatedType(), container);
129+
boolean isConnection = ConnectionUtil.isConnection(field, outputType);
130+
if (isConnection) {
131+
outputType = getGraphQLConnection(field, outputType, container.getRelay(), container.getTypeRegistry());
132+
builder.argument(container.getRelay().getConnectionFieldArguments());
133+
}
134+
135+
builder.type((GraphQLOutputType) outputType).description(new DescriptionBuilder(field).build())
136+
.deprecate(new DeprecateBuilder(field).build())
137+
.dataFetcher(new FieldDataFetcherBuilder(field, dataFetcherConstructor, outputType, typeFunction, container, isConnection).build());
138+
139+
return new GraphQLFieldDefinitionWrapper(builder.build());
140+
}
141+
142+
public GraphQLInputObjectField getInputField(Method method, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
143+
GraphQLInputObjectField.Builder builder = newInputObjectField();
144+
builder.name(new MethodNameBuilder(method).build());
145+
TypeFunction typeFunction = getTypeFunction(method, container);
146+
GraphQLInputType inputType = (GraphQLInputType) new MethodTypeBuilder(method, typeFunction, container, true).build();
147+
return builder.type(inputType).description(new DescriptionBuilder(method).build()).build();
148+
}
149+
150+
public GraphQLInputObjectField getInputField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
151+
GraphQLInputObjectField.Builder builder = newInputObjectField();
152+
builder.name(new FieldNameBuilder(field).build());
153+
TypeFunction typeFunction = getTypeFunction(field, container);
154+
graphql.schema.GraphQLType graphQLType = typeFunction.buildType(true,field.getType(), field.getAnnotatedType(), container);
155+
return builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build();
156+
}
157+
123158
private GraphQLFieldDefinition handleRelayArguments(Method method, ProcessingElementsContainer container, GraphQLFieldDefinition.Builder builder, GraphQLOutputType outputType, List<GraphQLArgument> args) {
124159
GraphQLFieldDefinition relayFieldDefinition = null;
125160
if (method.isAnnotationPresent(GraphQLRelayMutation.class)) {
126161
relayFieldDefinition = buildRelayMutation(method, container, builder, outputType, args);
127-
}
128-
else {
162+
} else {
129163
builder.argument(args);
130164
}
131165
return relayFieldDefinition;
@@ -166,24 +200,6 @@ private GraphQLFieldDefinition buildRelayMutation(Method method, ProcessingEleme
166200
return relayFieldDefinition;
167201
}
168202

169-
public GraphQLFieldDefinition getField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
170-
GraphQLFieldDefinition.Builder builder = newFieldDefinition();
171-
builder.name(new FieldNameBuilder(field).build());
172-
TypeFunction typeFunction = getTypeFunction(field, container);
173-
174-
GraphQLOutputType outputType = (GraphQLOutputType) typeFunction.buildType(field.getType(), field.getAnnotatedType(), container);
175-
boolean isConnection = ConnectionUtil.isConnection(field, outputType);
176-
if (isConnection) {
177-
outputType = getGraphQLConnection(field, outputType, container.getRelay(), container.getTypeRegistry());
178-
builder.argument(container.getRelay().getConnectionFieldArguments());
179-
}
180-
181-
builder.type(outputType).description(new DescriptionBuilder(field).build())
182-
.deprecate(new DeprecateBuilder(field).build())
183-
.dataFetcher(new FieldDataFetcherBuilder(field, dataFetcherConstructor, outputType, typeFunction, container, isConnection).build());
184-
185-
return new GraphQLFieldDefinitionWrapper(builder.build());
186-
}
187203

188204
private TypeFunction getTypeFunction(Field field, ProcessingElementsContainer container) {
189205
GraphQLType annotation = field.getAnnotation(GraphQLType.class);
@@ -196,7 +212,7 @@ private TypeFunction getTypeFunction(Field field, ProcessingElementsContainer co
196212
return typeFunction;
197213
}
198214

199-
private GraphQLOutputType getGraphQLConnection(AccessibleObject field, GraphQLOutputType type, Relay relay, Map<String, graphql.schema.GraphQLType> typeRegistry) {
215+
private GraphQLOutputType getGraphQLConnection(AccessibleObject field, graphql.schema.GraphQLType type, Relay relay, Map<String, graphql.schema.GraphQLType> typeRegistry) {
200216
if (type instanceof GraphQLNonNull) {
201217
GraphQLList listType = (GraphQLList) ((GraphQLNonNull) type).getWrappedType();
202218
return new GraphQLNonNull(internalGetGraphQLConnection(field, listType, relay, typeRegistry));
@@ -231,5 +247,4 @@ private GraphQLObjectType getActualType(GraphQLObjectType type, Map<String, grap
231247
return type;
232248
}
233249

234-
235250
}

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 GraphQLTypeRetriever outputObjectRetriever;
25+
private GraphQLTypeRetriever typeRetriever;
2626

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

3131
public GraphQLObjectHandler() {
3232
this(new GraphQLTypeRetriever());
3333
}
3434

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

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
2222
import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch;
2323
import graphql.annotations.processor.searchAlgorithms.ParentalSearch;
24-
import graphql.annotations.processor.typeBuilders.EnumBuilder;
25-
import graphql.annotations.processor.typeBuilders.InterfaceBuilder;
26-
import graphql.annotations.processor.typeBuilders.ObjectBuilder;
27-
import graphql.annotations.processor.typeBuilders.UnionBuilder;
28-
import graphql.annotations.processor.util.InputPropertiesUtil;
24+
import graphql.annotations.processor.typeBuilders.*;
2925
import graphql.schema.*;
3026

3127
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
@@ -65,16 +61,13 @@ public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer c
6561
String typeName = graphQLObjectInfoRetriever.getTypeName(object);
6662
GraphQLType type;
6763
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-
}
64+
typeName = DEFAULT_INPUT_PREFIX + typeName;
65+
7666
}
7767

68+
type = container.getTypeRegistry().get(typeName);
69+
if (type != null) return type;
70+
7871
container.getProcessing().push(typeName);
7972
if (object.getAnnotation(GraphQLUnion.class) != null) {
8073
type = new UnionBuilder(graphQLObjectInfoRetriever).getUnionBuilder(object, container).build();
@@ -83,11 +76,20 @@ public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer c
8376
} else if (Enum.class.isAssignableFrom(object)) {
8477
type = new EnumBuilder(graphQLObjectInfoRetriever).getEnumBuilder(object).build();
8578
} else {
86-
type = new ObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever), new BreadthFirstSearch(graphQLObjectInfoRetriever), graphQLFieldRetriever, new GraphQLInterfaceRetriever()).getObjectBuilder(object, container).build();
79+
ParentalSearch parentalSearch = new ParentalSearch(graphQLObjectInfoRetriever);
80+
BreadthFirstSearch breadthFirstSearch = new BreadthFirstSearch(graphQLObjectInfoRetriever);
81+
82+
if (isInput) {
83+
type = new InputObjectBuilder(graphQLObjectInfoRetriever, parentalSearch,
84+
breadthFirstSearch, graphQLFieldRetriever).getInputObjectBuilder(object, container).build();
85+
}
86+
else{
87+
type = new OutputObjectBuilder(graphQLObjectInfoRetriever, parentalSearch,
88+
breadthFirstSearch, graphQLFieldRetriever, new GraphQLInterfaceRetriever()).getOutputObjectBuilder(object, container).build();
89+
}
8790
}
8891

89-
if (isInput) typeName = DEFAULT_INPUT_PREFIX + typeName;
90-
container.getTypeRegistry().put(typeName, type);
92+
container.getTypeRegistry().put(type.getName(), type);
9193
container.getProcessing().pop();
9294

9395
return type;

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/field/FieldDataFetcherBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
public class FieldDataFetcherBuilder implements Builder<DataFetcher> {
3636
private Field field;
3737
private DataFetcherConstructor dataFetcherConstructor;
38-
private GraphQLOutputType outputType;
38+
private GraphQLType outputType;
3939
private TypeFunction typeFunction;
4040
private ProcessingElementsContainer container;
4141
private boolean isConnection;
4242

43-
public FieldDataFetcherBuilder(Field field, DataFetcherConstructor dataFetcherConstructor, GraphQLOutputType outputType, TypeFunction typeFunction, ProcessingElementsContainer container, boolean isConnection) {
43+
public FieldDataFetcherBuilder(Field field, DataFetcherConstructor dataFetcherConstructor, GraphQLType outputType, TypeFunction typeFunction, ProcessingElementsContainer container, boolean isConnection) {
4444
this.field = field;
4545
this.dataFetcherConstructor = dataFetcherConstructor;
4646
this.outputType = outputType;

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodTypeBuilder.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,36 @@
2020
import graphql.annotations.processor.typeFunctions.BatchedTypeFunction;
2121
import graphql.annotations.processor.typeFunctions.TypeFunction;
2222
import graphql.schema.GraphQLOutputType;
23+
import graphql.schema.GraphQLType;
2324

2425
import java.lang.reflect.AnnotatedType;
2526
import java.lang.reflect.Method;
2627

27-
public class MethodTypeBuilder implements Builder<GraphQLOutputType> {
28+
public class MethodTypeBuilder implements Builder<GraphQLType> {
2829
private Method method;
2930
private TypeFunction typeFunction;
3031
private ProcessingElementsContainer container;
32+
private boolean isInput;
3133

32-
public MethodTypeBuilder(Method method, TypeFunction typeFunction, ProcessingElementsContainer container) {
34+
public MethodTypeBuilder(Method method, TypeFunction typeFunction, ProcessingElementsContainer container, boolean isInput) {
3335
this.method = method;
3436
this.typeFunction = typeFunction;
3537
this.container = container;
38+
this.isInput = isInput;
3639
}
3740

3841
@Override
39-
public GraphQLOutputType build() {
42+
public GraphQLType build() {
4043
AnnotatedType annotatedReturnType = method.getAnnotatedReturnType();
4144

42-
TypeFunction outputTypeFunction;
45+
TypeFunction typeFunction;
4346
if (method.getAnnotation(GraphQLBatched.class) != null) {
44-
outputTypeFunction = new BatchedTypeFunction(typeFunction);
47+
typeFunction = new BatchedTypeFunction(this.typeFunction);
4548
} else {
46-
outputTypeFunction = typeFunction;
49+
typeFunction = this.typeFunction;
4750
}
4851

49-
return (GraphQLOutputType) outputTypeFunction.buildType(method.getReturnType(), annotatedReturnType, container);
52+
return typeFunction.buildType(isInput,method.getReturnType(), annotatedReturnType, container);
5053
}
5154

5255
}

0 commit comments

Comments
 (0)