class GraphQL::UnionType

@api deprecated

Attributes

resolve_type_proc[RW]
type_membership_class[RW]
type_memberships[R]

Public Class Methods

new() click to toggle source
Calls superclass method GraphQL::BaseType::new
# File lib/graphql/union_type.rb, line 22
def initialize
  super
  @type_membership_class = GraphQL::Schema::TypeMembership
  @type_memberships = []
  @cached_possible_types = nil
  @resolve_type_proc = nil
end

Public Instance Methods

add_possible_types(types, **options) click to toggle source
# File lib/graphql/union_type.rb, line 65
def add_possible_types(types, **options)
  @type_memberships ||= []
  Array(types).each { |t|
    @type_memberships << self.type_membership_class.new(self, t, **options)
  }
  nil
end
get_possible_type(type_name, ctx) click to toggle source

Get a possible type of this {UnionType} by type name @param type_name [String] @param ctx [GraphQL::Query::Context] The context for the current query @return [GraphQL::ObjectType, nil] The type named `type_name` if it exists and is a member of this {UnionType}, (else `nil`)

# File lib/graphql/union_type.rb, line 77
def get_possible_type(type_name, ctx)
  type = ctx.query.get_type(type_name)
  type if type && ctx.query.warden.possible_types(self).include?(type)
end
include?(child_type_defn, ctx = GraphQL::Query::NullContext) click to toggle source

@return [Boolean] True if `child_type_defn` is a member of this {UnionType}

# File lib/graphql/union_type.rb, line 42
def include?(child_type_defn, ctx = GraphQL::Query::NullContext)
  possible_types(ctx).include?(child_type_defn)
end
initialize_copy(other) click to toggle source
Calls superclass method GraphQL::BaseType#initialize_copy
# File lib/graphql/union_type.rb, line 30
def initialize_copy(other)
  super
  @type_membership_class = other.type_membership_class
  @type_memberships = other.type_memberships.dup
  @cached_possible_types = nil
end
kind() click to toggle source
# File lib/graphql/union_type.rb, line 37
def kind
  GraphQL::TypeKinds::UNION
end
possible_type?(type, ctx) click to toggle source

Check if a type is a possible type of this {UnionType} @param type [String, GraphQL::BaseType] Name of the type or a type definition @param ctx [GraphQL::Query::Context] The context for the current query @return [Boolean] True if the `type` exists and is a member of this {UnionType}, (else `nil`)

# File lib/graphql/union_type.rb, line 86
def possible_type?(type, ctx)
  type_name = type.is_a?(String) ? type : type.graphql_name
  !get_possible_type(type_name, ctx).nil?
end
possible_types(ctx = GraphQL::Query::NullContext) click to toggle source

@return [Array<GraphQL::ObjectType>] Types which may be found in this union

# File lib/graphql/union_type.rb, line 47
def possible_types(ctx = GraphQL::Query::NullContext)
  if ctx == GraphQL::Query::NullContext
    # Only cache the default case; if we cached for every `ctx`, it would be a memory leak
    # (The warden should cache calls to this method, so it's called only once per query,
    # unless user code calls it directly.)
    @cached_possible_types ||= possible_types_for_context(ctx)
  else
    possible_types_for_context(ctx)
  end
end
possible_types=(types) click to toggle source
# File lib/graphql/union_type.rb, line 58
def possible_types=(types)
  # This is a re-assignment, so clear the previous values
  @type_memberships = []
  @cached_possible_types = nil
  add_possible_types(types, **{})
end
resolve_type(value, ctx) click to toggle source
# File lib/graphql/union_type.rb, line 91
def resolve_type(value, ctx)
  ctx.query.resolve_type(self, value)
end
resolve_type=(new_resolve_type_proc) click to toggle source
# File lib/graphql/union_type.rb, line 95
def resolve_type=(new_resolve_type_proc)
  @resolve_type_proc = new_resolve_type_proc
end
type_memberships=(type_memberships) click to toggle source
# File lib/graphql/union_type.rb, line 99
def type_memberships=(type_memberships)
  @type_memberships = type_memberships
end

Private Instance Methods

possible_types_for_context(ctx) click to toggle source
# File lib/graphql/union_type.rb, line 105
def possible_types_for_context(ctx)
  visible_types = []
  @type_memberships.each do |type_membership|
    if type_membership.visible?(ctx)
      visible_types << BaseType.resolve_related_type(type_membership.object_type)
    end
  end
  visible_types
end