diff --git a/src/main/java/graphql/annotations/GraphQLFieldDefinitionWrapper.java b/src/main/java/graphql/annotations/GraphQLFieldDefinitionWrapper.java new file mode 100644 index 00000000..5f32b599 --- /dev/null +++ b/src/main/java/graphql/annotations/GraphQLFieldDefinitionWrapper.java @@ -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()); + } +} diff --git a/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java b/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java index be3d8ba6..7ea82e61 100644 --- a/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java +++ b/src/main/java/graphql/annotations/processor/GraphQLAnnotations.java @@ -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; @@ -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 typeRegistry = new HashMap<>(); - Map, Set>> extensionsTypeRegistry = new HashMap<>(); - final Stack 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(); @@ -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) { @@ -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) { @@ -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); + } + } diff --git a/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java b/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java index 7a04ac3a..d202c053 100644 --- a/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java +++ b/src/main/java/graphql/annotations/processor/ProcessingElementsContainer.java @@ -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; @@ -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; diff --git a/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java b/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java index aea5763a..ef55e477 100644 --- a/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java +++ b/src/main/java/graphql/annotations/processor/graphQLProcessors/GraphQLAnnotationsProcessor.java @@ -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, @@ -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. @@ -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); } diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java index e6317871..4728b6ee 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLFieldRetriever.java @@ -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 { @@ -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 args) { @@ -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) { diff --git a/src/main/java/graphql/annotations/processor/retrievers/GraphQLInputObjectRetriever.java b/src/main/java/graphql/annotations/processor/retrievers/GraphQLInputObjectRetriever.java index 51e10cc3..3541ac42 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/GraphQLInputObjectRetriever.java +++ b/src/main/java/graphql/annotations/processor/retrievers/GraphQLInputObjectRetriever.java @@ -36,7 +36,7 @@ public class GraphQLInputObjectRetriever { public GraphQLInputType getInputObject(graphql.schema.GraphQLType graphQLType, String newNamePrefix, Map 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) { @@ -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 typeRegistry) { + private GraphQLInputType handleGraphQLObjectType(GraphQLObjectType graphQLType, String newNamePrefix, Map typeRegistry) { GraphQLObjectType object = graphQLType; String newObjectName = newNamePrefix + object.getName(); GraphQLType objectInTypeRegistry = typeRegistry.get(newObjectName);