// Schema mode - union of object types
const TextMessage = ObjectType("TextMessage", {
content: StringType(),
});
const ImageMessage = ObjectType("ImageMessage", {
url: StringType(),
width: IntType(),
height: IntType(),
});
const Message = UnionType("Message", [TextMessage, ImageMessage]);
const api = load(Message);
const msg = { _type: "TextMessage", content: "Hello!" };
Create a discriminated union type from multiple named types or classes.
Unions allow a value to be one of several different types. Each variant
is distinguished by a _type discriminator field. The variant index is
encoded using the minimum bits needed.
Works in both schema mode (with ObjectTypes) and decorator mode (with classes).
// Schema mode - union of object types
const TextMessage = ObjectType("TextMessage", {
content: StringType(),
});
const ImageMessage = ObjectType("ImageMessage", {
url: StringType(),
width: IntType(),
height: IntType(),
});
const Message = UnionType("Message", [TextMessage, ImageMessage]);
const api = load(Message);
const msg = { _type: "TextMessage", content: "Hello!" };
Create a discriminated union type from multiple named types or classes.
Unions allow a value to be one of several different types. Each variant is distinguished by a
_typediscriminator field. The variant index is encoded using the minimum bits needed.Works in both schema mode (with ObjectTypes) and decorator mode (with classes).