Skip to content

Commit 6746cfe

Browse files
committed
code review fixes
1 parent 1291c7a commit 6746cfe

File tree

6 files changed

+101
-56
lines changed

6 files changed

+101
-56
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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;
16+
17+
import graphql.schema.GraphQLFieldDefinition;
18+
19+
public class GraphQLFieldDefinitionWrapper extends GraphQLFieldDefinition {
20+
21+
public GraphQLFieldDefinitionWrapper(GraphQLFieldDefinition fieldDefinition) {
22+
super(fieldDefinition.getName(), fieldDefinition.getDescription(), fieldDefinition.getType(),
23+
fieldDefinition.getDataFetcher(), fieldDefinition.getArguments(), fieldDefinition.getDeprecationReason());
24+
}
25+
26+
@Override
27+
public boolean equals(Object obj) {
28+
return obj instanceof GraphQLFieldDefinition &&
29+
((GraphQLFieldDefinition) obj).getName().contentEquals(getName());
30+
}
31+
}

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

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
*/
1515
package graphql.annotations.processor;
1616

17-
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
18-
import graphql.annotations.processor.retrievers.GraphQLObjectHandler;
1917
import graphql.annotations.annotationTypes.GraphQLName;
2018
import graphql.annotations.annotationTypes.GraphQLTypeExtension;
19+
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
2120
import graphql.annotations.processor.graphQLProcessors.GraphQLAnnotationsProcessor;
2221
import graphql.annotations.processor.graphQLProcessors.GraphQLInputProcessor;
2322
import graphql.annotations.processor.graphQLProcessors.GraphQLOutputProcessor;
23+
import graphql.annotations.processor.retrievers.GraphQLObjectHandler;
2424
import graphql.annotations.processor.typeFunctions.DefaultTypeFunction;
2525
import graphql.annotations.processor.typeFunctions.TypeFunction;
26-
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
2726
import graphql.relay.Relay;
28-
import graphql.schema.GraphQLFieldDefinition;
2927
import graphql.schema.GraphQLObjectType;
3028
import org.osgi.service.component.annotations.Component;
29+
import org.osgi.service.component.annotations.Reference;
3130

32-
import java.util.*;
31+
import java.util.HashSet;
32+
import java.util.Map;
3333

3434
import static graphql.annotations.processor.util.NamingKit.toGraphqlName;
3535

@@ -43,26 +43,13 @@ public class GraphQLAnnotations implements GraphQLAnnotationsProcessor {
4343
private GraphQLObjectHandler graphQLObjectHandler;
4444
private ProcessingElementsContainer container;
4545

46-
private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever;
47-
4846
public GraphQLAnnotations() {
49-
this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()), new GraphQLObjectInfoRetriever(), new GraphQLObjectHandler());
47+
this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()), new GraphQLObjectHandler());
5048
}
5149

