Class: GraphQL::EnumType

Inherits:
Object
  • Object
show all
Extended by:
DefinitionHelpers::Definable
Includes:
DefinitionHelpers::NonNullWithBang
Defined in:
lib/graph_ql/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.new do |e|
  e.name("Languages")
  e.descriptions("Programming languages for Web projects")
  e.value("PYTHON", "A dynamic, function-oriented language")
  e.value("RUBY", "A very dynamic language aimed at programmer happiness")
  e.value("JAVASCRIPT", "Accidental lingua franca of the web")
end

Defined Under Namespace

Classes: EnumValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefinitionHelpers::Definable

attr_definable

Methods included from DefinitionHelpers::NonNullWithBang

#!

Constructor Details

#initialize {|_self, GraphQL::DefinitionHelpers::TypeDefiner.instance, GraphQL::DefinitionHelpers::FieldDefiner.instance, GraphQL::DefinitionHelpers::ArgumentDefiner.instance| ... } ⇒ EnumType

Returns a new instance of EnumType.

Yields:

Yield Parameters:



18
19
20
21
22
23
24
25
26
# File 'lib/graph_ql/enum_type.rb', line 18

def initialize
  @values = {}
  yield(
    self,
    GraphQL::DefinitionHelpers::TypeDefiner.instance,
    GraphQL::DefinitionHelpers::FieldDefiner.instance,
    GraphQL::DefinitionHelpers::ArgumentDefiner.instance
  )
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#coerce(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



50
51
52
# File 'lib/graph_ql/enum_type.rb', line 50

def coerce(value_name)
  @values[value_name].value
end

#kindObject



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

def kind
  GraphQL::TypeKinds::ENUM
end

#value(name, description = nil, deprecation_reason: nil, value: name) ⇒ Object

Define a value within this enum

Parameters:

  • name (String)

    the string representation of this value

  • description (String) (defaults to: nil)
  • deprecation_reason (String) (defaults to: nil)

    if provided, ‘deprecated?` will be true

  • value (Object) (defaults to: name)

    the underlying value for this enum value



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

def value(name, description=nil, deprecation_reason: nil, value: name)
  @values[name] = EnumValue.new(name: name, description: description, deprecation_reason: deprecation_reason, value: value)
end