Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I took a stab at defining and adding examples for union types. As unions are very similar to interfaces, I based it heavily on the interface setup. I chose to also leverage a java interface as the union itself, because an interface with no defined fields/methods represents the concept of a union well.
Another approach to this could be to not have a "base class", i.e. interface, at all. Instead, the
@Union
annotation could require thevalue
and then any types annotated with the same union name would become members of that union. For example:would create
Additional Considerations
Mixing polymorphism in GraphQL is not very nice. In the case of unions, a member can only be a
type
. This means if you want an interface to be a part of a union, you must instead add each concrete implementation of the interface to the union. However, you can then query the union with fragments as if it were an interface.Should the spec explicitly define what should happen in this scenario? If someone structures code where a union would contain members that are interfaces, should it be explicitly stated that the implementation must automatically expand the implementations? For example:
should generate
Obviously this example is contrived, but is just to illustrate the scenario. I have a few services with very large schemas (10k+ types) and we have plenty of situations where a union contains one or more interfaces.