Skip to content

Commit ce5c214

Browse files
authored
Merge pull request #184 from sergehuber/custom-input-prefix-and-postfix
Make it possible to provide custom prefixes AND postfixes for input objects
2 parents e1ab013 + d98d9e3 commit ce5c214

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.Set;
2828
import java.util.Stack;
2929

30+
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX;
31+
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
32+
3033
public class ProcessingElementsContainer {
3134

3235
private TypeFunction defaultTypeFunction;
@@ -35,6 +38,8 @@ public class ProcessingElementsContainer {
3538
private Map<String, graphql.schema.GraphQLDirective> directiveRegistry;
3639
private Map<Class<?>, Set<Class<?>>> extensionsTypeRegistry;
3740
private Stack<String> processing;
41+
private String inputPrefix = DEFAULT_INPUT_PREFIX;
42+
private String inputSuffix = DEFAULT_INPUT_SUFFIX;
3843

3944
public Map<String, GraphQLDirective> getDirectiveRegistry() {
4045
return directiveRegistry;
@@ -100,4 +105,20 @@ public Stack<String> getProcessing() {
100105
public void setProcessing(Stack<String> processing) {
101106
this.processing = processing;
102107
}
108+
109+
public String getInputPrefix() {
110+
return inputPrefix;
111+
}
112+
113+
public void setInputPrefix(String inputPrefix) {
114+
this.inputPrefix = inputPrefix;
115+
}
116+
117+
public String getInputSuffix() {
118+
return inputSuffix;
119+
}
120+
121+
public void setInputSuffix(String inputSuffix) {
122+
this.inputSuffix = inputSuffix;
123+
}
103124
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import graphql.schema.*;
2727
import org.osgi.service.component.annotations.*;
2828

29-
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
30-
3129
@Component(service = GraphQLTypeRetriever.class, immediate = true)
3230
public class GraphQLTypeRetriever {
3331

@@ -48,6 +46,7 @@ public class GraphQLTypeRetriever {
4846
*
4947
* @param object the object class to examine*
5048
* @param container a class that hold several members that are required in order to build schema
49+
* @param isInput true if the type is an input type, false otherwise
5150
* @return a {@link GraphQLType} that represents that object class
5251
* @throws graphql.annotations.processor.exceptions.GraphQLAnnotationsException if the object class cannot be examined
5352
* @throws graphql.annotations.processor.exceptions.CannotCastMemberException if the object class cannot be examined
@@ -61,7 +60,7 @@ public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer c
6160
GraphQLType type;
6261

6362
if (isInput) {
64-
typeName = DEFAULT_INPUT_PREFIX + typeName;
63+
typeName = container.getInputPrefix() + typeName + container.getInputSuffix();
6564
}
6665

6766
if (container.getProcessing().contains(typeName)) {

src/main/java/graphql/annotations/processor/typeBuilders/InputObjectBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.ArrayList;
3030
import java.util.List;
3131

32-
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
3332
import static graphql.annotations.processor.util.ObjectUtil.getAllFields;
3433

3534
public class InputObjectBuilder {
@@ -56,7 +55,7 @@ public InputObjectBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever,
5655

5756
public GraphQLInputObjectType.Builder getInputObjectBuilder(Class<?> object, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
5857
GraphQLInputObjectType.Builder builder = GraphQLInputObjectType.newInputObject();
59-
builder.name(DEFAULT_INPUT_PREFIX + graphQLObjectInfoRetriever.getTypeName(object));
58+
builder.name(container.getInputPrefix() + graphQLObjectInfoRetriever.getTypeName(object) + container.getInputSuffix());
6059
GraphQLDescription description = object.getAnnotation(GraphQLDescription.class);
6160
if (description != null) {
6261
builder.description(description.value());

src/main/java/graphql/annotations/processor/util/InputPropertiesUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616

1717
public class InputPropertiesUtil {
1818
public static final String DEFAULT_INPUT_PREFIX = "Input";
19+
public static final String DEFAULT_INPUT_SUFFIX = "";
1920
}

src/test/java/graphql/annotations/GraphQLObjectTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.util.function.Supplier;
4141

4242
import static graphql.Scalars.GraphQLString;
43+
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_SUFFIX;
44+
import static graphql.annotations.processor.util.InputPropertiesUtil.DEFAULT_INPUT_PREFIX;
4345
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
4446
import static graphql.schema.GraphQLSchema.newSchema;
4547
import static org.testng.Assert.*;
@@ -724,9 +726,26 @@ public void inputObject() {
724726
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()).
725727
getInputObjectBuilder(InputObject.class, GraphQLAnnotations.getInstance().getContainer()).build();
726728

729+
assertEquals(type.getName(), DEFAULT_INPUT_PREFIX + InputObject.class.getSimpleName(), "Type name prefix did not match expected value");
727730
assertEquals(type.getFields().size(), InputObject.class.getDeclaredFields().length);
728731
}
729732

733+
@Test
734+
public void inputObjectCustomPrefixes() {
735+
GraphQLObjectInfoRetriever graphQLObjectInfoRetriever = new GraphQLObjectInfoRetriever();
736+
ProcessingElementsContainer container = GraphQLAnnotations.getInstance().getContainer();
737+
container.setInputPrefix("");
738+
container.setInputSuffix("Input");
739+
GraphQLInputObjectType type = new InputObjectBuilder(graphQLObjectInfoRetriever, new ParentalSearch(graphQLObjectInfoRetriever),
740+
new BreadthFirstSearch(graphQLObjectInfoRetriever), new GraphQLFieldRetriever()).
741+
getInputObjectBuilder(InputObject.class, GraphQLAnnotations.getInstance().getContainer()).build();
742+
743+
assertEquals(type.getName(), "" + InputObject.class.getSimpleName() + "Input", "Type name prefix did not match expected value");
744+
assertEquals(type.getFields().size(), InputObject.class.getDeclaredFields().length);
745+
container.setInputPrefix(DEFAULT_INPUT_PREFIX);
746+
container.setInputSuffix(DEFAULT_INPUT_SUFFIX);
747+
}
748+
730749
public static class UUIDTypeFunction implements TypeFunction {
731750
@Override
732751
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {

0 commit comments

Comments
 (0)