Skip to content

Commit d141674

Browse files
committed
Make it possible to provide custom prefixes AND postfixes for input object name generation. This is setup on the container.
The defaults were not changed and are equivalent to : container.setInputPrefix("Input"); container.setInputPostfix(""; If you want to change the naming generation to something more "compliant" with GraphQL's naming conventions you can change the configuration to: container.setInputPrefix(""); container.setInputPostfix("Input"); So for example if generating a name for the input type generated from the class Books it will be called InputBooks in the first configuration and BooksInput in the second configuration. This is also very useful if you are using prefixes to namespace your classes such as : ACMEBooks will be InputACMEBooks in the first configuration and ACMEBooksInput in the second configuration. As you can see the prefix and postfix configuration is also very flexible and the "Input" word is just a configuration variable now. By default however, nothing changes so existing projects should not break. Some unit tests were also added to make sure that the generation works properly.
1 parent e1ab013 commit d141674

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
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_POSTFIX;
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 inputPostfix = DEFAULT_INPUT_POSTFIX;
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 getInputPostfix() {
118+
return inputPostfix;
119+
}
120+
121+
public void setInputPostfix(String inputPostfix) {
122+
this.inputPostfix = inputPostfix;
123+
}
103124
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class GraphQLTypeRetriever {
4848
*
4949
* @param object the object class to examine*
5050
* @param container a class that hold several members that are required in order to build schema
51+
* @param isInput true if the type is an input type, false otherwise
5152
* @return a {@link GraphQLType} that represents that object class
5253
* @throws graphql.annotations.processor.exceptions.GraphQLAnnotationsException if the object class cannot be examined
5354
* @throws graphql.annotations.processor.exceptions.CannotCastMemberException if the object class cannot be examined
@@ -61,7 +62,7 @@ public GraphQLType getGraphQLType(Class<?> object, ProcessingElementsContainer c
6162
GraphQLType type;
6263

6364
if (isInput) {
64-
typeName = DEFAULT_INPUT_PREFIX + typeName;
65+
typeName = container.getInputPrefix() + typeName + container.getInputPostfix();
6566
}
6667

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public InputObjectBuilder(GraphQLObjectInfoRetriever graphQLObjectInfoRetriever,
5656

5757
public GraphQLInputObjectType.Builder getInputObjectBuilder(Class<?> object, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
5858
GraphQLInputObjectType.Builder builder = GraphQLInputObjectType.newInputObject();
59-
builder.name(DEFAULT_INPUT_PREFIX + graphQLObjectInfoRetriever.getTypeName(object));
59+
builder.name(container.getInputPrefix() + graphQLObjectInfoRetriever.getTypeName(object) + container.getInputPostfix());
6060
GraphQLDescription description = object.getAnnotation(GraphQLDescription.class);
6161
if (description != null) {
6262
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_POSTFIX = "";
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_POSTFIX;
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.setInputPostfix("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.setInputPostfix(DEFAULT_INPUT_POSTFIX);
747+
}
748+
730749
public static class UUIDTypeFunction implements TypeFunction {
731750
@Override
732751
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {

0 commit comments

Comments
 (0)