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
Attributes inherited from BaseType
Instance Method Summary collapse
- #add_value(enum_value) ⇒ Object
-
#coerce_non_null_input(value_name) ⇒ Object
Get the underlying value for this enum value.
- #coerce_result(value, warden = nil) ⇒ Object
-
#initialize ⇒ EnumType
constructor
A new instance of EnumType.
- #initialize_copy(other) ⇒ Object
- #kind ⇒ Object
- #to_s ⇒ Object
- #validate_non_null_input(value_name, warden) ⇒ Object
-
#values ⇒ Hash<String => EnumValue>
{name => value}
pairs contained in this type. - #values=(new_values) ⇒ Object
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, #unwrap, #valid_input?, #validate_input
Methods included from Define::InstanceDefinable
Methods included from Define::NonNullWithBang
Constructor Details
#initialize ⇒ EnumType
Returns a new instance of EnumType.
78 79 80 |
# File 'lib/graphql/enum_type.rb', line 78 def initialize @values_by_name = {} end |
Instance Method Details
#add_value(enum_value) ⇒ Object
95 96 97 |
# File 'lib/graphql/enum_type.rb', line 95 def add_value(enum_value) @values_by_name[enum_value.name] = enum_value end |
#coerce_non_null_input(value_name) ⇒ Object
Get the underlying value for this enum value
128 129 130 131 132 133 134 |
# File 'lib/graphql/enum_type.rb', line 128 def coerce_non_null_input(value_name) if @values_by_name.key?(value_name) @values_by_name.fetch(value_name).value else nil end end |
#coerce_result(value, warden = nil) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/graphql/enum_type.rb', line 136 def coerce_result(value, warden = nil) 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}") end end |
#initialize_copy(other) ⇒ Object
82 83 84 85 |
# File 'lib/graphql/enum_type.rb', line 82 def initialize_copy(other) super self.values = other.values.values end |
#kind ⇒ Object
104 105 106 |
# File 'lib/graphql/enum_type.rb', line 104 def kind GraphQL::TypeKinds::ENUM end |
#to_s ⇒ Object
146 147 148 |
# File 'lib/graphql/enum_type.rb', line 146 def to_s name end |
#validate_non_null_input(value_name, warden) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/graphql/enum_type.rb', line 108 def validate_non_null_input(value_name, warden) result = GraphQL::Query::InputValidationResult.new allowed_values = warden.enum_values(self) matching_value = allowed_values.find { |v| v.name == value_name } if matching_value.nil? result.add_problem("Expected #{GraphQL::Language.serialize(value_name)} to be one of: #{allowed_values.map(&:name).join(', ')}") end result end |
#values ⇒ Hash<String => EnumValue>
Returns {name => value}
pairs contained in this type.
100 101 102 |
# File 'lib/graphql/enum_type.rb', line 100 def values @values_by_name end |
#values=(new_values) ⇒ Object
89 90 91 92 |
# File 'lib/graphql/enum_type.rb', line 89 def values=(new_values) @values_by_name = {} new_values.each { |enum_value| add_value(enum_value) } end |