graphql-java-tools
graphql-java-tools copied to clipboard
Interface Using a Union Fails when Implemented
Currently using graphql-spring-boot 5.10.0
I have an interface that defines two properties. Both properties are defined with a type that is a union, this is done so that it can be implemented and overridden with a concrete type that is one of the union types defined in the schema. Here is a simplified version of the schema to illustrate this structure in GQL.
union SortResponse = NodeSortResponse
union Result = Node
interface SearchResponse {
results: [Result!]!
order: SortResponse
}
type Node {
id: ID!
title: String
}
type NodeSortResponse {
field: NodeSortField
direction: SortDirection
}
type NodeSearchResponse implements SearchResponse {
results: [Node!]!
order: NodeSortResponse
}
The exception is thrown in SchemaParser.kt on line 349. It fails the conditional that it is an acceptable output type. The property for unionDefinitions doesn't have the union in its list.
The unions you defined here don't actually seem to be doing a union though? So I'd expect something like union Result = Node | OtherNode. That might be why it's not added to the unionDefinitions. This seems to be a graphql-java-tools issue though, so I'll transfer this issue to that project.
I think we do not properly handle the case where a union only has a single type. I think we should just throw an error as it does not seem to be a valid grammar.
https://graphql.org/learn/schema/#union-types
You are looking at an example, not the spec:
"A Union type must include one or more unique member types."
Having a union with a single type is useful because you can also extend unions.