Closed tdraier closed 6 years ago
Hey, we can look into it. The new logic's purpose is to map between a source object to a graphql object, the same way as we do in PropertyDataFetcher. Meaning, if a graphql class has some method which is a GraphQLField, we would be able to map automatically a source object which contains a method with the same name, or a field with the same name.
Why is this necessary? While using interfaces, you define your fields as methods only. So if you implement an interface, you have to override these methods. But there are cases where we would like to treat these methods as properties, and map the source object to them, without creating resolvers.
For instance, if we have this class:
public interface X{
int a();
String b();
}
and you implement it:
public class Y implements X{
@GraphQLField
@Override
public int a(){return null;}
@GraphQLField
@Override
public String b(){return null;}
}
and the source class is:
public class YSource{
int a = 5;
String b = "hello world";
}
I would like to map between the methods and the properties in the source class. This is pretty usefull using GraphQL Interfaces.
Your use case completely makes sense, but I don't think it can conflicts with the case of the static method. Having the isStatic test at the beginning like before should not break this logic :
if (Modifier.isStatic(method.getModifiers())) {
return (T) method.invoke(null, invocationArgs(environment, container));
} else if (method.isAnnotationPresent(GraphQLBatched.class) ||
...
}
ive added this to the pr https://github.com/graphql-java/graphql-java-annotations/pull/179
great, thank you !
I have an issue with changes in #175 - all my fields defined by static method ( which I use on root query fields ) are now broken. The behaviour of MethodDataFetcher is changed and I don't understand the new logic. First the test on isStatic has been removed, so code is now going into the second case, trying to instantiate an instance of my class. This returns null as it's not intended to be built. Next, obj==null, environment.getSource() is the root context, so getGraphQLFieldValue() is called and it throws an exception "No GraphQL field found".
I would expect it to simply call method.invoke(null, invocationArgs(environment, container))
I could fix this by rewriting all code - removing static and adding a constructor with a root context to all classes where it's needed, but it would be good to keep compatibility here ?