Skip to content

Commit 0dfdb23

Browse files
authored
Merge pull request #182 from Jahia/prettify-default-settings
Add global settings to force prettify on all field/method
2 parents 8a86a43 + 4e53f5c commit 0dfdb23

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class GraphQLFieldRetriever {
5555

5656
private DataFetcherConstructor dataFetcherConstructor;
5757

58+
private boolean alwaysPrettify = false;
59+
5860
public GraphQLFieldRetriever(DataFetcherConstructor dataFetcherConstructor) {
5961
this.dataFetcherConstructor = dataFetcherConstructor;
6062
}
@@ -66,7 +68,7 @@ public GraphQLFieldRetriever() {
6668
public GraphQLFieldDefinition getField(Method method, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
6769
GraphQLFieldDefinition.Builder builder = newFieldDefinition();
6870
TypeFunction typeFunction = getTypeFunction(method, container);
69-
builder.name(new MethodNameBuilder(method).build());
71+
builder.name(new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build());
7072
GraphQLOutputType outputType = (GraphQLOutputType) new MethodTypeBuilder(method, typeFunction, container, false).build();
7173

7274
boolean isConnection = ConnectionUtil.isConnection(method, outputType);
@@ -85,7 +87,7 @@ public GraphQLFieldDefinition getField(Method method, ProcessingElementsContaine
8587

8688
public GraphQLFieldDefinition getField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
8789
GraphQLFieldDefinition.Builder builder = newFieldDefinition();
88-
builder.name(new FieldNameBuilder(field).build());
90+
builder.name(new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build());
8991
TypeFunction typeFunction = getTypeFunction(field, container);
9092

9193
GraphQLType outputType = typeFunction.buildType(field.getType(), field.getAnnotatedType(), container);
@@ -104,15 +106,15 @@ public GraphQLFieldDefinition getField(Field field, ProcessingElementsContainer
104106

105107
public GraphQLInputObjectField getInputField(Method method, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
106108
GraphQLInputObjectField.Builder builder = newInputObjectField();
107-
builder.name(new MethodNameBuilder(method).build());
109+
builder.name(new MethodNameBuilder(method).alwaysPrettify(alwaysPrettify).build());
108110
TypeFunction typeFunction = getTypeFunction(method, container);
109111
GraphQLInputType inputType = (GraphQLInputType) new MethodTypeBuilder(method, typeFunction, container, true).build();
110112
return builder.type(inputType).description(new DescriptionBuilder(method).build()).build();
111113
}
112114

113115
public GraphQLInputObjectField getInputField(Field field, ProcessingElementsContainer container) throws GraphQLAnnotationsException {
114116
GraphQLInputObjectField.Builder builder = newInputObjectField();
115-
builder.name(new FieldNameBuilder(field).build());
117+
builder.name(new FieldNameBuilder(field).alwaysPrettify(alwaysPrettify).build());
116118
TypeFunction typeFunction = getTypeFunction(field, container);
117119
GraphQLType graphQLType = typeFunction.buildType(true, field.getType(), field.getAnnotatedType(), container);
118120
return builder.type((GraphQLInputType) graphQLType).description(new DescriptionBuilder(field).build()).build();
@@ -195,6 +197,10 @@ private GraphQLObjectType getActualType(GraphQLObjectType type, Map<String, grap
195197
return type;
196198
}
197199

200+
public void setAlwaysPrettify(boolean alwaysPrettify) {
201+
this.alwaysPrettify = alwaysPrettify;
202+
}
203+
198204
@Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
199205
public void setDataFetcherConstructor(DataFetcherConstructor dataFetcherConstructor) {
200206
this.dataFetcherConstructor = dataFetcherConstructor;

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/field/FieldNameBuilder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,20 @@
2525
public class FieldNameBuilder implements Builder<String> {
2626
private Field field;
2727

28+
private boolean alwaysPrettify = false;
29+
2830
public FieldNameBuilder(Field field) {
2931
this.field = field;
3032
}
3133

34+
public FieldNameBuilder alwaysPrettify(boolean alwaysPrettify) {
35+
this.alwaysPrettify = alwaysPrettify;
36+
return this;
37+
}
38+
3239
@Override
3340
public String build() {
34-
if (field.isAnnotationPresent(GraphQLPrettify.class) && !field.isAnnotationPresent(GraphQLName.class)) {
41+
if ((alwaysPrettify || field.isAnnotationPresent(GraphQLPrettify.class)) && !field.isAnnotationPresent(GraphQLName.class)) {
3542
return toGraphqlName(prettifyName(field.getName()));
3643
}
3744
GraphQLName name = field.getAnnotation(GraphQLName.class);

src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import graphql.annotations.annotationTypes.GraphQLName;
1818
import graphql.annotations.annotationTypes.GraphQLPrettify;
1919
import graphql.annotations.processor.retrievers.fieldBuilders.Builder;
20+
import graphql.annotations.processor.retrievers.fieldBuilders.field.FieldNameBuilder;
2021

2122
import java.lang.reflect.Method;
2223

@@ -25,13 +26,20 @@
2526
public class MethodNameBuilder implements Builder<String> {
2627
private Method method;
2728

29+
private boolean alwaysPrettify = false;
30+
2831
public MethodNameBuilder(Method method) {
2932
this.method = method;
3033
}
3134

35+
public MethodNameBuilder alwaysPrettify(boolean alwaysPrettify) {
36+
this.alwaysPrettify = alwaysPrettify;
37+
return this;
38+
}
39+
3240
@Override
3341
public String build() {
34-
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
42+
if ((alwaysPrettify || method.isAnnotationPresent(GraphQLPrettify.class)) && !method.isAnnotationPresent(GraphQLName.class)) {
3543
return toGraphqlName(prettifyName(method.getName()));
3644
}
3745
GraphQLName name = method.getAnnotation(GraphQLName.class);

src/test/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ public void build_graphQLNameAnnotationNotExistsWithGetPrefix_returnCorrectName(
6767
assertEquals(name, "getTest");
6868
}
6969

70+
@Test
71+
public void build_graphQLNameAnnotationNotExistsWithGetPrefixAndPrettify_returnCorrectName() throws NoSuchMethodException {
72+
// arrange
73+
Method method = getClass().getMethod("getTest");
74+
MethodNameBuilder methodNameBuilder = new MethodNameBuilder(method);
75+
76+
// act
77+
String name = methodNameBuilder.alwaysPrettify(true).build();
78+
79+
// assert
80+
assertEquals(name, "test");
81+
}
82+
7083
@Test
7184
public void build_graphQLNameAnnotationNotExistsWithIsPrefix_returnCorrectName() throws NoSuchMethodException{
7285
// arrange

0 commit comments

Comments
 (0)