Class: GraphQL::EnumType

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

Overview

Represents a collection of related values. By convention, enum names are SCREAMING_CASE_NAMES, but other identifiers are supported too.

You can use as return types or as inputs.

By default, enums are passed to resolve functions as the strings that identify them, but you can provide a custom Ruby value with the value: keyword.

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

Using an enum as a return type

field :favoriteLanguage, LanguageEnum, "This person's favorite coding language"
# ...
# In a query:
Schema.execute("{ coder(id: 1) { favoriteLanguage } }")
# { "data" => { "coder" => { "favoriteLanguage" => "RUBY" } } }

Defining an enum input

field :coders, types[CoderType] do
  argument :knowing, types[LanguageEnum]
  resolve ->(obj, args, ctx) {
    Coder.where(language: args[:knowing])
  }
end

Using an enum as input

{
  # find coders who know Python and Ruby
  coders(knowing: [PYTHON, RUBY]) {
    name
    hourlyRate
  }
}

Enum whose values are different in Ruby-land

GraphQL::EnumType.define do
  # ...
  # use the `value:` keyword:
  value("RUBY", "Lisp? Smalltalk?", value: :rb)
end

# Now, resolve functions will receive `:rb` instead of `"RUBY"`
field :favoriteLanguage, LanguageEnum
resolve ->(obj, args, ctx) {
  args[:favoriteLanguage] # => :rb
}

Enum whose values are different in ActiveRecord-land

class Language < ActiveRecord::BaseType
  enum language: {
    rb: 0
  }
end

# Now enum type should be defined as
GraphQL::EnumType.define do
  # ...
  # use the `value:` keyword:
  value("RUBY", "Lisp? Smalltalk?", value: 'rb')
end

Defined Under Namespace

Classes: EnumValue, UnresolvedValueError

Instance Attribute Summary

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, #connection_type, #default_relay?, #default_scalar?, #define_connection, #define_edge, #edge_type, #get_field, #introspection?, resolve_related_type, #resolve_type, #to_definition, #to_list_type, #to_non_null_type, #unwrap, #valid_input?, #valid_isolated_input?, #validate_input, #validate_isolated_input

Methods included from Define::InstanceDefinable

#define, #metadata, #redefine

Methods included from Define::NonNullWithBang

#!

Constructor Details

#initializeEnumType

Returns a new instance of EnumType.



78
79
80
81
# File 'lib/graphql/enum_type.rb', line 78

def initialize
  super
  @values_by_name = {}
end

Instance Method Details

#add_value(enum_value) ⇒ Object

Parameters:

  • enum_value (EnumValue)

    A value to add to this type's set of values



95
96
97
98
99
100
101
# File 'lib/graphql/enum_type.rb', line 95

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/graphql/enum_type.rb', line 112

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}")
  end
end

#initialize_copy(other) ⇒ Object



83
84
85
86
# File 'lib/graphql/enum_type.rb', line 83

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

#kindObject



108
109
110
# File 'lib/graphql/enum_type.rb', line 108

def kind
  GraphQL::TypeKinds::ENUM
end

#to_sObject



128
129
130
# File 'lib/graphql/enum_type.rb', line 128

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



104
105
106
# File 'lib/graphql/enum_type.rb', line 104

def values
  @values_by_name
end

#values=(new_values) ⇒ Object

Parameters:

  • new_values (Array<EnumValue>)

    The set of values contained in this type



89
90
91
92
# File 'lib/graphql/enum_type.rb', line 89

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