Class: GraphQL::EnumType

Inherits:
BaseType show all
Extended by:
Define::InstanceDefinable::DeprecatedDefine
Defined in:
lib/graphql/enum_type.rb

Defined Under Namespace

Classes: EnumValue, UnresolvedValueError

Instance Attribute Summary collapse

Attributes inherited from BaseType

#default_relay, #default_scalar, #description, #introspection, #name

Instance Method Summary collapse

Methods included from Define::InstanceDefinable::DeprecatedDefine

define

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, #type_class, #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

#define, #deprecated_define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeEnumType

Returns a new instance of EnumType.



11
12
13
14
# File 'lib/graphql/enum_type.rb', line 11

def initialize
  super
  @values_by_name = {}
end

Instance Attribute Details

#ast_nodeObject



9
10
11
# File 'lib/graphql/enum_type.rb', line 9

def ast_node
  @ast_node
end

Instance Method Details

#add_value(enum_value) ⇒ Object

Parameters:

  • enum_value (EnumValue)

    A value to add to this type’s set of values



28
29
30
31
32
33
34
# File 'lib/graphql/enum_type.rb', line 28

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/graphql/enum_type.rb', line 45

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



16
17
18
19
# File 'lib/graphql/enum_type.rb', line 16

def initialize_copy(other)
  super
  self.values = other.values.values
end

#kindObject



41
42
43
# File 'lib/graphql/enum_type.rb', line 41

def kind
  GraphQL::TypeKinds::ENUM
end

#to_sObject



61
62
63
# File 'lib/graphql/enum_type.rb', line 61

def to_s
  name
end

#valuesHash<String => EnumValue>

Returns {name => value} pairs contained in this type.

Returns:

  • (Hash<String => EnumValue>)

    {name => value} pairs contained in this type



37
38
39
# File 'lib/graphql/enum_type.rb', line 37

def values
  @values_by_name
end

#values=(new_values) ⇒ Object

Parameters:

  • new_values (Array<EnumValue>)

    The set of values contained in this type



22
23
24
25
# File 'lib/graphql/enum_type.rb', line 22

def values=(new_values)
  @values_by_name = {}
  new_values.each { |enum_value| add_value(enum_value) }
end