graphql-java-tools icon indicating copy to clipboard operation
graphql-java-tools copied to clipboard

Union causes : Two different classes used for type

Open AdamSoufiane opened this issue 6 years ago • 1 comments

Hello, There is a bug using union with a generic Bean. Here's a schema snipet :

In root file :

union item = Order | User type SearchResponse { total: Int! items: [item] } extend type Query { orders(filter: OrderFilterInput): SearchResponse users(filter: UserFilterInput): SearchResponse }

In java, the bean for SearchResponse is :

@Data @NoArgsConstructor @AllArgsConstructor public class SearchResponse<T> { private Long total; private List<T> items;

}

then the resolvers are :

SearchResponse<User> users(UserFilterInput filters) SearchResponse<Order> orders(OrderFilterInput filters)

So depending on the resolver, SearchReponse could have a List of items of type Order or User. This rises this error:

Caused by: com.coxautodev.graphql.tools.SchemaClassScannerError: Two different classes used for type SearchResponse:

  • class com.api.graphql.common.core.SearchResponse: | return type of method public com.api.graphql.common.core.SearchResponse com.api.graphql.order.OrderResolver.orders(com.api.graphql.order.OrderFilterInput)

  • class com.api.graphql.common.core.SearchResponse: | return type of method public com.api.graphql.common.core.SearchResponse com.api.graphql.users.UserResolver.users(com.api.graphql.users.UserFilterInput)

Can someone help me solve this please?

AdamSoufiane avatar Jan 14 '19 07:01 AdamSoufiane

Hello, This is the hacky solution i've found, it's dirty but it works :

@Bean public SchemaParserDictionary dictionary() { return new SchemaParserDictionary().add(MyModelClassHere.class).add(AnotherMOdelCLass.class); }

all the classes involved in unions have to be added to the dictionnary.

Another possible solution is not to implement GraphQLQueryResolver but rather implement GraphQLResolver<MyModelClassHere> . I didn't test this enough because it breaks my current code, but i'm petty sure it could work.

AdamSoufiane avatar Feb 17 '19 10:02 AdamSoufiane