Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yarinvak committed Nov 9, 2017
1 parent 1291c7a commit 6746cfe
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.annotations;

import graphql.schema.GraphQLFieldDefinition;

public class GraphQLFieldDefinitionWrapper extends GraphQLFieldDefinition {

public GraphQLFieldDefinitionWrapper(GraphQLFieldDefinition fieldDefinition) {
super(fieldDefinition.getName(), fieldDefinition.getDescription(), fieldDefinition.getType(),
fieldDefinition.getDataFetcher(), fieldDefinition.getArguments(), fieldDefinition.getDeprecationReason());
}

@Override
public boolean equals(Object obj) {
return obj instanceof GraphQLFieldDefinition &&
((GraphQLFieldDefinition) obj).getName().contentEquals(getName());
}
}
60 changes: 20 additions & 40 deletions src/main/java/graphql/annotations/processor/GraphQLAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
*/
package graphql.annotations.processor;

import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.annotations.processor.retrievers.GraphQLObjectHandler;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.GraphQLTypeExtension;
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.annotations.processor.graphQLProcessors.GraphQLAnnotationsProcessor;
import graphql.annotations.processor.graphQLProcessors.GraphQLInputProcessor;
import graphql.annotations.processor.graphQLProcessors.GraphQLOutputProcessor;
import graphql.annotations.processor.retrievers.GraphQLObjectHandler;
import graphql.annotations.processor.typeFunctions.DefaultTypeFunction;
import graphql.annotations.processor.typeFunctions.TypeFunction;
import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever;
import graphql.relay.Relay;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import java.util.*;
import java.util.HashSet;
import java.util.Map;

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

Expand All @@ -43,26 +43,13 @@ public class GraphQLAnnotations implements GraphQLAnnotationsProcessor {
private GraphQLObjectHandler graphQLObjectHandler;
private ProcessingElementsContainer container;

private GraphQLObjectInfoRetriever graphQLObjectInfoRetriever;

public GraphQLAnnotations() {
this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()), new GraphQLObjectInfoRetriever(), new GraphQLObjectHandler());
this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()), new GraphQLObjectHandler());
}

