Enigmatis / graphql-java-annotations

GraphQL Annotations for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NullPointerException in MethodDataFetcher when using methods

alonkashtan opened this issue · comments

I often use DataFetchers that return an object of a different type then the one defining the GraphQL type, but with the same structure. I found a problem with using this technique when the GraphQL type includes a method.

For example:

@GraphQLType
class ApiType {
    @GraphQLField
    public int a() {return 0;}
}

class InternalType{
    public int a;
}

@GraphQLType
class Query{
    @GraphQLField
    @GraphQLDataFetcher(MyFetcher.class)
    public ApiType field;
}

class MyFetcher implements DataFetcher<InternalType>{
    public InternalType get(DataFetchingEnvironment environment){
        return new InternalType();
    }
}

Querying query{ field } will cause a NullPointerException.

This is caused because in MethodDataFetcher there is a search for a constructor that receives the actual type, and if none is found returns null that is not handled later.

Please note that I am sometimes required to use methods instead of fields because we want to use GraphQL interfaces or union types, which require implementing Java interfaces and thous using methods.

The behavior I would expect in this case is that the field value will be copied from the returned objects, as with fields (handled by graphql-java's PropertyDataFetcher).

Fixed in #172