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

Interface Using a Union Fails when Implemented

Open MFoster opened this issue 6 years ago • 3 comments

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.

MFoster avatar Sep 11 '19 21:09 MFoster

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.

oliemansm avatar Apr 04 '20 06:04 oliemansm

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

vojtapol avatar Apr 16 '20 14:04 vojtapol

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.

lqc avatar Mar 14 '21 22:03 lqc