Skip to content

Commit 81a436d

Browse files
committed
add GraphQLPrettify annotation
1 parent 4a5ac27 commit 81a436d

File tree

4 files changed

+101
-24
lines changed

4 files changed

+101
-24
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package graphql.annotations.annotationTypes;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface GraphQLPrettify {
11+
}

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,6 +15,7 @@
1515
package graphql.annotations.processor.retrievers.fieldBuilders.field;
1616

1717
import graphql.annotations.annotationTypes.GraphQLName;
18+
import graphql.annotations.annotationTypes.GraphQLPrettify;
1819
import graphql.annotations.processor.retrievers.fieldBuilders.Builder;
1920

2021
import java.lang.reflect.Field;
@@ -30,7 +31,16 @@ public FieldNameBuilder(Field field) {
3031

3132
@Override
3233
public String build() {
34+
if (field.isAnnotationPresent(GraphQLPrettify.class) && !field.isAnnotationPresent(GraphQLName.class)) {
35+
return toGraphqlName(prettifyName(field.getName()));
36+
}
3337
GraphQLName name = field.getAnnotation(GraphQLName.class);
3438
return toGraphqlName(name == null ? field.getName() : name.value());
3539
}
40+
41+
private String prettifyName(String originalName) {
42+
String name = originalName.replaceFirst("^(is|get|set)(.+)", "$2");
43+
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
44+
}
45+
3646
}

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,6 +15,7 @@
1515
package graphql.annotations.processor.retrievers.fieldBuilders.method;
1616

1717
import graphql.annotations.annotationTypes.GraphQLName;
18+
import graphql.annotations.annotationTypes.GraphQLPrettify;
1819
import graphql.annotations.processor.retrievers.fieldBuilders.Builder;
1920

2021
import java.lang.reflect.Method;
@@ -30,7 +31,15 @@ public MethodNameBuilder(Method method) {
3031

3132
@Override
3233
public String build() {
34+
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
35+
return toGraphqlName(pretifyName(method.getName()));
36+
}
3337
GraphQLName name = method.getAnnotation(GraphQLName.class);
3438
return toGraphqlName(name == null ? method.getName() : name.value());
3539
}
40+
41+
private String pretifyName(String originalName) {
42+
String name = originalName.replaceFirst("^(is|get|set)(.+)", "$2");
43+
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
44+
}
3645
}

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

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2016 Yurii Rashkovskii
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
* <p>
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -78,7 +78,7 @@ public String fieldWithArgs(@GraphQLName("a") @GraphQLNonNull String a, @GraphQL
7878
}
7979

8080
@GraphQLField
81-
public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env,@GraphQLName("a") String a, @GraphQLName("b") String b) {
81+
public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env, @GraphQLName("a") String a, @GraphQLName("b") String b) {
8282
return a;
8383
}
8484

@@ -141,7 +141,7 @@ public static class TestMappedObject {
141141
public String aaa;
142142
}
143143