52-
public GraphQLAnnotations(TypeFunction defaultTypeFunction, GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLObjectHandler graphQLObjectHandler) {
53-
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
54-
this.defaultTypeFunction = defaultTypeFunction;
50+
public GraphQLAnnotations(TypeFunction defaultTypeFunction, GraphQLObjectHandler graphQLObjectHandler) {
5551
this.graphQLObjectHandler = graphQLObjectHandler;
56-
this.container = initializeContainer(this.defaultTypeFunction);
57-
}
58-
59-
private ProcessingElementsContainer initializeContainer(TypeFunction defaultTypeFunction) {
60-
Map<String, graphql.schema.GraphQLType> typeRegistry = new HashMap<>();
61-
Map<Class<?>, Set<Class<?>>> extensionsTypeRegistry = new HashMap<>();
62-
final Stack<String> processing = new Stack<>();
63-
Relay relay = new Relay();
64-
ProcessingElementsContainer container = new ProcessingElementsContainer(defaultTypeFunction, relay, typeRegistry, extensionsTypeRegistry, processing);
65-
return container;
52+
this.container = new ProcessingElementsContainer(defaultTypeFunction);
6653
}
6754

6855
public static GraphQLAnnotations instance = new GraphQLAnnotations();
@@ -75,32 +62,16 @@ public void setRelay(Relay relay) {
7562
this.container.setRelay(relay);
7663
}
7764

78-
7965
public String getTypeName(Class<?> objectClass) {
8066
GraphQLName name = objectClass.getAnnotation(GraphQLName.class);
8167
return toGraphqlName(name == null ? objectClass.getSimpleName() : name.value());
8268
}
8369

8470
public static GraphQLObjectType object(Class<?> object) throws GraphQLAnnotationsException {
85-
return new GraphQLObjectHandler().getObject(object, getInstance().getContainer());
86-
}
87-
88-
public static class GraphQLFieldDefinitionWrapper extends GraphQLFieldDefinition {
89-
90-
public GraphQLFieldDefinitionWrapper(GraphQLFieldDefinition fieldDefinition) {
91-
super(fieldDefinition.getName(), fieldDefinition.getDescription(), fieldDefinition.getType(),
92-
fieldDefinition.getDataFetcher(), fieldDefinition.getArguments(), fieldDefinition.getDeprecationReason());
93-
}
94-
95-
@Override
96-
public boolean equals(Object obj) {
97-
return obj instanceof GraphQLFieldDefinition &&
98-
((GraphQLFieldDefinition) obj).getName().contentEquals(getName());
99-
}
71+
GraphQLAnnotations instance = getInstance();
72+
return instance.graphQLObjectHandler.getObject(object, instance.getContainer());
10073
}
10174

102-
protected TypeFunction defaultTypeFunction;
103-
10475
public void registerTypeExtension(Class<?> objectClass) {
10576
GraphQLTypeExtension typeExtension = objectClass.getAnnotation(GraphQLTypeExtension.class);
10677
if (typeExtension == null) {
@@ -127,7 +98,7 @@ public void unregisterTypeExtension(Class<?> objectClass) {
12798
}
12899

129100
public void registerType(TypeFunction typeFunction) {
130-
((DefaultTypeFunction) defaultTypeFunction).register(typeFunction);
101+
((DefaultTypeFunction) container.getDefaultTypeFunction()).register(typeFunction);
131102
}
132103

133104
public static void register(TypeFunction typeFunction) {
@@ -142,4 +113,13 @@ public ProcessingElementsContainer getContainer() {
142113
return container;
143114
}
144115

116+
public void setContainer(ProcessingElementsContainer container) {
117+
this.container = container;
118+
}
119+
120+
@Reference(target = "(type=default)")
121+
public void setDefaultTypeFunction(TypeFunction function) {
122+
this.container.setDefaultTypeFunction(function);
123+
}
124+
145125
}

src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
package graphql.annotations.processor;
1616

1717

18+
import graphql.annotations.processor.graphQLProcessors.GraphQLInputProcessor;
19+
import graphql.annotations.processor.graphQLProcessors.GraphQLOutputProcessor;
20+
import graphql.annotations.processor.typeFunctions.DefaultTypeFunction;
1821
import graphql.annotations.processor.typeFunctions.TypeFunction;
1922
import graphql.relay.Relay;
2023

24+
import java.util.HashMap;
2125
import java.util.Map;
2226
import java.util.Set;
2327
import java.util.Stack;
@@ -39,6 +43,13 @@ public ProcessingElementsContainer(TypeFunction defaultTypeFunction, Relay relay
3943
this.processing = processing;
4044
}
4145

46+
public ProcessingElementsContainer(TypeFunction typeFunction) {
47+
this(typeFunction, new Relay(), new HashMap<>(), new HashMap<>(), new Stack<>());
48+
}
49+
50+
public ProcessingElementsContainer(){
51+
this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()),new Relay(), new HashMap<>(), new HashMap<>(), new Stack<>());
52+
}
4253

4354
public Relay getRelay() {
4455
return this.relay;

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,6 +14,9 @@
1414
*/
1515
package graphql.annotations.processor.graphQLProcessors;
1616

17+
import graphql.annotations.processor.typeFunctions.TypeFunction;
18+
import graphql.relay.Relay;
19+
1720
public interface GraphQLAnnotationsProcessor {
1821
/**
1922
* Register a new type extension class. This extension will be used when the extended object will be created.
@@ -30,4 +33,17 @@ public interface GraphQLAnnotationsProcessor {
3033
*/
3134
void unregisterTypeExtension(Class<?> objectClass);
3235

36+
/**
37+
* Allows you to set a custom relay object
38+
*
39+
* @param relay The extension class to register
40+
*/
41+
void setRelay(Relay relay);
42+
43+
/**
44+
* Allows you to register a new type function
45+
*
46+
* @param typeFunction The extension class to register
47+
*/
48+
void registerType(TypeFunction typeFunction);
3349
}

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,43 @@
1515
package graphql.annotations.processor.retrievers;
1616

1717

18-
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
19-
import graphql.annotations.processor.ProcessingElementsContainer;
20-
import graphql.annotations.annotationTypes.*;
18+
import graphql.annotations.GraphQLFieldDefinitionWrapper;
19+
import graphql.annotations.annotationTypes.GraphQLConnection;
20+
import graphql.annotations.annotationTypes.GraphQLRelayMutation;
2121
import graphql.annotations.annotationTypes.GraphQLType;
22-
import graphql.annotations.processor.GraphQLAnnotations;
22+
import graphql.annotations.processor.ProcessingElementsContainer;
23+
import graphql.annotations.processor.exceptions.CannotCastMemberException;
24+
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
2325
import graphql.annotations.processor.retrievers.fieldBuilders.ArgumentBuilder;
2426
import graphql.annotations.processor.retrievers.fieldBuilders.DeprecateBuilder;
2527
import graphql.annotations.processor.retrievers.fieldBuilders.DescriptionBuilder;
2628
import graphql.annotations.processor.retrievers.fieldBuilders.field.FieldDataFetcherBuilder;
2729
import graphql.annotations.processor.retrievers.fieldBuilders.field.FieldNameBuilder;
28-
import graphql.annotations.processor.retrievers.fieldBuilders.method.*;
30+
import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodDataFetcherBuilder;
31+
import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodNameBuilder;
32+
import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodTypeBuilder;
2933
import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch;
30-
import graphql.annotations.processor.exceptions.CannotCastMemberException;
3134
import graphql.annotations.processor.searchAlgorithms.ParentalSearch;
3235
import graphql.annotations.processor.typeFunctions.TypeFunction;
3336
import graphql.annotations.processor.util.ConnectionUtil;
3437
import graphql.annotations.processor.util.DataFetcherConstructor;
3538
import graphql.relay.Relay;
3639
import graphql.schema.*;
37-
import graphql.schema.GraphQLNonNull;
3840

39-
import java.lang.reflect.*;
40-
import java.util.*;
41+
import java.lang.reflect.AccessibleObject;
42+
import java.lang.reflect.Field;
43+
import java.lang.reflect.Method;
44+
import java.lang.reflect.Modifier;
45+
import java.util.ArrayList;
46+
import java.util.Collections;
47+
import java.util.List;
48+
import java.util.Map;
4149
import java.util.stream.Collectors;
4250

43-
import static graphql.annotations.processor.util.ReflectionKit.newInstance;
4451
import static graphql.annotations.processor.util.ObjectUtil.getAllFields;
52+
import static graphql.annotations.processor.util.ReflectionKit.newInstance;
4553
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
4654
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
47-
import static java.util.Arrays.stream;
4855

4956
public class GraphQLFieldRetriever {
5057

@@ -110,7 +117,7 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine
110117
builder.description(new DescriptionBuilder(method).build())
111118
.deprecate(new DeprecateBuilder(method).build())
112119
.dataFetcher(new MethodDataFetcherBuilder(method, outputType, typeFunction, container, relayFieldDefinition, args, dataFetcherConstructor, isConnection).build());
113-
return new GraphQLAnnotations.GraphQLFieldDefinitionWrapper(builder.build());
120+
return new GraphQLFieldDefinitionWrapper(builder.build());
114121
}
115122

116123
private GraphQLFieldDefinition handleRelayArguments(Method method, ProcessingElementsContainer container, GraphQLFieldDefinition.Builder builder, GraphQLOutputType outputType, List<GraphQLArgument> args) {
@@ -175,7 +182,7 @@ public GraphQLFieldDefinition getField(Field field, ProcessingElementsContainer
175182
.deprecate(new DeprecateBuilder(field).build())
176183
.dataFetcher(new FieldDataFetcherBuilder(field, dataFetcherConstructor, outputType, typeFunction, container, isConnection).build());
177184

178-
return new GraphQLAnnotations.GraphQLFieldDefinitionWrapper(builder.build());
185+
return new GraphQLFieldDefinitionWrapper(builder.build());
179186
}
180187

181188
private TypeFunction getTypeFunction(Field field, ProcessingElementsContainer container) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class GraphQLInputObjectRetriever {
3636

3737
public GraphQLInputType getInputObject(graphql.schema.GraphQLType graphQLType, String newNamePrefix, Map<String, graphql.schema.GraphQLType> typeRegistry) {
3838
if (graphQLType instanceof GraphQLObjectType) {
39-
return HandleGraphQLObjectType((GraphQLObjectType) graphQLType, newNamePrefix, typeRegistry);
39+
return handleGraphQLObjectType((GraphQLObjectType) graphQLType, newNamePrefix, typeRegistry);
4040
} else if (graphQLType instanceof GraphQLList) {
4141
return new GraphQLList(getInputObject(((GraphQLList) graphQLType).getWrappedType(), newNamePrefix, typeRegistry));
4242
} else if (graphQLType instanceof graphql.schema.GraphQLNonNull) {
@@ -49,7 +49,7 @@ public GraphQLInputType getInputObject(graphql.schema.GraphQLType graphQLType, S
4949
throw new IllegalArgumentException("Cannot convert type to input : " + graphQLType);
5050
}
5151

52-
private GraphQLInputType HandleGraphQLObjectType(GraphQLObjectType graphQLType, String newNamePrefix, Map<String, GraphQLType> typeRegistry) {
52+
private GraphQLInputType handleGraphQLObjectType(GraphQLObjectType graphQLType, String newNamePrefix, Map<String, GraphQLType> typeRegistry) {
5353
GraphQLObjectType object = graphQLType;
5454
String newObjectName = newNamePrefix + object.getName();
5555
GraphQLType objectInTypeRegistry = typeRegistry.get(newObjectName);

0 commit comments

Comments
 (0)