Class: GraphQL::UnionType

Inherits:
BaseType show all
Includes:
BaseType::HasPossibleTypes
Defined in:
lib/graphql/union_type.rb

Overview

A Union is is a collection of object types which may appear in the same place.

The members of a union are declared with ‘possible_types`.

A union itself has no fields; only its members have fields. So, when you query, you must use fragment spreads to access fields.

Examples:

A union of object types

MediaUnion = GraphQL::UnionType.define do
  name "Media"
  description "Media objects which you can enjoy"
  possible_types [AudioType, ImageType, VideoType]
end

Querying for fields on union members

{
  searchMedia(name: "Jens Lekman") {
    ... on Audio { name, duration }
    ... on Image { name, height, width }
    ... on Video { name, length, quality }
  }
}

Constant Summary

Constants included from BaseType::HasPossibleTypes

BaseType::HasPossibleTypes::DEFAULT_RESOLVE_TYPE

Instance Method Summary collapse

Methods included from BaseType::HasPossibleTypes

#legacy_resolve_type, #resolve_type, #resolve_type=, #warn_resolve_type_deprecated

Methods inherited from BaseType

#==, #coerce_input, #connection_type, #define_connection, #define_edge, #edge_type, #get_field, resolve_related_type, #resolve_type, #to_list_type, #to_non_null_type, #to_s, #unwrap, #valid_input?, #validate_input

Methods included from Define::InstanceDefinable

#definition_proc=, included, #metadata

Methods included from Define::NonNullWithBang

#!

Instance Method Details

#include?(child_type_defn) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/graphql/union_type.rb', line 33

def include?(child_type_defn)
  possible_types.include?(child_type_defn)
end

#kindObject



29
30
31
# File 'lib/graphql/union_type.rb', line 29

def kind
  GraphQL::TypeKinds::UNION
end

#possible_typesObject



42
43
44
45
46
47
48
49
# File 'lib/graphql/union_type.rb', line 42

def possible_types
  @clean_possible_types ||= begin
    ensure_defined
    @dirty_possible_types.map { |type| GraphQL::BaseType.resolve_related_type(type) }
  rescue
    @dirty_possible_types
  end
end

#possible_types=(new_possible_types) ⇒ Object



37
38
39
40
# File 'lib/graphql/union_type.rb', line 37

def possible_types=(new_possible_types)
  @clean_possible_types = nil
  @dirty_possible_types = new_possible_types
end