How to handle generic types in GraphQl-Java?
Asked Answered
S

1

6

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?

Sweeper answered 19/4, 2018 at 9:34 Comment(1)
The error message is very misleading. The limitation doesn't come from graphql-java at all, but from the way the library you're using (seems to be graphql-java-tools) works, and how Java works, to a certain extent. I unfortunately don't know enough about that library to help you, but I can recommend either using graphql-java directly as it now provides schema-first design features out-of-the-box (so much less need for graphql-java-tools) or GraphQL SPQR if you want to map Java to GraphQL types (my project), as it has no issues with generic types.Tantara
S
0

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]!
Sweeper answered 19/10, 2021 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.