Skip to content

Commit 48d7bc3

Browse files
committed
add test fix batched
1 parent d0895c4 commit 48d7bc3

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package graphql.annotations.dataFetchers;
1616

17+
import graphql.annotations.annotationTypes.GraphQLBatched;
1718
import graphql.annotations.annotationTypes.GraphQLInvokeDetached;
1819
import graphql.annotations.annotationTypes.GraphQLName;
1920
import graphql.annotations.processor.ProcessingElementsContainer;
@@ -45,10 +46,7 @@ public MethodDataFetcher(Method method, TypeFunction typeFunction, ProcessingEle
4546
public T get(DataFetchingEnvironment environment) {
4647
try {
4748
T obj;
48-
49-
if (Modifier.isStatic(method.getModifiers())) {
50-
obj = null;
51-
} else if (method.getAnnotation(GraphQLInvokeDetached.class) != null) {
49+
if (method.isAnnotationPresent(GraphQLBatched.class) || method.isAnnotationPresent(GraphQLInvokeDetached.class)) {
5250
obj = newInstance((Class<T>) method.getDeclaringClass());
5351
} else if (!method.getDeclaringClass().isInstance(environment.getSource())) {
5452
obj = newInstance((Class<T>) method.getDeclaringClass(), environment.getSource());
@@ -61,11 +59,11 @@ public T get(DataFetchingEnvironment environment) {
6159

6260
if (obj == null && environment.getSource() != null) {
6361
Object value = getFieldValue(environment.getSource(), method.getName());
64-
if (value != null) return (T) value;
62+
return (T) value;
6563
}
6664

6765
return (T) method.invoke(obj, invocationArgs(environment, container));
68-
} catch (IllegalAccessException | InvocationTargetException e) {
66+
} catch (IllegalAccessException | InvocationTargetException | NoSuchFieldException e) {
6967
throw new RuntimeException(e);
7068
}
7169
}
@@ -137,13 +135,9 @@ private Object buildArg(Type p, GraphQLType graphQLType, Object arg) {
137135
}
138136
}
139137

140-
private Object getFieldValue(Object source, String fieldName) throws IllegalAccessException {
141-
try {
142-
Field field = source.getClass().getDeclaredField(fieldName);
143-
field.setAccessible(true);
144-
return field.get(source);
145-
} catch (NoSuchFieldException e) {
146-
return null;
147-
}
138+
private Object getFieldValue(Object source, String fieldName) throws IllegalAccessException, NoSuchFieldException {
139+
Field field = source.getClass().getDeclaredField(fieldName);
140+
field.setAccessible(true);
141+
return field.get(source);
148142
}
149143
}

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package graphql.annotations;
1616

17+
import graphql.ExceptionWhileDataFetching;
1718
import graphql.ExecutionResult;
1819
import graphql.GraphQL;
1920
import graphql.annotations.annotationTypes.GraphQLDataFetcher;
@@ -31,8 +32,7 @@
3132
import java.util.Map;
3233

3334
import static graphql.schema.GraphQLSchema.newSchema;
34-
import static org.testng.Assert.assertEquals;
35-
import static org.testng.Assert.assertTrue;
35+
import static org.testng.Assert.*;
3636

3737
@SuppressWarnings("unchecked")
3838
public class MethodDataFetcherTest {
@@ -73,6 +73,11 @@ public int a() {
7373
public int b() {
7474
return 2;
7575
}
76+
77+
@GraphQLField
78+
public int c() {
79+
return 4;
80+
}
7681
}
7782

7883
public static class InternalType {
@@ -103,6 +108,7 @@ public ApiType get(DataFetchingEnvironment environment) {
103108
}
104109
}
105110

111+
106112
@Test
107113
public void queryingOneFieldNotAnnotatedWithGraphQLInvokeDetached_valueIsDeterminedByEntity() {
108114
GraphQLObjectType object = GraphQLAnnotations.object(Query.class);
@@ -133,4 +139,14 @@ public void queryingFieldsFromApiEntityFetcher_valueIsDeterminedByApiEntity() {
133139
assertEquals(((Map<String, Map<String, Integer>>) result.getData()).get("apiField").get("a").toString(), "1");
134140
assertEquals(((Map<String, Map<String, Integer>>) result.getData()).get("apiField").get("b").toString(), "2");
135141
}
142+
143+
@Test
144+
public void queryingFieldsFromNoApiEntityFetcher_noMatchingFieldInEntity_throwException(){
145+
GraphQLObjectType object = GraphQLAnnotations.object(Query.class);
146+
GraphQLSchema schema = newSchema().query(object).build();
147+
148+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { field { c } }");
149+
assertFalse(result.getErrors().isEmpty());
150+
assertTrue(((ExceptionWhileDataFetching)result.getErrors().get(0)).getException().getCause() instanceof NoSuchFieldException);
151+
}
136152
}

0 commit comments

Comments
 (0)