Class: GraphQL::EnumType
Overview
Represents a collection of related values.
By convention, enum names are SCREAMING_CASE_NAMES
,
but other identifiers are supported too.
You can use as return types or as inputs.
By default, enums are passed to resolve
functions as
the strings that identify them, but you can provide a
custom Ruby value with the value:
keyword.
Defined Under Namespace
Classes: EnumValue, UnresolvedValueError
Instance Attribute Summary collapse
-
#ast_node ⇒ Object
Returns the value of attribute ast_node.
Attributes inherited from BaseType
#default_relay, #default_scalar, #description, #introspection, #name
Instance Method Summary collapse
-
#add_value(enum_value) ⇒ Object
-
#coerce_result(value, ctx = nil) ⇒ Object
-
#initialize ⇒ EnumType
constructor
A new instance of EnumType.
-
#initialize_copy(other) ⇒ Object
-
#kind ⇒ Object
-
#to_s ⇒ Object
-
#values ⇒ Hash<String => EnumValue>
{name => value}
pairs contained in this type. -
#values=(new_values) ⇒ Object
Methods inherited from BaseType
#==, #coerce_input, #coerce_isolated_input, #coerce_isolated_result, #default_relay?, #default_scalar?, #get_field, #introspection?, #list?, #non_null?, resolve_related_type, #resolve_type, #to_definition, #to_list_type, #to_non_null_type, #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 ⇒ EnumType
Returns a new instance of EnumType
79 80 81 82 |
# File 'lib/graphql/enum_type.rb', line 79 def initialize super @values_by_name = {} end |
Instance Attribute Details
#ast_node ⇒ Object
Returns the value of attribute ast_node
77 78 79 |
# File 'lib/graphql/enum_type.rb', line 77 def ast_node @ast_node end |
Instance Method Details
#add_value(enum_value) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/graphql/enum_type.rb', line 96 def add_value(enum_value) if @values_by_name.key?(enum_value.name) raise "Enum value names must be unique. Value `#{enum_value.name}` already exists on Enum `#{name}`." end @values_by_name[enum_value.name] = enum_value end |
#coerce_result(value, ctx = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/graphql/enum_type.rb', line 113 def coerce_result(value, ctx = nil) if ctx.nil? warn_deprecated_coerce("coerce_isolated_result") ctx = GraphQL::Query::NullContext end warden = ctx.warden all_values = warden ? warden.enum_values(self) : @values_by_name.each_value enum_value = all_values.find { |val| val.value == value } if enum_value enum_value.name else raise(UnresolvedValueError, "Can't resolve enum #{name} for #{value.inspect}") end end |
#initialize_copy(other) ⇒ Object
84 85 86 87 |
# File 'lib/graphql/enum_type.rb', line 84 def initialize_copy(other) super self.values = other.values.values end |
#kind ⇒ Object
109 110 111 |
# File 'lib/graphql/enum_type.rb', line 109 def kind GraphQL::TypeKinds::ENUM end |
#to_s ⇒ Object
129 130 131 |
# File 'lib/graphql/enum_type.rb', line 129 def to_s name end |
#values ⇒ Hash<String => EnumValue>
Returns {name => value}
pairs contained in this type
105 106 107 |
# File 'lib/graphql/enum_type.rb', line 105 def values @values_by_name end |
#values=(new_values) ⇒ Object
90 91 92 93 |
# File 'lib/graphql/enum_type.rb', line 90 def values=(new_values) @values_by_name = {} new_values.each { |enum_value| add_value(enum_value) } end |