Class: Rails::GraphQL::Type::Enum

Inherits:
Rails::GraphQL::Type show all
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

DirectiveLocationEnum, TypeKindEnum

Defined Under Namespace

Classes: DirectiveLocationEnum, TypeKindEnum

Constant Summary

Constants inherited from Rails::GraphQL::Type

KINDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#to_gid_param, #to_global_id

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

#valueObject (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. A true 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).

Raises:



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_valuesObject

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

Returns:

  • (Boolean)


35
36
37
# File 'lib/rails/graphql/type/enum.rb', line 35

def indexed?
  defined?(@indexed) && @indexed.present?
end

.inspectObject



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)

Raises:



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

Returns:

  • (Boolean)


201
202
203
# File 'lib/rails/graphql/type/enum.rb', line 201

def deprecated?
  directives&.any?(Directive::DeprecatedDirective) || false
end

#deprecated_reasonObject

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

#descriptionObject

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

#directivesObject

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_iObject

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_symObject

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

Returns:

  • (Boolean)


182
183
184
# File 'lib/rails/graphql/type/enum.rb', line 182

def valid?
  self.class.valid_output?(@value)
end