Skip to content

Commit

Permalink
Merge pull request #171 from guy120494/method-name-is-not-change-when…
Browse files Browse the repository at this point in the history
…-it-becomes-field

Method name is not changed when it becomes field
  • Loading branch information
yarinvak authored May 13, 2018
2 parents 5ec0c0e + dcae807 commit 9c7a3a9
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2016 Yurii Rashkovskii
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package graphql.annotations.annotationTypes;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface GraphQLPrettify {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package graphql.annotations.processor.retrievers.fieldBuilders.field;

import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.GraphQLPrettify;
import graphql.annotations.processor.retrievers.fieldBuilders.Builder;

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

@Override
public String build() {
if (field.isAnnotationPresent(GraphQLPrettify.class) && !field.isAnnotationPresent(GraphQLName.class)) {
return toGraphqlName(prettifyName(field.getName()));
}
GraphQLName name = field.getAnnotation(GraphQLName.class);
return toGraphqlName(name == null ? field.getName() : name.value());
}

private String prettifyName(String originalName) {
String name = originalName.replaceFirst("^(is|get|set)(.+)", "$2");
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package graphql.annotations.processor.retrievers.fieldBuilders.method;

import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.GraphQLPrettify;
import graphql.annotations.processor.retrievers.fieldBuilders.Builder;

import java.lang.reflect.Method;
Expand All @@ -30,9 +31,15 @@ public MethodNameBuilder(Method method) {

@Override
public String build() {
String name = method.getName().replaceFirst("^(is|get|set)(.+)", "$2");
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
GraphQLName nameAnn = method.getAnnotation(GraphQLName.class);
return toGraphqlName(nameAnn == null ? name : nameAnn.value());
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
return toGraphqlName(pretifyName(method.getName()));
}
GraphQLName name = method.getAnnotation(GraphQLName.class);
return toGraphqlName(name == null ? method.getName() : name.value());
}

private String pretifyName(String originalName) {
String name = originalName.replaceFirst("^(is|get|set)(.+)", "$2");
return Character.toLowerCase(name.charAt(0)) + name.substring(1);
}
}
20 changes: 7 additions & 13 deletions src/test/java/graphql/annotations/GraphQLDataFetcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.PropertyDataFetcher;
import graphql.schema.*;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.HashMap;

import static graphql.schema.GraphQLSchema.newSchema;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.*;

public class GraphQLDataFetcherTest {

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

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

// Then
final HashMap<String, Object> data = (HashMap) result.getData();
final HashMap<String, Object> data = result.getData();
assertNotNull(data);
assertFalse((Boolean)data.get("great"));
}
Expand All @@ -83,12 +77,12 @@ public void shouldUseTargetAndArgumentsForDataFetcherDeclaredInMethod() {
final GraphQL graphql = GraphQL.newGraphQL(schema).build();

// When
final ExecutionResult result = graphql.execute("{sample {bad}}");
final ExecutionResult result = graphql.execute("{sample {isBad}}");

// Then
final HashMap<String, Object> data = (HashMap) result.getData();
final HashMap<String, Object> data = result.getData();
assertNotNull(data);
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("bad"));
assertTrue(((HashMap<String,Boolean>)data.get("sample")).get("isBad"));
}

@GraphQLName("Query")
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/graphql/annotations/GraphQLEnumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.schema.*;
import graphql.schema.GraphQLObjectType;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

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

ExecutionResult result = graphql.execute("{ defaultUser{ name } }");
assertEquals(result.getData().toString(), "{defaultUser={name=ONE}}");
ExecutionResult result = graphql.execute("{ defaultUser{ getName } }");
assertEquals(result.getData().toString(), "{defaultUser={getName=ONE}}");
}

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

ExecutionResult result = graphql.execute("{ user(param:TWO){ name } }");
assertEquals(result.getData().toString(), "{user={name=TWO}}");
ExecutionResult result = graphql.execute("{ user(param:TWO){ getName } }");
assertEquals(result.getData().toString(), "{user={getName=TWO}}");
}


Expand Down
7 changes: 4 additions & 3 deletions src/test/java/graphql/annotations/GraphQLExtensionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import graphql.schema.*;
import org.testng.annotations.Test;

import java.util.Comparator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -80,7 +81,7 @@ public TestObjectExtensionInvalid(TestObject obj) {
}

@GraphQLField
public String getField() {
public String field() {
return "invalid";
}
}
Expand All @@ -102,7 +103,7 @@ public void fields() {
List<GraphQLFieldDefinition> fields = object.getFieldDefinitions();
assertEquals(fields.size(), 5);

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

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

ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{field field2 field3 field4 field5}", new GraphQLExtensionsTest.TestObject());
Map<String, Object> data = (Map<String, Object>) result.getData();
Map<String, Object> data = result.getData();
assertEquals(data.get("field"), "test");
assertEquals(data.get("field2"), "test test2");
assertEquals(data.get("field3"), "test test3");
Expand Down
9 changes: 2 additions & 7 deletions src/test/java/graphql/annotations/GraphQLFragmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static org.testng.AssertJUnit.assertEquals;

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

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

// Then
Expand Down
36 changes: 20 additions & 16 deletions src/test/java/graphql/annotations/GraphQLInputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.GraphQLTypeResolver;
import graphql.annotations.processor.GraphQLAnnotations;
import graphql.annotations.processor.ProcessingElementsContainer;
import graphql.annotations.processor.exceptions.GraphQLAnnotationsException;
import graphql.schema.*;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import graphql.schema.TypeResolver;
import org.testng.annotations.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static graphql.schema.GraphQLSchema.newSchema;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -284,9 +288,9 @@ public void testInputAndOutputWithSameName() {
// arrange + act
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QueryInputAndOutput.class)).build();
// assert
assertEquals(schema.getQueryType().getFieldDefinition("hero").getType().getName(), "hero");
assertEquals(schema.getQueryType().getFieldDefinition("string").getArgument("input").getType().getName(), "Inputhero");
assertEquals(((GraphQLInputObjectType) schema.getQueryType().getFieldDefinition("string")
assertEquals(schema.getQueryType().getFieldDefinition("getHero").getType().getName(), "hero");
assertEquals(schema.getQueryType().getFieldDefinition("getString").getArgument("input").getType().getName(), "Inputhero");
assertEquals(((GraphQLInputObjectType) schema.getQueryType().getFieldDefinition("getString")
.getArgument("input").getType()).getField("skill").getType().getName(), "InputSkill");
}

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

@GraphQLName("A")
Expand Down Expand Up @@ -345,13 +349,13 @@ public void testInputAndOutputWithSameNameWithChildrenWithSameName(){
// arrange + act
GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QuerySameNameWithChildren.class)).build();
// assert
assertEquals(schema.getQueryType().getFieldDefinition("aout").getType().getName(), "A");
assertEquals(schema.getQueryType().getFieldDefinition("aout").getType().getClass(),GraphQLObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("bout").getType().getName(), "B");
assertEquals(schema.getQueryType().getFieldDefinition("bout").getType().getClass(),GraphQLObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("a").getArgument("input").getType().getName(), "InputA");
assertEquals(schema.getQueryType().getFieldDefinition("a").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("b").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getName(), "A");
assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getClass(),GraphQLObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("getBout").getType().getName(), "B");
assertEquals(schema.getQueryType().getFieldDefinition("getBout").getType().getClass(),GraphQLObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("input").getType().getName(), "InputA");
assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);
assertEquals(schema.getQueryType().getFieldDefinition("getB").getArgument("input").getType().getClass(), GraphQLInputObjectType.class);

}
}
Loading

0 comments on commit 9c7a3a9

Please sign in to comment.