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
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) ⇒ Object
-
#initialize ⇒ EnumType
constructor
A new instance of EnumType.
- #kind ⇒ Object
- #to_s ⇒ Object
- #validate_non_null_input(value_name) ⇒ 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
#define, #definition_proc=, #metadata
Methods included from Define::NonNullWithBang
Constructor Details
#initialize ⇒ EnumType
Returns a new instance of EnumType.
61 62 63 64 |
# File 'lib/graphql/enum_type.rb', line 61 def initialize @values_by_name = {} @values_by_value = {} end |
Instance Method Details
#add_value(enum_value) ⇒ Object
74 75 76 77 |
# File 'lib/graphql/enum_type.rb', line 74 def add_value(enum_value) @values_by_name[enum_value.name] = enum_value @values_by_value[enum_value.value] = enum_value end |
#coerce_non_null_input(value_name) ⇒ Object
Get the underlying value for this enum value
108 109 110 111 112 113 114 115 |
# File 'lib/graphql/enum_type.rb', line 108 def coerce_non_null_input(value_name) ensure_defined if @values_by_name.key?(value_name) @values_by_name.fetch(value_name).value else nil end end |
#coerce_result(value) ⇒ Object
117 118 119 120 |
# File 'lib/graphql/enum_type.rb', line 117 def coerce_result(value) ensure_defined @values_by_value.fetch(value).name end |
#kind ⇒ Object
85 86 87 |
# File 'lib/graphql/enum_type.rb', line 85 def kind GraphQL::TypeKinds::ENUM end |
#to_s ⇒ Object
122 123 124 |
# File 'lib/graphql/enum_type.rb', line 122 def to_s name end |
#validate_non_null_input(value_name) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/graphql/enum_type.rb', line 89 def validate_non_null_input(value_name) ensure_defined result = GraphQL::Query::InputValidationResult.new if !@values_by_name.key?(value_name) result.add_problem("Expected #{JSON.generate(value_name, quirks_mode: true)} to be one of: #{@values_by_name.keys.join(', ')}") end result end |
#values ⇒ Hash<String => EnumValue>
Returns {name => value}
pairs contained in this type.
80 81 82 83 |
# File 'lib/graphql/enum_type.rb', line 80 def values ensure_defined @values_by_name end |
#values=(new_values) ⇒ Object
67 68 69 70 71 |
# File 'lib/graphql/enum_type.rb', line 67 def values=(new_values) @values_by_name = {} @values_by_value = {} new_values.each { |enum_value| add_value(enum_value) } end |