Class: GraphQL::EnumType

Inherits:
BaseType show all
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 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, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeEnumType

Returns a new instance of EnumType.



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

def initialize
  super
  @values_by_name = {}
end

Instance Attribute Details

#ast_nodeObject



7
8
9
# File 'lib/graphql/enum_type.rb', line 7

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



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

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



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

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



14
15
16
17
# File 'lib/graphql/enum_type.rb', line 14

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

#kindObject



39
40
41
# File 'lib/graphql/enum_type.rb', line 39

def kind
  GraphQL::TypeKinds::ENUM
end

#to_sObject



59
60
61
# File 'lib/graphql/enum_type.rb', line 59

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



35
36
37
# File 'lib/graphql/enum_type.rb', line 35

def values
  @values_by_name
end

#values=(new_values) ⇒ Object

Parameters:

  • new_values (Array<EnumValue>)

    The set of values contained in this type



20
21
22
23
# File 'lib/graphql/enum_type.rb', line 20

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