Skip to content

Commit 9c7a3a9

Browse files
authored
Merge pull request #171 from guy120494/method-name-is-not-change-when-it-becomes-field
Method name is not changed when it becomes field
2 parents 5ec0c0e + dcae807 commit 9c7a3a9

File tree

13 files changed

+200
-122
lines changed

13 files changed

+200
-122
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.annotationTypes;
16+
17+
import java.lang.annotation.ElementType;
18+
import java.lang.annotation.Retention;
19+
import java.lang.annotation.RetentionPolicy;
20+
import java.lang.annotation.Target;
21+
22+
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
23+
@Retention(RetentionPolicy.RUNTIME)
24+
public @interface GraphQLPrettify {
25+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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,9 +31,15 @@ public MethodNameBuilder(Method method) {
3031

3132
@Override
3233
public String build() {
33-
String name = method.getName().replaceFirst("^(is|get|set)(.+)", "$2");
34-
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
35-
GraphQLName nameAnn = method.getAnnotation(GraphQLName.class);
36-
return toGraphqlName(nameAnn == null ? name : nameAnn.value());
34+
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
35+
return toGraphqlName(pretifyName(method.getName()));
36+
}
37+
GraphQLName name = method.getAnnotation(GraphQLName.class);
38+
return toGraphqlName(name == null ? method.getName() : name.value());
39+
}
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);
3744
}
3845
}

src/test/java/graphql/annotations/GraphQLDataFetcherTest.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,14 @@
2020
import graphql.annotations.annotationTypes.GraphQLField;
2121
import graphql.annotations.annotationTypes.GraphQLName;
2222
import graphql.annotations.processor.GraphQLAnnotations;
23-
import graphql.schema.DataFetcher;
24-
import graphql.schema.DataFetchingEnvironment;
25-
import graphql.schema.GraphQLObjectType;
26-
import graphql.schema.GraphQLSchema;
27-
import graphql.schema.PropertyDataFetcher;
23+
import graphql.schema.*;
2824
import org.testng.annotations.BeforeMethod;
2925
import org.testng.annotations.Test;
3026

3127
import java.util.HashMap;
3228

3329
import static graphql.schema.GraphQLSchema.newSchema;
34-
import static org.testng.Assert.assertFalse;
35-
import static org.testng.Assert.assertNotNull;
36-
import static org.testng.Assert.assertTrue;
30+
import static org.testng.Assert.*;
3731

3832
public class GraphQLDataFetcherTest {
3933

@@ -53,7 +47,7 @@ public void shouldUsePreferredConstructor() {
5347
final ExecutionResult result = graphql.execute("{sample {isGreat isBad}}");
5448

5549
// Then
56-
final HashMap<String, Object> data = (HashMap) result.getData();
50+
final HashMap<String, Object> data = result.getData();
5751
assertNotNull(data);
5852
assertTrue(((HashMap<String, Boolean>) data.get("sample")).get("isGreat"));
5953
assertTrue(((HashMap<String, Boolean>) data.get("sample")).get("isBad"));
@@ -70,7 +64,7 @@ public void shouldUseProvidedSoloArgumentForDataFetcherDeclaredInMethod() {
7064
final ExecutionResult result = graphql.execute("{great}");
7165

7266
// Then
73-
final HashMap<String, Object> data = (HashMap) result.getData();
67+
final HashMap<String, Object> data = result.getData();
7468
assertNotNull(data);
7569
assertFalse((Boolean)data.get("great"));
7670
}
@@ -83,12 +77,12 @@ public void shouldUseTargetAndArgumentsForDataFetcherDeclaredInMethod() {
8377
final GraphQL graphql = GraphQL.newGraphQL(schema).build();
8478

8579
// When
86-
final ExecutionResult result = graphql.execute("{sample {bad}}");
80+
final ExecutionResult result = graphql.execute("{sample {isBad}}");
8781

8882
// Then
89-
final HashMap<String, Object> data = (HashMap) result.getData();
83+
final HashMap<String, Object> data = result.getData();
9084
assertNotNull(data);
91-
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("bad"));
85+
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("isBad"));
9286
}
9387

9488
@GraphQLName("Query")

