Search code examples
javagraphql-java

How to handle generic types in GraphQl-Java?


I try to convert our model into a GraphQL schema using the following resolver:

@Component
public class QueryResolver implements GraphQLQueryResolver {

    public List<Result> results;
}

But I get the following error:

Type java.util.List 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.

How may I get around this problem, since I need to use generic types?


Solution

  • If you get that error above, check your schema files, if the return types of your fields are correct! In my case it was just caused by missing brackets:

    Change:

    list(page: Int = 0, size: Int = 10): Result!
    

    To:

    list(page: Int = 0, size: Int = 10): [Result]!