Class: GraphQL::EnumType

Inherits:
BaseType show all
Defined in:
lib/graphql/enum_type.rb

Overview

A finite set of possible values, represented in query strings with SCREAMING_CASE_NAMES

Examples:

An enum of programming languages


LanguageEnum = GraphQL::EnumType.define do
  name "Languages"
  description "Programming languages for Web projects"
  value("PYTHON", "A dynamic, function-oriented language")
  value("RUBY", "A very dynamic language aimed at programmer happiness")
  value("JAVASCRIPT", "Accidental lingua franca of the web")
end

Defined Under Namespace

Classes: EnumValue

Instance Method Summary collapse

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

#definition_proc=, included, #metadata

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeEnumType

Returns a new instance of EnumType.



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

def initialize
  @values_by_name = {}
  @values_by_value = {}
end

Instance Method Details

#add_value(enum_value) ⇒ Object



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

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

Examples:

get episode value from Enum

episode = EpisodeEnum.coerce("NEWHOPE")
episode # => 6

Parameters:

  • value_name (String)

    the string representation of this enum value

Returns:

  • (Object)

    the underlying value for this enum value



61
62
63
64
65
66
67
68
# File 'lib/graphql/enum_type.rb', line 61

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



70
71
72
73
# File 'lib/graphql/enum_type.rb', line 70

def coerce_result(value)
  ensure_defined
  @values_by_value.fetch(value).name
end

#kindObject



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

def kind
  GraphQL::TypeKinds::ENUM
end

#to_sObject



75
76
77
# File 'lib/graphql/enum_type.rb', line 75

def to_s
  name
end

#validate_non_null_input(value_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/graphql/enum_type.rb', line 42

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.dump(value_name)} to be one of: #{@values_by_name.keys.join(', ')}")
  end

  result
end

#valuesObject



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

def values
  ensure_defined
  @values_by_name
end

#values=(new_values) ⇒ Object



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

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