Skip to content

Commit cb18256

Browse files
committed
fix bug in methoddatafetcher that name was taken from the method but not from the environment field
1 parent 99e7e91 commit cb18256

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

src/main/java/graphql/annotations/dataFetchers/MethodDataFetcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public T get(DataFetchingEnvironment environment) {
5858
}
5959

6060
if (obj == null && environment.getSource() != null) {
61-
Object value = getFieldValue(environment.getSource(), method.getName());
61+
Object value = getFieldValue(environment.getSource(), environment.getField().getName());
6262
return (T) value;
6363
}
6464

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public MethodNameBuilder(Method method) {
3232
@Override
3333
public String build() {
3434
if (method.isAnnotationPresent(GraphQLPrettify.class) && !method.isAnnotationPresent(GraphQLName.class)) {
35-
return toGraphqlName(pretifyName(method.getName()));
35+
return toGraphqlName(prettifyName(method.getName()));
3636
}
3737
GraphQLName name = method.getAnnotation(GraphQLName.class);
3838
return toGraphqlName(name == null ? method.getName() : name.value());
3939
}
4040

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

src/test/java/graphql/annotations/MethodDataFetcherTest.java

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,9 +17,7 @@
1717
import graphql.ExceptionWhileDataFetching;
1818
import graphql.ExecutionResult;
1919
import graphql.GraphQL;
20-
import graphql.annotations.annotationTypes.GraphQLDataFetcher;
21-
import graphql.annotations.annotationTypes.GraphQLField;
22-
import graphql.annotations.annotationTypes.GraphQLInvokeDetached;
20+
import graphql.annotations.annotationTypes.*;
2321
import graphql.annotations.annotationTypes.GraphQLType;
2422
import graphql.annotations.dataFetchers.MethodDataFetcher;
2523
import graphql.annotations.processor.GraphQLAnnotations;
@@ -68,6 +66,12 @@ public int a() {
6866
return 1;
6967
}
7068

69+
@GraphQLField
70+
@GraphQLPrettify
71+
public int getX() {
72+
return 1;
73+
}
74+
7175
@GraphQLField
7276
@GraphQLInvokeDetached
7377
public int b() {
@@ -78,11 +82,41 @@ public int b() {
7882
public int c() {
7983
return 4;
8084
}
85+
86+
@GraphQLField
87+
@GraphQLPrettify
88+
@GraphQLDataFetcher(CanonizedFetcher.class)
89+
public CanonizedTypeApi getCanonizedType() {
90+
return null;
91+
}
92+
93+
94+
}
95+
96+
public static class CanonizedFetcher implements DataFetcher<CanonizedType> {
97+
98+
@Override
99+
public CanonizedType get(DataFetchingEnvironment environment) {
100+
return new CanonizedType();
101+
}
102+
}
103+
104+
public static class CanonizedTypeApi {
105+
@GraphQLPrettify
106+
@GraphQLField
107+
public int getM() {
108+
return 1;
109+
}
110+
}
111+
112+
public static class CanonizedType {
113+
public int m = 5;
81114
}
82115

83116
public static class InternalType {
84117
public int a = 123;
85118
public int b;
119+
public int x = 5;
86120
}
87121

88122
@GraphQLType
@@ -119,6 +153,27 @@ public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDetermi
119153
assertEquals(((Map<String, Map<String, Integer>>) result.getData()).get("field").get("a").toString(), "123");
120154
}
121155

156+
157+
@Test
158+
public void queryingOneCanonizedFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByEntity() {
159+
GraphQLObjectType object = GraphQLAnnotations.object(Query.class);
160+
GraphQLSchema schema = newSchema().query(object).build();
161+
162+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { canonizedType { m } } }");
163+
assertTrue(result.getErrors().isEmpty());
164+
assertEquals(((Map<String, Map<String, Map<String, Integer>>>) result.getData()).get("field").get("canonizedType").get("m").toString(), "5");
165+
}
166+
167+
@Test
168+
public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetachedAndNameIsPrettified_valueIsDeterminedByEntity() {
169+
GraphQLObjectType object = GraphQLAnnotations.object(Query.class);
170+
GraphQLSchema schema = newSchema().query(object).build();
171+
172+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { x } }");
173+
assertTrue(result.getErrors().isEmpty());
174+
assertEquals(((Map<String, Map<String, Integer>>) result.getData()).get("field").get("x").toString(), "5");
175+
}
176+
122177
@Test
123178
public void queryingOneFieldAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByApiEntity() {
124179
GraphQLObjectType object = GraphQLAnnotations.object(Query.class);
@@ -141,12 +196,12 @@ public void queryingFieldsFromApiEntityFetcher_valueIsDeterminedByApiEntity() {
141196
}
142197

143198
@Test
144-
public void queryingFieldsFromNoApiEntityFetcher_noMatchingFieldInEntity_throwException(){
199+
public void queryingFieldsFromNoApiEntityFetcher_noMatchingFieldInEntity_throwException() {
145200
GraphQLObjectType object = GraphQLAnnotations.object(Query.class);
146201
GraphQLSchema schema = newSchema().query(object).build();
147202

148203
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { c } }");
149204
assertFalse(result.getErrors().isEmpty());
150-
assertTrue(((ExceptionWhileDataFetching)result.getErrors().get(0)).getException().getCause() instanceof NoSuchFieldException);
205+
assertTrue(((ExceptionWhileDataFetching) result.getErrors().get(0)).getException().getCause() instanceof NoSuchFieldException);
151206
}
152207
}

0 commit comments

Comments
 (0)