144-
public static class TestObjectDB{
144+
public static class TestObjectDB {
145145
public String aaa;
146146

147147
private String name;
@@ -151,11 +151,12 @@ public String getName() {
151151
}
152152

153153
public TestObjectDB(String name, String aaa) {
154-
this.name = name; this.aaa=aaa;
154+
this.name = name;
155+
this.aaa = aaa;
155156
}
156157
}
157158

158-
public static class TestQuery{
159+
public static class TestQuery {
159160
@GraphQLField
160161
@GraphQLDataFetcher(ObjectFetcher.class)
161162
public TestMappedObject object;
@@ -169,15 +170,62 @@ public TestObjectDB get(DataFetchingEnvironment environment) {
169170
}
170171
}
171172

173+
public static class NameTest {
174+
@GraphQLField
175+
public Boolean isCool;
176+
177+
@GraphQLField
178+
@GraphQLPrettify
179+
public Boolean isAwesome;
180+
181+
@GraphQLField
182+
@GraphQLPrettify
183+
@GraphQLName("yarinnn")
184+
public Boolean isYarin;
185+
186+
@GraphQLField
187+
public String getX() {
188+
return "Asdf0";
189+
}
190+
191+
@GraphQLField
192+
@GraphQLPrettify
193+
public String getY() {
194+
return "asd";
195+
}
196+
197+
@GraphQLField
198+
@GraphQLPrettify
199+
@GraphQLName("daniel")
200+
public String setM(){
201+
return "Asdf";
202+
}
203+
204+
}
205+
172206
@Test
173-
public void fetchTestMappedObject_assertNameIsMappedFromDBObject(){
207+
public void objectCreation_nameIsCorrect() {
208+
// Act
209+
GraphQLObjectType object = GraphQLAnnotations.object(NameTest.class);
210+
211+
// Assert
212+
assertNotNull(object.getFieldDefinition("awesome"));
213+
assertNotNull(object.getFieldDefinition("isCool"));
214+
assertNotNull(object.getFieldDefinition("yarinnn"));
215+
assertNotNull(object.getFieldDefinition("getX"));
216+
assertNotNull(object.getFieldDefinition("y"));
217+
assertNotNull(object.getFieldDefinition("daniel"));
218+
}
219+
220+
@Test
221+
public void fetchTestMappedObject_assertNameIsMappedFromDBObject() {
174222
GraphQLObjectType object = GraphQLAnnotations.object(TestQuery.class);
175223
GraphQLSchema schema = newSchema().query(object).build();
176224

177225
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{object {name aaa}}");
178226
assertTrue(result.getErrors().isEmpty());
179-
assertEquals(((LinkedHashMap)(((LinkedHashMap)result.getData()).get("object"))).get("name"), "test");
180-
assertEquals(((LinkedHashMap)(((LinkedHashMap)result.getData()).get("object"))).get("aaa"), "test");
227+
assertEquals(((LinkedHashMap) (((LinkedHashMap) result.getData()).get("object"))).get("name"), "test");
228+
assertEquals(((LinkedHashMap) (((LinkedHashMap) result.getData()).get("object"))).get("aaa"), "test");
181229
}
182230

183231
@Test
@@ -540,7 +588,7 @@ public static class Class2 {
540588
@Test
541589
public void recursiveTypes() {
542590
GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations();
543-
GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getObject(Class1.class,graphQLAnnotations.getContainer());
591+
GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getObject(Class1.class, graphQLAnnotations.getContainer());
544592
GraphQLSchema schema = newSchema().query(object).build();
545593

546594
Class1 class1 = new Class1();
@@ -593,7 +641,7 @@ public void customTypeFunction() {
593641
assertEquals(object.getFieldDefinition("id").getType(), GraphQLString);
594642
}
595643

596-
public static class TestInputArgument {
644+
public static class TestInputArgument {
597645
@GraphQLField
598646
public String a;
599647
@GraphQLField
@@ -621,8 +669,7 @@ public Collection<TestInputArgument> inputs() {
621669
}
622670

623671

624-
625-
public static class TestObjectInput {
672+
public static class TestObjectInput {
626673
@GraphQLField
627674
public String test(@GraphQLName("other") int other, @GraphQLName("arg") TestInputArgument arg) {
628675
return arg.a;
@@ -634,7 +681,7 @@ public String test2(@GraphQLName("other") int other, @GraphQLName("arg") TestCom
634681
}
635682
}
636683

637-
public static class InputObject{
684+
public static class InputObject {
638685
@GraphQLField
639686
int a;
640687

@@ -688,7 +735,7 @@ public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
688735

689736
@Override
690737
public GraphQLType buildType(boolean input, Class<?> aClass, AnnotatedType annotatedType, ProcessingElementsContainer container) {
691-
return buildType(input,aClass,annotatedType);
738+
return buildType(input, aClass, annotatedType);
692739
}
693740

694741
@Override

0 commit comments

Comments
 (0)