Class: GraphQL::EnumType

Inherits:
Object
  • Object
show all
Includes:
DefinitionHelpers::DefinedByConfig, DefinitionHelpers::NonNullWithBang
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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from DefinitionHelpers::DefinedByConfig

included

Methods included from DefinitionHelpers::NonNullWithBang

#!

Instance Attribute Details

#descriptionObject

Returns the value of attribute description


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

def description
  @description
end

#nameObject

Returns the value of attribute name


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

def name
  @name
end

#valuesObject

Returns the value of attribute values


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

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


45
46
47
# File 'lib/graphql/enum_type.rb', line 45

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

#kindObject


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

def kind
  GraphQL::TypeKinds::ENUM
end

#to_sObject


49
50
51
# File 'lib/graphql/enum_type.rb', line 49

def to_s
  name
end

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

Deprecated.

use define API instead

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


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

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