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 Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseType

#==, #coerce_input, #resolve_type, #to_list_type, #to_non_null_type, #unwrap, #valid_input?

Methods included from DefinitionHelpers::DefinedByConfig

included

Methods included from DefinitionHelpers::NonNullWithBang

#!

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#valuesObject

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#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



56
57
58
# File 'lib/graphql/enum_type.rb', line 56

def coerce_non_null_input(value_name)
  @values_by_name.fetch(value_name).value
end

#coerce_result(value) ⇒ Object



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

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

#kindObject



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

def kind
  GraphQL::TypeKinds::ENUM
end

#to_sObject



64
65
66
# File 'lib/graphql/enum_type.rb', line 64

def to_s
  name
end

#valid_non_null_input?(value_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid_non_null_input?(value_name)
  @values_by_name.key?(value_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



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

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