src/test/java/graphql/annotations/GraphQLEnumTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import graphql.annotations.annotationTypes.GraphQLField;
2020
import graphql.annotations.annotationTypes.GraphQLName;
2121
import graphql.annotations.processor.GraphQLAnnotations;
22-
import graphql.schema.*;
22+
import graphql.schema.GraphQLObjectType;
2323
import org.testng.annotations.BeforeMethod;
2424
import org.testng.annotations.Test;
2525

@@ -72,17 +72,17 @@ public void test() throws IllegalAccessException, NoSuchMethodException, Instant
7272
GraphQLObjectType queryObject = GraphQLAnnotations.object(Query.class);
7373
GraphQL graphql = GraphQL.newGraphQL(newSchema().query(queryObject).build()).build();
7474

75-
ExecutionResult result = graphql.execute("{ defaultUser{ name } }");
76-
assertEquals(result.getData().toString(), "{defaultUser={name=ONE}}");
75+
ExecutionResult result = graphql.execute("{ defaultUser{ getName } }");
76+
assertEquals(result.getData().toString(), "{defaultUser={getName=ONE}}");
7777
}
7878

7979
@Test
8080
public void testAsInput() throws IllegalAccessException, NoSuchMethodException, InstantiationException {
8181
GraphQLObjectType queryObject = GraphQLAnnotations.object(Query.class);
8282
GraphQL graphql = GraphQL.newGraphQL(newSchema().query(queryObject).build()).build();
8383

84-
ExecutionResult result = graphql.execute("{ user(param:TWO){ name } }");
85-
assertEquals(result.getData().toString(), "{user={name=TWO}}");
84+
ExecutionResult result = graphql.execute("{ user(param:TWO){ getName } }");
85+
assertEquals(result.getData().toString(), "{user={getName=TWO}}");
8686
}
8787

8888

src/test/java/graphql/annotations/GraphQLExtensionsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import graphql.schema.*;
2424
import org.testng.annotations.Test;
2525

26+
import java.util.Comparator;
2627
import java.util.List;
2728
import java.util.Map;
2829

@@ -80,7 +81,7 @@ public TestObjectExtensionInvalid(TestObject obj) {
8081
}
8182

