Skip to content

Commit b6bc72d

Browse files
committed
add static handling
1 parent 8cf3440 commit b6bc72d

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public MethodDataFetcher(Method method, TypeFunction typeFunction, ProcessingEle
6868
public T get(DataFetchingEnvironment environment) {
6969
try {
7070
T obj;
71-
if (method.isAnnotationPresent(GraphQLBatched.class) || method.isAnnotationPresent(GraphQLInvokeDetached.class)) {
71+
if (Modifier.isStatic(method.getModifiers())){
72+
return (T) method.invoke(null, invocationArgs(environment, container));
73+
}
74+
else if (method.isAnnotationPresent(GraphQLBatched.class) || method.isAnnotationPresent(GraphQLInvokeDetached.class)) {
7275
obj = newInstance((Class<T>) method.getDeclaringClass());
7376
} else if (!method.getDeclaringClass().isInstance(environment.getSource())) {
7477
obj = newInstance((Class<T>) method.getDeclaringClass(), environment.getSource());

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ public void init() {
4040
GraphQLAnnotations.getInstance().getTypeRegistry().clear();
4141
}
4242

43+
public static class StaticApi {
44+
@GraphQLField
45+
public static String name() {
46+
return "osher";
47+
}
48+
}
49+
50+
@Test
51+
public void query_staticMethod_valueIsDeterminedByMethod(){
52+
GraphQLObjectType object = GraphQLAnnotations.object(StaticApi.class);
53+
GraphQLSchema schema = newSchema().query(object).build();
54+
55+
ExecutionResult result = GraphQL.newGraphQL(schema).build().execute(builder -> builder.query("query { name }").root(new StaticApi()));
56+
assertTrue(result.getErrors().isEmpty());
57+
assertEquals(((Map<String, String>) result.getData()).get("name").toString(), "osher");
58+
}
59+
60+
4361
/**
4462
* CASE 1 : Only Api class, value determined by field
4563
*/

0 commit comments

Comments
 (0)