diff --git a/src/main/java/graphql/annotations/annotationTypes/GraphQLPrettify.java b/src/main/java/graphql/annotations/annotationTypes/GraphQLPrettify.java new file mode 100644 index 00000000..8307892e --- /dev/null +++ b/src/main/java/graphql/annotations/annotationTypes/GraphQLPrettify.java @@ -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 { +} diff --git a/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/field/FieldNameBuilder.java b/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/field/FieldNameBuilder.java index 6d19ce93..f609307a 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/field/FieldNameBuilder.java +++ b/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/field/FieldNameBuilder.java @@ -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; @@ -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); + } + } diff --git a/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilder.java b/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilder.java index b62bd36d..b4ba519c 100644 --- a/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilder.java +++ b/src/main/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilder.java @@ -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; @@ -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); } } diff --git a/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java b/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java index 1279687e..1cc0626d 100644 --- a/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java +++ b/src/test/java/graphql/annotations/GraphQLDataFetcherTest.java @@ -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 { @@ -53,7 +47,7 @@ public void shouldUsePreferredConstructor() { final ExecutionResult result = graphql.execute("{sample {isGreat isBad}}"); // Then - final HashMap data = (HashMap) result.getData(); + final HashMap data = result.getData(); assertNotNull(data); assertTrue(((HashMap) data.get("sample")).get("isGreat")); assertTrue(((HashMap) data.get("sample")).get("isBad")); @@ -70,7 +64,7 @@ public void shouldUseProvidedSoloArgumentForDataFetcherDeclaredInMethod() { final ExecutionResult result = graphql.execute("{great}"); // Then - final HashMap data = (HashMap) result.getData(); + final HashMap data = result.getData(); assertNotNull(data); assertFalse((Boolean)data.get("great")); } @@ -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 data = (HashMap) result.getData(); + final HashMap data = result.getData(); assertNotNull(data); - assertTrue(((HashMap)data.get("sample")).get("bad")); + assertTrue(((HashMap)data.get("sample")).get("isBad")); } @GraphQLName("Query") diff --git a/src/test/java/graphql/annotations/GraphQLEnumTest.java b/src/test/java/graphql/annotations/GraphQLEnumTest.java index ce3bcb36..ee927e9a 100644 --- a/src/test/java/graphql/annotations/GraphQLEnumTest.java +++ b/src/test/java/graphql/annotations/GraphQLEnumTest.java @@ -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; @@ -72,8 +72,8 @@ 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 @@ -81,8 +81,8 @@ public void testAsInput() throws IllegalAccessException, NoSuchMethodException, 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}}"); } diff --git a/src/test/java/graphql/annotations/GraphQLExtensionsTest.java b/src/test/java/graphql/annotations/GraphQLExtensionsTest.java index b3095c93..4025ac03 100644 --- a/src/test/java/graphql/annotations/GraphQLExtensionsTest.java +++ b/src/test/java/graphql/annotations/GraphQLExtensionsTest.java @@ -23,6 +23,7 @@ import graphql.schema.*; import org.testng.annotations.Test; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -80,7 +81,7 @@ public TestObjectExtensionInvalid(TestObject obj) { } @GraphQLField - public String getField() { + public String field() { return "invalid"; } } @@ -102,7 +103,7 @@ public void fields() { List 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"); @@ -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 data = (Map) result.getData(); + Map data = result.getData(); assertEquals(data.get("field"), "test"); assertEquals(data.get("field2"), "test test2"); assertEquals(data.get("field3"), "test test3"); diff --git a/src/test/java/graphql/annotations/GraphQLFragmentTest.java b/src/test/java/graphql/annotations/GraphQLFragmentTest.java index 42eb107b..6ab5416c 100644 --- a/src/test/java/graphql/annotations/GraphQLFragmentTest.java +++ b/src/test/java/graphql/annotations/GraphQLFragmentTest.java @@ -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; @@ -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 diff --git a/src/test/java/graphql/annotations/GraphQLInputTest.java b/src/test/java/graphql/annotations/GraphQLInputTest.java index ecf73193..a80ac7cb 100644 --- a/src/test/java/graphql/annotations/GraphQLInputTest.java +++ b/src/test/java/graphql/annotations/GraphQLInputTest.java @@ -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; @@ -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"); } @@ -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") @@ -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); } } diff --git a/src/test/java/graphql/annotations/GraphQLObjectTest.java b/src/test/java/graphql/annotations/GraphQLObjectTest.java index b0b21f40..ad7339d7 100644 --- a/src/test/java/graphql/annotations/GraphQLObjectTest.java +++ b/src/test/java/graphql/annotations/GraphQLObjectTest.java @@ -5,7 +5,7 @@ * 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 + * 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, @@ -22,14 +22,12 @@ import graphql.annotations.processor.GraphQLAnnotations; import graphql.annotations.processor.ProcessingElementsContainer; import graphql.annotations.processor.retrievers.GraphQLFieldRetriever; -import graphql.annotations.processor.retrievers.GraphQLObjectHandler; import graphql.annotations.processor.retrievers.GraphQLObjectInfoRetriever; import graphql.annotations.processor.searchAlgorithms.BreadthFirstSearch; import graphql.annotations.processor.searchAlgorithms.ParentalSearch; import graphql.annotations.processor.typeBuilders.InputObjectBuilder; import graphql.annotations.processor.typeFunctions.TypeFunction; import graphql.schema.*; - import graphql.schema.GraphQLType; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.SchemaPrinter; @@ -44,10 +42,7 @@ import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLSchema.newSchema; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; +import static org.testng.Assert.*; @SuppressWarnings("unchecked") public class GraphQLObjectTest { @@ -83,7 +78,7 @@ public String fieldWithArgs(@GraphQLName("a") @GraphQLNonNull String a, @GraphQL } @GraphQLField - public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env,@GraphQLName("a") String a, @GraphQLName("b") String b) { + public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env, @GraphQLName("a") String a, @GraphQLName("b") String b) { return a; } @@ -146,7 +141,7 @@ public static class TestMappedObject { public String aaa; } - public static class TestObjectDB{ + public static class TestObjectDB { public String aaa; private String name; @@ -156,11 +151,12 @@ public String getName() { } public TestObjectDB(String name, String aaa) { - this.name = name; this.aaa=aaa; + this.name = name; + this.aaa = aaa; } } - public static class TestQuery{ + public static class TestQuery { @GraphQLField @GraphQLDataFetcher(ObjectFetcher.class) public TestMappedObject object; @@ -174,15 +170,62 @@ public TestObjectDB get(DataFetchingEnvironment environment) { } } + public static class NameTest { + @GraphQLField + public Boolean isCool; + + @GraphQLField + @GraphQLPrettify + public Boolean isAwesome; + + @GraphQLField + @GraphQLPrettify + @GraphQLName("yarinnn") + public Boolean isYarin; + + @GraphQLField + public String getX() { + return "Asdf0"; + } + + @GraphQLField + @GraphQLPrettify + public String getY() { + return "asd"; + } + + @GraphQLField + @GraphQLPrettify + @GraphQLName("daniel") + public String setM(){ + return "Asdf"; + } + + } + + @Test + public void objectCreation_nameIsCorrect() { + // Act + GraphQLObjectType object = GraphQLAnnotations.object(NameTest.class); + + // Assert + assertNotNull(object.getFieldDefinition("awesome")); + assertNotNull(object.getFieldDefinition("isCool")); + assertNotNull(object.getFieldDefinition("yarinnn")); + assertNotNull(object.getFieldDefinition("getX")); + assertNotNull(object.getFieldDefinition("y")); + assertNotNull(object.getFieldDefinition("daniel")); + } + @Test - public void fetchTestMappedObject_assertNameIsMappedFromDBObject(){ + public void fetchTestMappedObject_assertNameIsMappedFromDBObject() { GraphQLObjectType object = GraphQLAnnotations.object(TestQuery.class); GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{object {name aaa}}"); assertTrue(result.getErrors().isEmpty()); - assertEquals(((LinkedHashMap)(((LinkedHashMap)result.getData()).get("object"))).get("name"), "test"); - assertEquals(((LinkedHashMap)(((LinkedHashMap)result.getData()).get("object"))).get("aaa"), "test"); + assertEquals(((LinkedHashMap) (((LinkedHashMap) result.getData()).get("object"))).get("name"), "test"); + assertEquals(((LinkedHashMap) (((LinkedHashMap) result.getData()).get("object"))).get("aaa"), "test"); } @Test @@ -359,10 +402,10 @@ public void accessors() { GraphQLObjectType object = GraphQLAnnotations.object(TestAccessors.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 2); - fields.sort((o1, o2) -> o1.getName().compareTo(o2.getName())); + fields.sort(Comparator.comparing(GraphQLFieldDefinition::getName)); - assertEquals(fields.get(1).getName(), "value"); - assertEquals(fields.get(0).getName(), "anotherValue"); + assertEquals(fields.get(0).getName(), "getValue"); + assertEquals(fields.get(1).getName(), "setAnotherValue"); } @@ -439,7 +482,7 @@ public void onMethod() { GraphQLObjectType object = GraphQLAnnotations.object(OnMethodTest.class); List fields = object.getFieldDefinitions(); assertEquals(fields.size(), 1); - assertEquals(fields.get(0).getName(), "value"); + assertEquals(fields.get(0).getName(), "getValue"); } public static class TestFetcher implements DataFetcher { @@ -545,7 +588,7 @@ public static class Class2 { @Test public void recursiveTypes() { GraphQLAnnotations graphQLAnnotations = new GraphQLAnnotations(); - GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getObject(Class1.class,graphQLAnnotations.getContainer()); + GraphQLObjectType object = graphQLAnnotations.getObjectHandler().getObject(Class1.class, graphQLAnnotations.getContainer()); GraphQLSchema schema = newSchema().query(object).build(); Class1 class1 = new Class1(); @@ -598,7 +641,7 @@ public void customTypeFunction() { assertEquals(object.getFieldDefinition("id").getType(), GraphQLString); } - public static class TestInputArgument { + public static class TestInputArgument { @GraphQLField public String a; @GraphQLField @@ -619,15 +662,14 @@ public TestComplexInputArgument(@GraphQLName("inputs") Collection getInputs() { + public Collection inputs() { return inputs; } } - - public static class TestObjectInput { + public static class TestObjectInput { @GraphQLField public String test(@GraphQLName("other") int other, @GraphQLName("arg") TestInputArgument arg) { return arg.a; @@ -639,7 +681,7 @@ public String test2(@GraphQLName("other") int other, @GraphQLName("arg") TestCom } } - public static class InputObject{ + public static class InputObject { @GraphQLField int a; @@ -671,7 +713,7 @@ public void complexInputObjectArgument() { GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("{ test2(arg: {inputs:[{ a:\"ok\", b:2 }]}, other:0) }", new TestObjectInput()); assertTrue(result.getErrors().isEmpty()); - Map v = (Map) result.getData(); + Map v = result.getData(); assertEquals(v.get("test2"), "ok"); } @@ -693,7 +735,7 @@ public boolean canBuildType(Class aClass, AnnotatedType annotatedType) { @Override public GraphQLType buildType(boolean input, Class aClass, AnnotatedType annotatedType, ProcessingElementsContainer container) { - return buildType(input,aClass,annotatedType); + return buildType(input, aClass, annotatedType); } @Override @@ -818,12 +860,12 @@ public static class InheritGraphQLFieldTest { @GraphQLField(false) public String forcedOff; - public String getOn() { + public String on() { return "on"; } @GraphQLField(false) - public String getOff() { + public String off() { return "off"; } diff --git a/src/test/java/graphql/annotations/GraphQLUnionTest.java b/src/test/java/graphql/annotations/GraphQLUnionTest.java index 3d43ae04..29e60e29 100644 --- a/src/test/java/graphql/annotations/GraphQLUnionTest.java +++ b/src/test/java/graphql/annotations/GraphQLUnionTest.java @@ -106,10 +106,10 @@ public void unionQueryWithCustomTypeResolver_askForDog_getDog() { GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - String query = "{ pet(kindOfPet:\"dog\"){ ... on Cat {mew}, ... on Dog{waf}} }"; + String query = "{ getPet(kindOfPet:\"dog\"){ ... on Cat {mew}, ... on Dog{waf}} }"; ExecutionResult result = graphQL.execute(query); assertTrue(result.getErrors().isEmpty()); - assertEquals(((Map>) result.getData()).get("pet").get("waf"), "waf"); + assertEquals(((Map>) result.getData()).get("getPet").get("waf"), "waf"); } @Test @@ -117,10 +117,10 @@ public void unionQueryWithCustomTypeResolver_askForCat_getCat() { GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(Query.class)).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - String query = "{ pet(kindOfPet:\"cat\"){ ... on Cat {mew}, ... on Dog{waf}} }"; + String query = "{ getPet(kindOfPet:\"cat\"){ ... on Cat {mew}, ... on Dog{waf}} }"; ExecutionResult result = graphQL.execute(query); assertTrue(result.getErrors().isEmpty()); - assertEquals(((Map>) result.getData()).get("pet").get("mew"), "mew"); + assertEquals(((Map>) result.getData()).get("getPet").get("mew"), "mew"); } static class Screen implements Hardware { diff --git a/src/test/java/graphql/annotations/RelayTest.java b/src/test/java/graphql/annotations/RelayTest.java index 212cd82c..c8c0601d 100644 --- a/src/test/java/graphql/annotations/RelayTest.java +++ b/src/test/java/graphql/annotations/RelayTest.java @@ -110,20 +110,20 @@ public void noArgMutation() { assertTrue(doSomething.getType() instanceof GraphQLObjectType); GraphQLObjectType returnType = (GraphQLObjectType) doSomething.getType(); - assertNotNull(returnType.getFieldDefinition("i")); + assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); - ExecutionResult result = graphQL.execute("mutation { doSomething(input: {clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject()); + ExecutionResult result = graphQL.execute("mutation { doSomething(input: {clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map returns = (Map) ((Map) result.getData()).get("doSomething"); - assertEquals(returns.get("i"), 0); + assertEquals(returns.get("getI"), 0); assertEquals(returns.get("clientMutationId"), "1"); } @@ -139,13 +139,13 @@ public void interfaceReturningMutation() { GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); - ExecutionResult result = graphQL.execute("mutation { doSomethingI(input: {clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject()); + ExecutionResult result = graphQL.execute("mutation { doSomethingI(input: {clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map returns = (Map) ((Map) result.getData()).get("doSomethingI"); - assertEquals(returns.get("i"), 0); + assertEquals(returns.get("getI"), 0); assertEquals(returns.get("clientMutationId"), "1"); } @@ -171,20 +171,20 @@ public void argMutation() { assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType); GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType(); - assertNotNull(returnType.getFieldDefinition("i")); + assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).queryExecutionStrategy(new EnhancedExecutionStrategy()).build(); - ExecutionResult result = graphQL.execute("mutation { doSomethingElse(input: {a: 0, b: 1, clientMutationId: \"1\"}) { i clientMutationId } }", new TestObject()); + ExecutionResult result = graphQL.execute("mutation { doSomethingElse(input: {a: 0, b: 1, clientMutationId: \"1\"}) { getI clientMutationId } }", new TestObject()); assertEquals(result.getErrors().size(), 0); Map returns = (Map) ((Map) result.getData()).get("doSomethingElse"); - assertEquals(returns.get("i"), -1); + assertEquals(returns.get("getI"), -1); assertEquals(returns.get("clientMutationId"), "1"); } @@ -208,7 +208,7 @@ public void argVariableMutation() { assertTrue(doSomethingElse.getType() instanceof GraphQLObjectType); GraphQLObjectType returnType = (GraphQLObjectType) doSomethingElse.getType(); - assertNotNull(returnType.getFieldDefinition("i")); + assertNotNull(returnType.getFieldDefinition("getI")); assertNotNull(returnType.getFieldDefinition("clientMutationId")); GraphQLSchema schema = GraphQLSchema.newSchema().query(object).mutation(object).build(); @@ -221,13 +221,13 @@ public void argVariableMutation() { inputVariables.put("b", 1); inputVariables.put("clientMutationId", "1"); variables.put("input", inputVariables); - ExecutionResult result = graphQL.execute("mutation VariableMutation($input:DoSomethingElseInput!) { doSomethingElse(input: $input) { i clientMutationId } }", new TestObject(), variables); + ExecutionResult result = graphQL.execute("mutation VariableMutation($input:DoSomethingElseInput!) { doSomethingElse(input: $input) { getI clientMutationId } }", new TestObject(), variables); assertEquals(result.getErrors().size(), 0); Map returns = (Map) ((Map) result.getData()).get("doSomethingElse"); - assertEquals(returns.get("i"), -1); + assertEquals(returns.get("getI"), -1); assertEquals(returns.get("clientMutationId"), "1"); } } diff --git a/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java b/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java index c0d1a348..58bafed4 100644 --- a/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java +++ b/src/test/java/graphql/annotations/connection/GraphQLConnectionTest.java @@ -140,29 +140,13 @@ public void methodList() { GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }", + ExecutionResult result = graphQL.execute("{ getObjs(first: 1) { edges { cursor node { id, val } } } }", new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); assertTrue(result.getErrors().isEmpty()); - testResult("objs", result); - - } - - @Test - public void customConnection() { - GraphQLObjectType object = GraphQLAnnotations.object(TestCustomConnection.class); - GraphQLSchema schema = newSchema().query(object).build(); + testResult("getObjs", result); - GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ objs(first: 1) { edges { cursor node { id, val } } } }", - new TestCustomConnection(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); - - assertTrue(result.getErrors().isEmpty()); - Map data = result.getData(); - Map objs = (Map) (data.get("objs")); - List edges = (List) objs.get("edges"); - assertEquals(edges.size(), 0); } @Test @@ -199,12 +183,12 @@ public void methodStream() { GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ objStream(first: 1) { edges { cursor node { id, val } } } }", + ExecutionResult result = graphQL.execute("{ getObjStream(first: 1) { edges { cursor node { id, val } } } }", new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); assertTrue(result.getErrors().isEmpty()); - testResult("objStream", result); + testResult("getObjStream", result); } @Test @@ -213,12 +197,12 @@ public void methodNonNull() { GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ nonNullObjs(first: 1) { edges { cursor node { id, val } } } }", + ExecutionResult result = graphQL.execute("{ getNonNullObjs(first: 1) { edges { cursor node { id, val } } } }", new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); assertTrue(result.getErrors().isEmpty()); - testResult("nonNullObjs", result); + testResult("getNonNullObjs", result); } @Test @@ -227,14 +211,14 @@ public void methodNull() { GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ nullObj(first: 1) { edges { cursor node { id, val } } } }", + ExecutionResult result = graphQL.execute("{ getNullObj(first: 1) { edges { cursor node { id, val } } } }", new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); assertTrue(result.getErrors().isEmpty()); Map>>>> data = result.getData(); - assertEquals(data.get("nullObj").get("edges").size(), 0); + assertEquals(data.get("getNullObj").get("edges").size(), 0); } @Test @@ -243,12 +227,12 @@ public void emptyListData() { GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ objStreamWithParam(first: 1, filter:\"hel\") { edges { cursor node { id, val } } } }", + ExecutionResult result = graphQL.execute("{ getObjStreamWithParam(first: 1, filter:\"hel\") { edges { cursor node { id, val } } } }", new TestConnections(emptyList())); assertTrue(result.getErrors().isEmpty()); Map>>>> data = result.getData(); - List>> edges = data.get("objStreamWithParam").get("edges"); + List>> edges = data.get("getObjStreamWithParam").get("edges"); assertEquals(edges.size(), 0); } @@ -259,13 +243,13 @@ public void methodListWithParam() { GraphQLSchema schema = newSchema().query(object).build(); GraphQL graphQL = GraphQL.newGraphQL(schema).build(); - ExecutionResult result = graphQL.execute("{ objStreamWithParam(first: 2, filter:\"hel\") { edges { cursor node { id, val } } } }", + ExecutionResult result = graphQL.execute("{ getObjStreamWithParam(first: 2, filter:\"hel\") { edges { cursor node { id, val } } } }", new TestConnections(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world"), new Obj("4", "hello world"), new Obj("5", "hello again")))); assertTrue(result.getErrors().isEmpty()); Map>>>> data = result.getData(); - List>> edges = data.get("objStreamWithParam").get("edges"); + List>> edges = data.get("getObjStreamWithParam").get("edges"); assertEquals(edges.size(), 2); assertEquals(edges.get(0).get("node").get("id"), "2"); @@ -305,6 +289,22 @@ public String getCursor(Obj entity) { } } + @Test + public void customConnection() { + GraphQLObjectType object = GraphQLAnnotations.object(TestCustomConnection.class); + GraphQLSchema schema = newSchema().query(object).build(); + + GraphQL graphQL = GraphQL.newGraphQL(schema).build(); + ExecutionResult result = graphQL.execute("{ getObjs(first: 1) { edges { cursor node { id, val } } } }", + new TestCustomConnection(Arrays.asList(new Obj("1", "test"), new Obj("2", "hello"), new Obj("3", "world")))); + + assertTrue(result.getErrors().isEmpty()); + Map data = result.getData(); + Map objs = (Map) (data.get("getObjs")); + List edges = (List) objs.get("edges"); + assertEquals(edges.size(), 0); + } + @Test public void duplicateConnection() { try { diff --git a/src/test/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilderTest.java b/src/test/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilderTest.java index 0beba9fa..99ff51fc 100644 --- a/src/test/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilderTest.java +++ b/src/test/java/graphql/annotations/processor/retrievers/fieldBuilders/method/MethodNameBuilderTest.java @@ -64,7 +64,7 @@ public void build_graphQLNameAnnotationNotExistsWithGetPrefix_returnCorrectName( String name = methodNameBuilder.build(); // assert - assertEquals(name, "test"); + assertEquals(name, "getTest"); } @Test @@ -77,7 +77,7 @@ public void build_graphQLNameAnnotationNotExistsWithIsPrefix_returnCorrectName() String name = methodNameBuilder.build(); // assert - assertEquals(name, "test"); + assertEquals(name, "isTest"); } @Test @@ -90,7 +90,7 @@ public void build_graphQLNameAnnotationNotExistsWithSetPrefix_returnCorrectName( String name = methodNameBuilder.build(); // assert - assertEquals(name, "test"); + assertEquals(name, "setTest"); }