Class: GraphQL::Schema::Enum

Inherits:
Member
  • Object
show all
Extended by:
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::BaseDSLMethods

#accessible?, #authorized?, #default_graphql_name, #description, #graphql_name, #introspection, #introspection?, #mutation, #name, #overridden_graphql_name, #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

.all_enum_value_definitionsArray<Schema::EnumValue>

Returns An unfiltered list of all definitions.

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/graphql/schema/enum.rb', line 87

def all_enum_value_definitions
  all_defns = if superclass.respond_to?(:all_enum_value_definitions)
    superclass.all_enum_value_definitions
  else
    []
  end

  @own_values && @own_values.each do |_key, value|
    if value.is_a?(Array)
      all_defns.concat(value)
    else
      all_defns << value
    end
  end

  all_defns
end

.coerce_input(value_name, ctx) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/graphql/schema/enum.rb', line 147

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



136
137
138
139
140
141
142
143
144
145
# File 'lib/graphql/schema/enum.rb', line 136

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.new(enum: self, value: value, context: ctx)
  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



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

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

.enum_values(context = GraphQL::Query::NullContext) ⇒ Array<GraphQL::Schema::EnumValue>

Returns Possible values of this enum.

Returns:



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/graphql/schema/enum.rb', line 64

def enum_values(context = GraphQL::Query::NullContext)
  inherited_values = superclass.respond_to?(:enum_values) ? superclass.enum_values(context) : nil
  visible_values = []
  warden = Warden.from_context(context)
  own_values.each do |key, values_entry|
    if (v = Warden.visible_entry?(:visible_enum_value?, values_entry, context, warden))
      visible_values << v
    end
  end

  if inherited_values
    # Local values take precedence over inherited ones
    inherited_values.each do |i_val|
      if !visible_values.any? { |v| v.graphql_name == i_val.graphql_name }
        visible_values << i_val
      end
    end
  end

  visible_values
end

.kindObject



121
122
123
# File 'lib/graphql/schema/enum.rb', line 121

def kind
  GraphQL::TypeKinds::ENUM
end

.validate_non_null_input(value_name, ctx, max_errors: nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/graphql/schema/enum.rb', line 125

def validate_non_null_input(value_name, ctx, max_errors: nil)
  allowed_values = ctx.warden.enum_values(self)
  matching_value = allowed_values.find { |v| v.graphql_name == value_name }

  if matching_value.nil?
    GraphQL::Query::InputValidationResult.from_problem("Expected #{GraphQL::Language.serialize(value_name)} to be one of: #{allowed_values.map(&:graphql_name).join(', ')}")
  else
    nil
  end
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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/graphql/schema/enum.rb', line 45

def value(*args, **kwargs, &block)
  kwargs[:owner] = self
  value = enum_value_class.new(*args, **kwargs, &block)
  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

.values(context = GraphQL::Query::NullContext) ⇒ Hash<String => GraphQL::Schema::EnumValue>

Returns Possible values of this enum, keyed by name.

Returns:



106
107
108
# File 'lib/graphql/schema/enum.rb', line 106

def values(context = GraphQL::Query::NullContext)
  enum_values(context).each_with_object({}) { |val, obj| obj[val.graphql_name] = val }
end