Class: GraphQL::UnionType
- 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.
Instance Attribute Summary collapse
-
#resolve_type_proc ⇒ Object
Returns the value of attribute resolve_type_proc.
-
#type_membership_class ⇒ Object
Returns the value of attribute type_membership_class.
-
#type_memberships ⇒ Object
Returns the value of attribute type_memberships.
Attributes inherited from BaseType
#ast_node, #default_relay, #default_scalar, #description, #introspection, #name
Instance Method Summary collapse
-
#add_possible_types(types, options) ⇒ Object
-
#get_possible_type(type_name, ctx) ⇒ GraphQL::ObjectType?
Get a possible type of this UnionType by type name.
-
#include?(child_type_defn, ctx = GraphQL::Query::NullContext) ⇒ Boolean
True if
child_type_defn
is a member of this UnionType. -
#initialize ⇒ UnionType
constructor
A new instance of UnionType.
-
#initialize_copy(other) ⇒ Object
-
#kind ⇒ Object
-
#possible_type?(type, ctx) ⇒ Boolean
Check if a type is a possible type of this UnionType.
-
#possible_types(ctx = GraphQL::Query::NullContext) ⇒ Array<GraphQL::ObjectType>
Types which may be found in this union.
-
#possible_types=(types) ⇒ Object
-
#resolve_type(value, ctx) ⇒ Object
-
#resolve_type=(new_resolve_type_proc) ⇒ Object
Methods inherited from BaseType
#==, #coerce_input, #coerce_isolated_input, #coerce_isolated_result, #coerce_result, #default_relay?, #default_scalar?, #get_field, #introspection?, #list?, #non_null?, resolve_related_type, #to_definition, #to_list_type, #to_non_null_type, #to_s, #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
Methods included from Define::NonNullWithBang
Constructor Details
#initialize ⇒ UnionType
Returns a new instance of UnionType
35 36 37 38 39 40 41 |
# File 'lib/graphql/union_type.rb', line 35 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_proc ⇒ Object
Returns the value of attribute resolve_type_proc
31 32 33 |
# File 'lib/graphql/union_type.rb', line 31 def resolve_type_proc @resolve_type_proc end |
#type_membership_class ⇒ Object
Returns the value of attribute type_membership_class
33 34 35 |
# File 'lib/graphql/union_type.rb', line 33 def type_membership_class @type_membership_class end |
#type_memberships ⇒ Object
Returns the value of attribute type_memberships
32 33 34 |
# File 'lib/graphql/union_type.rb', line 32 def type_memberships @type_memberships end |
Instance Method Details
#add_possible_types(types, options) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/graphql/union_type.rb', line 78 def add_possible_types(types, ) @type_memberships ||= [] Array(types).each { |t| @type_memberships << self.type_membership_class.new(self, t, ) } nil end |
#get_possible_type(type_name, ctx) ⇒ GraphQL::ObjectType?
Get a possible type of this GraphQL::UnionType by type name
90 91 92 93 |
# File 'lib/graphql/union_type.rb', line 90 def get_possible_type(type_name, ctx) type = ctx.query.get_type(type_name) type if type && ctx.query.schema.possible_types(self, ctx).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
55 56 57 |
# File 'lib/graphql/union_type.rb', line 55 def include?(child_type_defn, ctx = GraphQL::Query::NullContext) possible_types(ctx).include?(child_type_defn) end |
#initialize_copy(other) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/graphql/union_type.rb', line 43 def initialize_copy(other) super @type_membership_class = other.type_membership_class @type_memberships = other.type_memberships.dup @cached_possible_types = nil end |
#kind ⇒ Object
50 51 52 |
# File 'lib/graphql/union_type.rb', line 50 def kind GraphQL::TypeKinds::UNION end |
#possible_type?(type, ctx) ⇒ Boolean
Check if a type is a possible type of this GraphQL::UnionType
99 100 101 102 |
# File 'lib/graphql/union_type.rb', line 99 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
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/graphql/union_type.rb', line 60 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
71 72 73 74 75 76 |
# File 'lib/graphql/union_type.rb', line 71 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
104 105 106 |
# File 'lib/graphql/union_type.rb', line 104 def resolve_type(value, ctx) ctx.query.resolve_type(self, value) end |
#resolve_type=(new_resolve_type_proc) ⇒ Object
108 109 110 |
# File 'lib/graphql/union_type.rb', line 108 def resolve_type=(new_resolve_type_proc) @resolve_type_proc = new_resolve_type_proc end |