public GraphQLAnnotations(TypeFunction defaultTypeFunction, GraphQLObjectInfoRetriever graphQLObjectInfoRetriever, GraphQLObjectHandler graphQLObjectHandler) {
this.graphQLObjectInfoRetriever = graphQLObjectInfoRetriever;
this.defaultTypeFunction = defaultTypeFunction;
public GraphQLAnnotations(TypeFunction defaultTypeFunction, GraphQLObjectHandler graphQLObjectHandler) {
this.graphQLObjectHandler = graphQLObjectHandler;
this.container = initializeContainer(this.defaultTypeFunction);
}

private ProcessingElementsContainer initializeContainer(TypeFunction defaultTypeFunction) {
Map<String, graphql.schema.GraphQLType> typeRegistry = new HashMap<>();
Map<Class<?>, Set<Class<?>>> extensionsTypeRegistry = new HashMap<>();
final Stack<String> processing = new Stack<>();
Relay relay = new Relay();
ProcessingElementsContainer container = new ProcessingElementsContainer(defaultTypeFunction, relay, typeRegistry, extensionsTypeRegistry, processing);
return container;
this.container = new ProcessingElementsContainer(defaultTypeFunction);
}

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


public String getTypeName(Class<?> objectClass) {
GraphQLName name = objectClass.getAnnotation(GraphQLName.class);
return toGraphqlName(name == null ? objectClass.getSimpleName() : name.value());
}

public static GraphQLObjectType object(Class<?> object) throws GraphQLAnnotationsException {
return new GraphQLObjectHandler().getObject(object, getInstance().getContainer());
}

public static class GraphQLFieldDefinitionWrapper extends GraphQLFieldDefinition {

public GraphQLFieldDefinitionWrapper(GraphQLFieldDefinition fieldDefinition) {
super(fieldDefinition.getName(), fieldDefinition.getDescription(), fieldDefinition.getType(),
fieldDefinition.getDataFetcher(), fieldDefinition.getArguments(), fieldDefinition.getDeprecationReason());
}

@Override
public boolean equals(Object obj) {
return obj instanceof GraphQLFieldDefinition &&
((GraphQLFieldDefinition) obj).getName().contentEquals(getName());
}
GraphQLAnnotations instance = getInstance();
return instance.graphQLObjectHandler.getObject(object, instance.getContainer());
}

protected TypeFunction defaultTypeFunction;

public void registerTypeExtension(Class<?> objectClass) {
GraphQLTypeExtension typeExtension = objectClass.getAnnotation(GraphQLTypeExtension.class);
if (typeExtension == null) {
Expand All @@ -127,7 +98,7 @@ public void unregisterTypeExtension(Class<?> objectClass) {
}

public void registerType(TypeFunction typeFunction) {
((DefaultTypeFunction) defaultTypeFunction).register(typeFunction);
((DefaultTypeFunction) container.getDefaultTypeFunction()).register(typeFunction);
}

public static void register(TypeFunction typeFunction) {
Expand All @@ -142,4 +113,13 @@ public ProcessingElementsContainer getContainer() {
return container;
}

public void setContainer(ProcessingElementsContainer container) {
this.container = container;
}

@Reference(target = "(type=default)")
public void setDefaultTypeFunction(TypeFunction function) {
this.container.setDefaultTypeFunction(function);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
package graphql.annotations.processor;


import graphql.annotations.processor.graphQLProcessors.GraphQLInputProcessor;
import graphql.annotations.processor.graphQLProcessors.GraphQLOutputProcessor;
import graphql.annotations.processor.typeFunctions.DefaultTypeFunction;
import graphql.annotations.processor.typeFunctions.TypeFunction;
import graphql.relay.Relay;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
Expand All @@ -39,6 +43,13 @@ public ProcessingElementsContainer(TypeFunction defaultTypeFunction, Relay relay
this.processing = processing;
}

public ProcessingElementsContainer(TypeFunction typeFunction) {
this(typeFunction, new Relay(), new HashMap<>(), new HashMap<>(), new Stack<>());
}

public ProcessingElementsContainer(){
this(new DefaultTypeFunction(new GraphQLInputProcessor(), new GraphQLOutputProcessor()),new Relay(), new HashMap<>(), new HashMap<>(), new Stack<>());
}

public Relay getRelay() {
return this.relay;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -14,6 +14,9 @@
*/
package graphql.annotations.processor.graphQLProcessors;

import graphql.annotations.processor.typeFunctions.TypeFunction;
import graphql.relay.Relay;

public interface GraphQLAnnotationsProcessor {
/**
* Register a new type extension class. This extension will be used when the extended object will be created.
Expand All @@ -30,4 +33,17 @@ public interface GraphQLAnnotationsProcessor {
*/
void unregisterTypeExtension(Class<?> objectClass);

/**
* Allows you to set a custom relay object
*
* @param relay The extension class to register
*/
void setRelay(Relay relay);

/**
* Allows you to register a new type function
*
* @param typeFunction The extension class to register
*/
void registerType(TypeFunction typeFunction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,43 @@
package graphql.annotations.processor.retrievers;


import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.annotationTypes.*;
import graphql.annotations.GraphQLFieldDefinitionWrapper;
import graphql.annotations.annotationTypes.GraphQLConnection;
import graphql.annotations.annotationTypes.GraphQLRelayMutation;
import graphql.annotations.annotationTypes.GraphQLType;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.processor.exceptions.CannotCastMemberException;
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.annotations.processor.retrievers.fieldBuilders.ArgumentBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.DeprecateBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.DescriptionBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.field.FieldDataFetcherBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.field.FieldNameBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.method.*;
import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodDataFetcherBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodNameBuilder;
import graphql.annotations.processor.retrievers.fieldBuilders.method.MethodTypeBuilder;
import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch;
import graphql.annotations.processor.exceptions.CannotCastMemberException;
import graphql.annotations.processor.searchAlgorithms.ParentalSearch;
import graphql.annotations.processor.typeFunctions.TypeFunction;
import graphql.annotations.processor.util.ConnectionUtil;
import graphql.annotations.processor.util.DataFetcherConstructor;
import graphql.relay.Relay;
import graphql.schema.*;
import graphql.schema.GraphQLNonNull;

import java.lang.reflect.*;
import java.util.*;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static graphql.annotations.processor.util.ReflectionKit.newInstance;
import static graphql.annotations.processor.util.ObjectUtil.getAllFields;
import static graphql.annotations.processor.util.ReflectionKit.newInstance;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLInputObjectField.newInputObjectField;
import static java.util.Arrays.stream;

public class GraphQLFieldRetriever {

Expand Down Expand Up @@ -110,7 +117,7 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine
builder.description(new DescriptionBuilder(method).build())
.deprecate(new DeprecateBuilder(method).build())
.dataFetcher(new MethodDataFetcherBuilder(method, outputType, typeFunction, container, relayFieldDefinition, args, dataFetcherConstructor, isConnection).build());
return new GraphQLAnnotations.GraphQLFieldDefinitionWrapper(builder.build());
return new GraphQLFieldDefinitionWrapper(builder.build());
}

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

return new GraphQLAnnotations.GraphQLFieldDefinitionWrapper(builder.build());
return new GraphQLFieldDefinitionWrapper(builder.build());
}

private TypeFunction getTypeFunction(Field field, ProcessingElementsContainer container) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class GraphQLInputObjectRetriever {

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

private GraphQLInputType HandleGraphQLObjectType(GraphQLObjectType graphQLType, String newNamePrefix, Map<String, GraphQLType> typeRegistry) {
private GraphQLInputType handleGraphQLObjectType(GraphQLObjectType graphQLType, String newNamePrefix, Map<String, GraphQLType> typeRegistry) {
GraphQLObjectType object = graphQLType;
String newObjectName = newNamePrefix + object.getName();
GraphQLType objectInTypeRegistry = typeRegistry.get(newObjectName);
Expand Down

0 comments on commit 6746cfe

Please sign in to comment.