Skip to content

Union causes : Two different classes used for type #221

Closed as duplicate

Description

@AdamSoufiane

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 {
private Long total;
private List items;

}

then the resolvers are :

SearchResponse users(UserFilterInput filters)
SearchResponse 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?

Activity

  1. AdamSoufiane commented on Feb 17, 2019

    @AdamSoufiane
    Author

    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.

  2. oryan-block commented on Oct 3, 2026

    @oryan-block
    Collaborator

    Closing as a duplicate of #343. One GraphQL type cannot be backed by several parameterizations of a generic class. Use a shared type argument (SearchResponse<Any> or a common supertype) and add the union members to the dictionary. The broader request is tracked in #111.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions