Union types represent an object that could be one of a list of GraphQL object types. Unlike interfaces, unions don’t define shared fields.Inherits from:Rails::GraphQL::TypeSpecification:GraphQL Union Type Definition
class ActivityUnion < GraphQL::UnionType # Reference types by symbol (must be registered in type map) append :comment_activity_type, :like_activity_typeend
When GraphQL needs to determine which member type a value belongs to, it calls type_for(value). Ensure each member type implements valid_member?(value) correctly:
class UserType < GraphQL::ObjectType self.assigned_to = User # valid_member? automatically checks if value is a User instanceendclass PostType < GraphQL::ObjectType self.assigned_to = Post # valid_member? automatically checks if value is a Post instanceend
All union members must share the same base class. This ensures consistent behavior when resolving union types:
# Valid - all inherit from ActiveRecord::Baseappend UserType, PostType, CommentType# Invalid - mixing different base classesappend UserType, SomeOtherBaseType # ArgumentError!