8283
@GraphQLField
83-
public String getField() {
84+
public String field() {
8485
return "invalid";
8586
}
8687
}
@@ -102,7 +103,7 @@ public void fields() {
102103
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
103104
assertEquals(fields.size(), 5);
104105

105-
fields.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
106+
fields.sort(Comparator.comparing(GraphQLFieldDefinition::getName));
106107

107108
assertEquals(fields.get(0).getName(), "field");
108109
assertEquals(fields.get(1).getName(), "field2");
@@ -122,7 +123,7 @@ public void values() {
122123
GraphQLSchema schemaInherited = newSchema().query(object).build();
123124

124125
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field field2 field3 field4 field5}", new GraphQLExtensionsTest.TestObject());
125-
Map<String, Object> data = (Map<String, Object>) result.getData();
126+
Map<String, Object> data = result.getData();
126127
assertEquals(data.get("field"), "test");
127128
assertEquals(data.get("field2"), "test test2");
128129
assertEquals(data.get("field3"), "test test3");

src/test/java/graphql/annotations/GraphQLFragmentTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@
2828
import org.testng.annotations.BeforeMethod;
2929
import org.testng.annotations.Test;
3030

31-
import java.util.Arrays;
32-
import java.util.HashMap;
33-
import java.util.HashSet;
34-
import java.util.List;
35-
import java.util.Map;
36-
import java.util.Set;
31+
import java.util.*;
3732

3833
import static org.testng.AssertJUnit.assertEquals;
3934

@@ -76,7 +71,7 @@ public void testInterfaceInlineFragment() throws Exception {
7671
GraphQL graphQL2 = GraphQL.newGraphQL(schema).build();
7772

7873
// When
79-
ExecutionResult graphQLResult = graphQL2.execute("{items { ... on MyObject {a, my {b}} ... on MyObject2 {a, b} }}", new RootObject());
74+
ExecutionResult graphQLResult = graphQL2.execute("{getItems { ... on MyObject {getA, getMy {getB}} ... on MyObject2 {getA, getB} }}", new RootObject());
8075
Set resultMap = ((Map) graphQLResult.getData()).entrySet();
8176

8277
// Then

src/test/java/graphql/annotations/GraphQLInputTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
import graphql.annotations.annotationTypes.GraphQLName;
2222
import graphql.annotations.annotationTypes.GraphQLTypeResolver;
2323
import graphql.annotations.processor.GraphQLAnnotations;
24-
import graphql.annotations.processor.ProcessingElementsContainer;
2524
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
26-
import graphql.schema.*;
25+
import graphql.schema.GraphQLInputObjectType;
26+
import graphql.schema.GraphQLObjectType;
27+
import graphql.schema.GraphQLSchema;
28+
import graphql.schema.TypeResolver;
2729
import org.testng.annotations.Test;
2830

29-
import java.util.*;
30-
import java.util.stream.Collectors;
31+
import java.util.Collections;
32+
import java.util.HashMap;
33+
import java.util.List;
34+
import java.util.Map;
3135

3236
import static graphql.schema.GraphQLSchema.newSchema;
3337
import static org.testng.Assert.assertEquals;
@@ -284,9 +288,9 @@ public void testInputAndOutputWithSameName() {
284288
// arrange + act
285289
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput.class)).build();
286290
// assert
287-
assertEquals(schema.getQueryType().getFieldDefinition("hero").getType().getName(), "hero");
288-
assertEquals(schema.getQueryType().getFieldDefinition("string").getArgument("input").getType().getName(), "Inputhero");
289-
assertEquals(((GraphQLInputObjectType) schema.getQueryType().getFieldDefinition("string")
291+
assertEquals(schema.getQueryType().getFieldDefinition("getHero").getType().getName(), "hero");
292+
assertEquals(schema.getQueryType().getFieldDefinition("getString").getArgument("input").getType().getName(), "Inputhero");
293+
assertEquals(((GraphQLInputObjectType) schema.getQueryType().getFieldDefinition("getString")
290294
.getArgument("input").getType()).getField("skill").getType().getName(), "InputSkill");
291295
}
292296

@@ -295,8 +299,8 @@ public void testInputAndOutputSameClass() {
295299
// arrange + act
296300
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput2.class)).build();
297301
// assert
298-
assertEquals(schema.getQueryType().getFieldDefinition("skill").getType().getName(), "Skill");
299-
assertEquals(schema.getQueryType().getFieldDefinition("a").getArgument("skill").getType().getName(), "InputSkill");
302+
assertEquals(schema.getQueryType().getFieldDefinition("getSkill").getType().getName(), "Skill");
303+
assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("skill").getType().getName(), "InputSkill");
300304
}
301305

302306
@GraphQLName("A")
@@ -345,13 +349,13 @@ public void testInputAndOutputWithSameNameWithChildrenWithSameName(){
345349
// arrange + act
346350
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QuerySameNameWithChildren.class)).build();
347351
// assert
348-
assertEquals(schema.getQueryType().getFieldDefinition("aout").getType().getName(), "A");
349-
assertEquals(schema.getQueryType().getFieldDefinition("aout").getType().getClass(),GraphQLObjectType.class);
350-
assertEquals(schema.getQueryType().getFieldDefinition("bout").getType().getName(), "B");
351-
assertEquals(schema.getQueryType().getFieldDefinition("bout").getType().getClass(),GraphQLObjectType.class);
352-
assertEquals(schema.getQueryType().getFieldDefinition("a").getArgument("input").getType().getName(), "InputA");
353-
assertEquals(schema.getQueryType().getFieldDefinition("a").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
354-
assertEquals(schema.getQueryType().getFieldDefinition("b").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
352+
assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getName(), "A");
353+
assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getClass(),GraphQLObjectType.class);
354+
assertEquals(schema.getQueryType().getFieldDefinition("getBout").getType().getName(), "B");
355+
assertEquals(schema.getQueryType().getFieldDefinition("getBout").getType().getClass(),GraphQLObjectType.class);
356+
assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("input").getType().getName(), "InputA");
357+
assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
358+
assertEquals(schema.getQueryType().getFieldDefinition("getB").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
355359

356360
}
357361
}

0 commit comments

Comments
 (0)