Method: GraphQL::Schema::Enum.value

Defined in:
lib/graphql/schema/enum.rb

.value(*args, value_method: nil, **kwargs, &block) ⇒ void

This method returns an undefined value.

Define a value for this enum

Parameters:

  • value_method (Symbol, false) (defaults to: nil)

    A method to generate for this value, or false to skip generation

  • kwargs (Hash)

    a customizable set of options

Options Hash (**kwargs):

  • :graphql_name (String, Symbol)

    the GraphQL value for this, usually SCREAMING_CASE

  • :description, (String)

    the GraphQL description for this value, present in documentation

  • :comment, (String)

    the GraphQL comment for this value, present in documentation

  • :value (::Object)

    the translated Ruby value for this object (defaults to graphql_name)

  • :value_method, (::Object)

    the method name to fetch graphql_name (defaults to graphql_name.downcase)

  • :deprecation_reason (String)

    if this object is deprecated, include a message here

See Also:

  • which handles these inputs by default


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/graphql/schema/enum.rb', line 69

def value(*args, value_method: nil, **kwargs, &block)
  kwargs[:owner] = self
  value = enum_value_class.new(*args, **kwargs, &block)

  if value_method || (value_methods && value_method != false)
    generate_value_method(value, value_method)
  end

  key = value.graphql_name
  prev_value = own_values[key]
  case prev_value
  when nil
    own_values[key] = value
  when GraphQL::Schema::EnumValue
    own_values[key] = [prev_value, value]
  when Array
    prev_value << value
  else
    raise "Invariant: Unexpected enum value for #{key.inspect}: #{prev_value.inspect}"
  end
  value
end