Class: GraphQL::UnionType

Inherits:
BaseType show all
Extended by:
Define::InstanceDefinable::DeprecatedDefine
Defined in:
lib/graphql/union_type.rb

Defined Under Namespace

Classes: AcceptPossibleTypesDefinition

Instance Attribute Summary collapse

Attributes inherited from BaseType

#ast_node, #default_relay, #default_scalar, #description, #introspection, #name

Instance Method Summary collapse

Methods included from Define::InstanceDefinable::DeprecatedDefine

define

Methods inherited from BaseType

#==, #coerce_input, #coerce_isolated_input, #coerce_isolated_result, #coerce_result, #default_relay?, #default_scalar?, #get_field, #graphql_definition, #introspection?, #list?, #non_null?, resolve_related_type, #to_definition, #to_list_type, #to_non_null_type, #to_s, #type_class, #unwrap, #valid_input?, #valid_isolated_input?, #validate_input, #validate_isolated_input

Methods included from Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Define::InstanceDefinable

#define, #deprecated_define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeUnionType

Returns a new instance of UnionType.


22
23
24
25
26
27
28
# 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

Instance Attribute Details

#resolve_type_procObject


18
19
20
# File 'lib/graphql/union_type.rb', line 18

def resolve_type_proc
  @resolve_type_proc
end

#type_membership_classObject


20
21
22
# File 'lib/graphql/union_type.rb', line 20

def type_membership_class
  @type_membership_class
end

#type_membershipsObject


19
20
21
# File 'lib/graphql/union_type.rb', line 19

def type_memberships
  @type_memberships
end

Instance Method Details

#add_possible_types(types, **options) ⇒ Object


65
66
67
68
69
70
71
# 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) ⇒ GraphQL::ObjectType?

Get a possible type of this GraphQL::UnionType by type name

Parameters:

Returns:


77
78
79
80
# 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) ⇒ Boolean

Returns True if child_type_defn is a member of this GraphQL::UnionType.

Returns:


42
43
44
# 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) ⇒ Object


30
31
32
33
34
35
# 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

#kindObject


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

def kind
  GraphQL::TypeKinds::UNION
end

#possible_type?(type, ctx) ⇒ Boolean

Check if a type is a possible type of this GraphQL::UnionType

Parameters:

Returns:

  • (Boolean)

    True if the type exists and is a member of this GraphQL::UnionType, (else nil)


86
87
88
89
# 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) ⇒ Array<GraphQL::ObjectType>

Returns Types which may be found in this union.

Returns:


47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object


58
59
60
61
62
63
# 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) ⇒ Object


91
92
93
# 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) ⇒ Object


95
96
97
# File 'lib/graphql/union_type.rb', line 95

def resolve_type=(new_resolve_type_proc)
  @resolve_type_proc = new_resolve_type_proc
end