Class: Rails::GraphQL::Type::Enum
- Inherits:
-
Rails::GraphQL::Type
- Object
- Rails::GraphQL::Type
- Rails::GraphQL::Type::Enum
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/rails/graphql/type/enum.rb
Overview
GraphQL EnumType
Enum types, like scalar types, also represent leaf values in a GraphQL type system. However Enum types describe the set of possible values. See spec.graphql.org/June2018/#EnumTypeDefinition
Direct Known Subclasses
Defined Under Namespace
Classes: DirectiveLocationEnum, TypeKindEnum
Constant Summary
Constants inherited from Rails::GraphQL::Type
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.add(value, desc: nil, description: nil, directives: nil, deprecated: false) ⇒ Object
Use this method to add values to the enum type.
-
.all_deprecated_values ⇒ Object
Build a hash with deprecated values and their respective reason for logging and introspection purposes.
-
.as_json(value) ⇒ Object
Transforms the given value to its representation in a Hash object.
-
.decorate(value) ⇒ Object
Use the instance as decorator.
-
.deserialize(value) ⇒ Object
Turn a user input of this given type into an ruby object.
-
.indexed! ⇒ Object
Mark the enum as indexed, allowing values being set by number.
-
.indexed? ⇒ Boolean
Checks if the enum was marked as indexed.
- .inspect ⇒ Object
-
.to_json(value) ⇒ Object
Transforms the given value to its representation in a JSON string.
-
.valid_input?(value) ⇒ Boolean
Check if a given value is a valid non-deserialized input.
-
.valid_output?(value) ⇒ Boolean
Check if a given value is a valid non-serialized output.
-
.value_using?(value, directive) ⇒ Boolean
Check if a given
value
is using adirective
.
Instance Method Summary collapse
-
#deprecated? ⇒ Boolean
Checks if the current value is marked as deprecated.
-
#deprecated_reason ⇒ Object
Return the deprecated reason.
-
#description ⇒ Object
Gets all the description of the current value.
-
#directives ⇒ Object
Gets all the directives associated with the current value.
-
#initialize(value) ⇒ Enum
constructor
TODO: Maybe add delegate missing.
-
#to_i ⇒ Object
Allow finding the indexed position of the value.
-
#to_sym ⇒ Object
Use lower case for symbolized value.
-
#valid? ⇒ Boolean
Checks if the current value is valid.
Methods inherited from Rails::GraphQL::Type
=~, base_type, create!, find_by_gid, gid_base_class, input_type?, kind, kind_enum, leaf_type?, operational?, output_type?, to_gql_backtrace
Methods included from Helpers::WithDirectives
#all_directive_events, #all_directive_listeners, #directive_events?, #directive_listeners?, extended, included, #initialize_copy, #use, #using?, #validate!
Methods included from Helpers::WithGlobalID
Methods included from Helpers::Registerable
#aliases, extended, #inherited, #register!, #registered?
Constructor Details
#initialize(value) ⇒ Enum
TODO: Maybe add delegate missing
167 168 169 |
# File 'lib/rails/graphql/type/enum.rb', line 167 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
161 162 163 |
# File 'lib/rails/graphql/type/enum.rb', line 161 def value @value end |
Class Method Details
.add(value, desc: nil, description: nil, directives: nil, deprecated: false) ⇒ Object
Use this method to add values to the enum type
Options
-
:desc
- The description of the enum value (defaults to nil). -
:description
- Alias to the above. -
:directives
- The list of directives associated with the value (defaults to nil). -
:deprecated
- A shortcut that auto-attach a @deprecated directive to the value. Atrue
value simple attaches the directive, but provide a string so it can be used as the reason of the deprecation. See DeprecatedDirective (defaults to false).
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rails/graphql/type/enum.rb', line 94 def add(value, desc: nil, description: nil, directives: nil, deprecated: false) value = value&.to_s raise ArgumentError, (+<<~MSG).squish unless value.is_a?(String) && !value.empty? The "#{value}" is invalid. MSG value = value.upcase raise ArgumentError, (+<<~MSG).squish if all_values&.include?(value) The "#{value}" is already defined for #{gql_name} enum. MSG directives = ::Array.wrap(directives) directives << Directive::DeprecatedDirective.new( reason: (deprecated.is_a?(String) ? deprecated : nil), ) if deprecated.present? directives = GraphQL.directives_to_set(directives, location: :enum_value, source: self, ) desc = description if desc.nil? values << value value_description[value] = desc unless desc.nil? value_directives[value] = directives if directives end |
.all_deprecated_values ⇒ Object
Build a hash with deprecated values and their respective reason for logging and introspection purposes
133 134 135 136 137 138 139 140 |
# File 'lib/rails/graphql/type/enum.rb', line 133 def all_deprecated_values @all_deprecated_values ||= begin all_value_directives&.each&.with_object({}) do |(value, dirs), hash| obj = dirs&.find { |dir| dir.is_a?(Directive::DeprecatedDirective) } hash[value] = obj.args.reason || true unless obj.nil? end end.freeze end |
.as_json(value) ⇒ Object
Transforms the given value to its representation in a Hash object
58 59 60 61 62 63 |
# File 'lib/rails/graphql/type/enum.rb', line 58 def as_json(value) return if value.nil? return value.to_s if value.is_a?(self) return all_values.drop(value).first if indexed? && value.is_a?(Numeric) value.to_s.underscore.upcase end |
.decorate(value) ⇒ Object
Use the instance as decorator
77 78 79 |
# File 'lib/rails/graphql/type/enum.rb', line 77 def decorate(value) deserialize(as_json(value)) end |
.deserialize(value) ⇒ Object
Turn a user input of this given type into an ruby object
66 67 68 69 70 71 72 73 74 |
# File 'lib/rails/graphql/type/enum.rb', line 66 def deserialize(value) if valid_token?(value, :enum) new(value.to_s) elsif allow_string_input? && valid_token?(value, :string) new(value[1..-2]) elsif valid_input?(value) new(value) end end |
.indexed! ⇒ Object
Mark the enum as indexed, allowing values being set by number
30 31 32 |
# File 'lib/rails/graphql/type/enum.rb', line 30 def indexed! @indexed = true end |
.indexed? ⇒ Boolean
Checks if the enum was marked as indexed
35 36 37 |
# File 'lib/rails/graphql/type/enum.rb', line 35 def indexed? defined?(@indexed) && @indexed.present? end |
.inspect ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rails/graphql/type/enum.rb', line 142 def inspect return super if self.eql?(Type::Enum) values = all_values.to_a (+<<~INFO).squish << '>' #<GraphQL::Enum #{gql_name} (#{values.size}) {#{values.to_a.join(' | ')}} #{inspect_directives} INFO end |
.to_json(value) ⇒ Object
Transforms the given value to its representation in a JSON string
53 54 55 |
# File 'lib/rails/graphql/type/enum.rb', line 53 def to_json(value) as_json(value)&.inspect end |
.valid_input?(value) ⇒ Boolean
Check if a given value is a valid non-deserialized input
40 41 42 43 44 45 |
# File 'lib/rails/graphql/type/enum.rb', line 40 def valid_input?(value) (valid_token?(value, :enum) && all_values.include?(value.to_s)) || (value.is_a?(String) && all_values.include?(value)) || (allow_string_input? && valid_token?(value, :string) && all_values.include?(value.to_s[1..-2])) end |
.valid_output?(value) ⇒ Boolean
Check if a given value is a valid non-serialized output
48 49 50 |
# File 'lib/rails/graphql/type/enum.rb', line 48 def valid_output?(value) all_values.include?(as_json(value)) end |
.value_using?(value, directive) ⇒ Boolean
Check if a given value
is using a directive
123 124 125 126 127 128 129 |
# File 'lib/rails/graphql/type/enum.rb', line 123 def value_using?(value, directive) raise ArgumentError, (+<<~MSG).squish unless directive < GraphQL::Directive The provided #{item_or_symbol.inspect} is not a valid directive. MSG all_value_directives.try(:[], as_json(value))&.any?(directive) || false end |
Instance Method Details
#deprecated? ⇒ Boolean
Checks if the current value is marked as deprecated
201 202 203 |
# File 'lib/rails/graphql/type/enum.rb', line 201 def deprecated? directives&.any?(Directive::DeprecatedDirective) || false end |
#deprecated_reason ⇒ Object
Return the deprecated reason
206 207 208 209 210 |
# File 'lib/rails/graphql/type/enum.rb', line 206 def deprecated_reason directives&.find do |dir| dir.is_a?(Directive::DeprecatedDirective) end&.args&.reason end |
#description ⇒ Object
Gets all the description of the current value
187 188 189 190 191 |
# File 'lib/rails/graphql/type/enum.rb', line 187 def description return unless @value return @description if defined?(@description) @description = all_value_description.try(:[], @value) end |
#directives ⇒ Object
Gets all the directives associated with the current value
194 195 196 197 198 |
# File 'lib/rails/graphql/type/enum.rb', line 194 def directives return unless @value return @directives if defined?(@directives) @directives = all_value_directives.try(:[], @value) end |
#to_i ⇒ Object
Allow finding the indexed position of the value
177 178 179 |
# File 'lib/rails/graphql/type/enum.rb', line 177 def to_i values.find_index(@value) end |
#to_sym ⇒ Object
Use lower case for symbolized value
172 173 174 |
# File 'lib/rails/graphql/type/enum.rb', line 172 def to_sym @value.downcase.to_sym end |
#valid? ⇒ Boolean
Checks if the current value is valid
182 183 184 |
# File 'lib/rails/graphql/type/enum.rb', line 182 def valid? self.class.valid_output?(@value) end |