I inherited a project using GraphQL in Java with Grapqhl-SPQR spring boot starter package (v 0.0.3). It uses annotations to autogenerate the GraphQL SDL, but I need to improve error handling. From what I have found online, I need to implement a union of different types so the user can query multiple types at the same time. Almost all of the online guides discuss this implementation in SDL. I am trying to figure out how to do this with GraphQL SPQR.
So if I have a type
type User {
id: ID!
name: String
}
and another type
type UserError {
message: String
}
Then I can create a union using union UserResult = User | UserError
in SDL so that I can query.
userResult(id: 1) {
... on User {
id
name
}
... on UserError {
message
}
How do I create a union in Graphql-SPQR? I know that there is an @GraphQLUnion
annotation, but I can't find any working examples of its implementation. Does anyone have examples or can point me to examples?