Unions

A GraphQL union represents a set of object types that share no required common fields. Unlike interfaces, union members do not need to declare the same fields. Clients use inline fragments to select fields from each possible type.

GraphQL schema

GraphQL
type TextContent {
text: String!
}
type ImageContent {
imageUrl: String!
height: Int!
}
union PostContent = TextContent | ImageContent

Client query

GraphQL
{
content {
... on TextContent {
text
}
... on ImageContent {
imageUrl
}
}
}

Defining a Union Type

Use a marker interface (an interface with no members) or an abstract class to group the types that belong to the union.

C#
// Types/IPostContent.cs
[UnionType("PostContent")]
public interface IPostContent
{
}
// Types/TextContent.cs
public class TextContent : IPostContent
{
public string Text { get; set; }
}
// Types/ImageContent.cs
public class ImageContent : IPostContent
{
public string ImageUrl { get; set; }
public int Height { get; set; }
}
// Types/ContentQueries.cs
[QueryType]
public static partial class ContentQueries
{
public static IPostContent GetContent()
{
// ...
}
}
C#
// Program.cs
builder
.AddGraphQL()
.AddType<TextContent>()
.AddType<ImageContent>();

Each type that implements the marker interface must be registered so Hot Chocolate includes it in the union.

Union vs Interface

Choose a union when the grouped types have no meaningful shared fields. Choose an interface when you want to guarantee a common set of fields across all implementing types.

FeatureUnionInterface
Common fields requiredNoYes
Query shared fields directlyNoYes
Types can belong to multipleYesYes

Next Steps

Last updated on April 13, 2026 by Michael Staib