Class: GraphQL::Schema::Enum

Inherits:
Member
  • Object
show all
Extended by:
Member::AcceptsDefinition, Member::ValidatesInput
Defined in:
lib/graphql/schema/enum.rb

Defined Under Namespace

Classes: UnresolvedValueError

Constant Summary

Constants included from Member::HasDirectives

Member::HasDirectives::NO_DIRECTIVES

Constants included from Member::GraphQLTypeNames

Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int

Class Method Summary collapse

Methods included from Member::ValidatesInput

coerce_isolated_input, coerce_isolated_result, valid_input?, valid_isolated_input?, validate_input

Methods included from Member::CachedGraphQLDefinition

#graphql_definition, #initialize_copy, #type_class

Methods included from Relay::TypeExtensions

#connection_type, #define_connection, #define_edge, #edge_type

Methods included from Member::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #to_graphql, #visible?

Methods included from Member::BaseDSLMethods::ConfigurationExtension

#inherited

Methods included from Member::TypeSystemHelpers

#kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature

Methods included from Member::Scoped

#scope_items

Methods included from Member::RelayShortcuts

#connection_type, #connection_type_class, #edge_type, #edge_type_class

Methods included from Member::HasPath

#path

Methods included from Member::HasAstNode

#ast_node

Methods included from Member::HasDirectives

#directive, #directives, #remove_directive

Class Method Details

.coerce_input(value_name, ctx) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/graphql/schema/enum.rb', line 107

def coerce_input(value_name, ctx)
  all_values = ctx.warden ? ctx.warden.enum_values(self) : values.each_value

  if v = all_values.find { |val| val.graphql_name == value_name }
    v.value
  elsif v = all_values.find { |val| val.value == value_name }
    # this is for matching default values, which are "inputs", but they're
    # the Ruby value, not the GraphQL string.
    v.value
  else
    nil
  end
end

.coerce_result(value, ctx) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/graphql/schema/enum.rb', line 96

def coerce_result(value, ctx)
  warden = ctx.warden
  all_values = warden ? warden.enum_values(self) : values.each_value
  enum_value = all_values.find { |val| val.value == value }
  if enum_value
    enum_value.graphql_name
  else
    raise(self::UnresolvedValueError, "Can't resolve enum #{graphql_name} for #{value.inspect}")
  end
end

.enum_value_class(new_enum_value_class = nil) ⇒ Class

Returns for handling value(...) inputs and building GraphQL::Enum::EnumValues out of them.

Returns:

  • (Class)

    for handling value(...) inputs and building GraphQL::Enum::EnumValues out of them



69
70
71
72
73
74
75
76
77
# File 'lib/graphql/schema/enum.rb', line 69

def enum_value_class(new_enum_value_class = nil)
  if new_enum_value_class
    @enum_value_class = new_enum_value_class
  elsif defined?(@enum_value_class) && @enum_value_class
    @enum_value_class
  else
    superclass <= GraphQL::Schema::Enum ? superclass.enum_value_class : nil
  end
end

.kindObject



79
80
81
# File 'lib/graphql/schema/enum.rb', line 79

def kind
  GraphQL::TypeKinds::ENUM
end

.to_graphqlGraphQL::EnumType

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/graphql/schema/enum.rb', line 55

def to_graphql
  enum_type = GraphQL::EnumType.new
  enum_type.name = graphql_name
  enum_type.description = description
  enum_type.introspection = introspection
  enum_type.ast_node = ast_node
  values.each do |name, val|
    enum_type.add_value(val.to_graphql)
  end
  enum_type.[:type_class] = self
  enum_type
end

.validate_non_null_input(value_name, ctx) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/graphql/schema/enum.rb', line 83

def validate_non_null_input(value_name, ctx)
  result = GraphQL::Query::InputValidationResult.new

  allowed_values = ctx.warden.enum_values(self)
  matching_value = allowed_values.find { |v| v.graphql_name == value_name }

  if matching_value.nil?
    result.add_problem("Expected #{GraphQL::Language.serialize(value_name)} to be one of: #{allowed_values.map(&:graphql_name).join(', ')}")
  end

  result
end

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

This method returns an undefined value.

Define a value for this enum

Parameters:

  • graphql_name (String, Symbol)

    the GraphQL value for this, usually SCREAMING_CASE

  • description (String)

    , the GraphQL description for this value, present in documentation

  • value (Object)

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

  • deprecation_reason (String)

    if this object is deprecated, include a message here

See Also:

  • which handles these inputs by default


37
38
39
40
41
42
43
44
45
# File 'lib/graphql/schema/enum.rb', line 37

def value(*args, **kwargs, &block)
  kwargs[:owner] = self
  value = enum_value_class.new(*args, **kwargs, &block)
  if own_values.key?(value.graphql_name)
    raise ArgumentError, "#{value.graphql_name} is already defined for #{self.graphql_name}, please remove one of the definitions."
  end
  own_values[value.graphql_name] = value
  nil
end

.valuesHash<String => GraphQL::Schema::Enum::Value>

Returns Possible values of this enum, keyed by name.

Returns:

  • (Hash<String => GraphQL::Schema::Enum::Value>)

    Possible values of this enum, keyed by name



48
49
50
51
52
# File 'lib/graphql/schema/enum.rb', line 48

def values
  inherited_values = superclass <= GraphQL::Schema::Enum ? superclass.values : {}
  # Local values take precedence over inherited ones
  inherited_values.merge(own_values)
end