Skip to content

Support for PropertyDataFetcher behavior #113

Closed
@nickweedon

Description

@nickweedon

When using graphql-java alone and taking the approach of writing DataFetchers, the framework will allow you to return a Map object that contains the property names and values for the corresponding GraphQL type and the framework will then map this to the appropriate GraphQL type using the PropertyDataFetcher (as described in https://graphql-java.readthedocs.io/en/latest/schema.html under DataFetcher and TypeResolver).

It seems that this same behavior is not supported when using graphql-java-tools with the GraphQLResolver approach.

Here is an example:

public class Book {

    private int id;
    private String name;
    private int authorId;

    // constructor and getters/setters ...
}

public class BookResolver implements GraphQLResolver<Book> {

    public Map author(Book book) {

        return Collections.unmodifiableMap(new HashMap<String, Object>() {
            {
                put("id", "1");
                put("name", "smith");
            }
        });
    }
}

The GraphQL schema file is:

type Query {
    books: [Book!]
}

type Book {
    id: Int!
    name: String!
    author: Author!
}

type Author {
    id: Int!
    name: String!
}

When I attempt to build this schema, I get the following exception:

com.coxautodev.graphql.tools.FieldResolverError: No method or field found with any of the following signatures (with or without graphql.schema.DataFetchingEnvironment as the last argument), in priority order:

java.util.Map.id()
java.util.Map.getId()
java.util.Map.id

I also tried changing the return type to Map but then I get this:

com.coxautodev.graphql.tools.TypeClassMatcher$RawClassRequiredForGraphQLMappingException: Type java.util.Map cannot be mapped to a GraphQL type! Since GraphQL-Java deals with erased types at runtime, only non-parameterized classes can represent a GraphQL type. This allows for reverse-lookup by java class in interfaces and union types.

My expectation in this case would be that the map would be resolved to the correct type in the same way that the PropertyDataFetcher used to work. I actually found this to be an extremely valuable feature since it meant that I did not require a POJO for every GraphQL schema type.

I am happy to try to contribute to fixing this issue via PR but I first wanted to check that this is indeed a bug and that I am not missing something here such as another means of achieving this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions