Class: GraphQL::Schema::Enum
- Extended by:
- Member::ValidatesInput
- Defined in:
- lib/graphql/schema/enum.rb
Overview
Extend this class to define GraphQL enums in your schema.
By default, GraphQL enum values are translated into Ruby strings.
You can provide a custom value with the value: keyword.
Examples
Example: # equivalent to
# enum PizzaTopping {
# MUSHROOMS
# ONIONS
# PEPPERS
# }
class PizzaTopping < GraphQL::Schema::Enum
value :MUSHROOMS
value :ONIONS
value :PEPPERS
end
Direct Known Subclasses
Introspection::DirectiveLocationEnum, Introspection::TypeKindEnum
Defined Under Namespace
Classes: MissingValuesError, UnresolvedValueError
Constant Summary
Constants included from Member::GraphQLTypeNames
Member::GraphQLTypeNames::Boolean, Member::GraphQLTypeNames::ID, Member::GraphQLTypeNames::Int
Instance Attribute Summary
Attributes included from Member::BaseDSLMethods
#default_graphql_name, #graphql_name
Attributes included from Member::RelayShortcuts
#connection_type, #connection_type_class, #edge_type, #edge_type_class
Attributes included from Member::HasAstNode
Class Method Summary collapse
-
.all_enum_value_definitions ⇒ Object
Returns.
-
.coerce_input(value_name, ctx) ⇒ Object
Called by the runtime with incoming string representations from a query.
-
.coerce_result(value, ctx) ⇒ Object
Called by the runtime when a field returns a value to give back to the client.
-
.enum_value_class(new_enum_value_class = nil) ⇒ Object
Returns.
-
.enum_values(context = GraphQL::Query::NullContext.instance) ⇒ Object
Returns.
- .inherited(child_class) ⇒ Object
- .kind ⇒ Object
- .validate_non_null_input(value_name, ctx, max_errors: nil) ⇒ Object
-
.value(*args, value_method: nil, **kwargs, &block) ⇒ Object
Define a value for this enum See Schema::EnumValue which handles these inputs by default.
- .value_methods(new_value = NOT_CONFIGURED) ⇒ Object
-
.values(context = GraphQL::Query::NullContext.instance) ⇒ Object
Returns.
Methods included from Member::ValidatesInput
coerce_isolated_input, coerce_isolated_result, valid_input?, valid_isolated_input?, validate_input
Methods inherited from Member
Methods included from Member::BaseDSLMethods
#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?
Methods included from FindInheritedValue
Methods included from Member::BaseDSLMethods::ConfigurationExtension
Methods included from Member::TypeSystemHelpers
#initialize, #kind, #list?, #non_null?, #to_list_type, #to_non_null_type, #to_type_signature
Methods included from Member::Scoped
#inherited, #reauthorize_scoped_objects, #scope_items
Methods included from Member::HasPath
Methods included from Member::HasAstNode
Methods included from Member::HasDirectives
add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive
Class Method Details
.all_enum_value_definitions ⇒ Object
Returns
Array<Schema::EnumValue>— An unfiltered list of all definitions
:call-seq:
all_enum_value_definitions() -> Array[Schema::EnumValue]
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/graphql/schema/enum.rb', line 158 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
Called by the runtime with incoming string representations from a query. It will match the string to a configured by name or by Ruby value.
Parameters
value_name(String, Object) — A string from a GraphQL query, or a Ruby value matching avalue(..., value: ...)configurationctx(GraphQL::Query::Context)
Raises
GraphQL::UnauthorizedEnumValueError— if an EnumValue matches but returns false for.authorized?. Goes to Schema.unauthorized_object.
Returns
Object— The Ruby value for the matched GraphQL::Schema::EnumValue
:call-seq:
coerce_input(String | Object value_name, GraphQL::Query::Context ctx) -> Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/graphql/schema/enum.rb', line 278 def coerce_input(value_name, ctx) all_values = ctx.types ? ctx.types.enum_values(self) : values.each_value # This tries matching by incoming GraphQL string, then checks Ruby-defined values if v = (all_values.find { |val| val.graphql_name == value_name } || all_values.find { |val| val.value == value_name }) if v.(ctx) v.value else raise GraphQL::UnauthorizedEnumValueError.new(type: self, enum_value: v, context: ctx) end else nil end end |
.coerce_result(value, ctx) ⇒ Object
Called by the runtime when a field returns a value to give back to the client. This method checks that the incoming value matches one of the enum's defined values.
Parameters
value(Object) — Any value matching the values for this enum.ctx(GraphQL::Query::Context)
Raises
GraphQL::Schema::Enum::UnresolvedValueError— if value doesn't match a configured value or if the matching value isn't authorized.
Returns
String— The GraphQL-ready string for value
:call-seq:
coerce_result(Object value, GraphQL::Query::Context ctx) -> String
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/graphql/schema/enum.rb', line 249 def coerce_result(value, ctx) types = ctx.types all_values = types ? types.enum_values(self) : values.each_value enum_value = all_values.find { |val| val.value == value } if enum_value && (was_authed = enum_value.(ctx)) enum_value.graphql_name else raise const_get(:UnresolvedValueError).new(enum: self, value: value, context: ctx, authorized: was_authed) end end |
.enum_value_class(new_enum_value_class = nil) ⇒ Object
Returns
Class— for handlingvalue(...)inputs and buildingGraphQL::Enum::EnumValues out of them
:call-seq:
enum_value_class(new_enum_value_class) -> Class
192 193 194 195 196 197 198 199 200 |
# File 'lib/graphql/schema/enum.rb', line 192 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.instance) ⇒ Object
Returns
Array<GraphQL::Schema::EnumValue>— Possible values of this enum
:call-seq:
enum_values(context:) -> Array[GraphQL::Schema::EnumValue]
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/graphql/schema/enum.rb', line 116 def enum_values(context = GraphQL::Query::NullContext.instance) inherited_values = superclass.respond_to?(:enum_values) ? superclass.enum_values(context) : nil visible_values = [] types = Warden.types_from_context(context) own_values.each do |key, values_entry| visible_value = nil #: GraphQL::Schema::EnumValue? if values_entry.is_a?(Array) values_entry.each do |v| if types.visible_enum_value?(v, context) if visible_value.nil? visible_value = v visible_values << v else raise DuplicateNamesError.new( duplicated_name: v.path, duplicated_definition_1: visible_value.inspect, duplicated_definition_2: v.inspect ) end end end elsif types.visible_enum_value?(values_entry, context) visible_values << values_entry 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 |
.inherited(child_class) ⇒ Object
293 294 295 296 297 298 299 300 301 |
# File 'lib/graphql/schema/enum.rb', line 293 def inherited(child_class) if child_class.name # Don't assign a custom error class to anonymous classes # because they would end up with names like `#<Class0x1234>::UnresolvedValueError` which messes up bug trackers child_class.const_set(:UnresolvedValueError, Class.new(Schema::Enum::UnresolvedValueError)) end child_class.class_exec { @value_methods = nil } super end |
.kind ⇒ Object
214 215 216 |
# File 'lib/graphql/schema/enum.rb', line 214 def kind GraphQL::TypeKinds::ENUM end |
.validate_non_null_input(value_name, ctx, max_errors: nil) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/graphql/schema/enum.rb', line 218 def validate_non_null_input(value_name, ctx, max_errors: nil) allowed_values = ctx.types.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 # rescue MissingValuesError # nil end |
.value(*args, value_method: nil, **kwargs, &block) ⇒ Object
Define a value for this enum See Schema::EnumValue which handles these inputs by default
Options
kwargs.:graphql_name(String, Symbol) — the GraphQL value for this, usuallySCREAMING_CASEkwargs.:description,(String) — the GraphQL description for this value, present in documentationkwargs.:comment,(String) — the GraphQL comment for this value, present in documentationkwargs.:value(::Object) — the translated Ruby value for this object (defaults tographql_name)kwargs.:value_method,(::Object) — the method name to fetchgraphql_name(defaults tographql_name.downcase)kwargs.:deprecation_reason(String) — if this object is deprecated, include a message here
Parameters
value_method(Symbol, false) — A method to generate for this value, orfalseto skip generation
Returns
void
:call-seq:
value(*args, Symbol | false value_method:, **kwargs, &block) -> void
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/graphql/schema/enum.rb', line 87 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 |
.value_methods(new_value = NOT_CONFIGURED) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/graphql/schema/enum.rb', line 202 def value_methods(new_value = NOT_CONFIGURED) if NOT_CONFIGURED.equal?(new_value) if @value_methods != nil @value_methods else find_inherited_value(:value_methods, false) end else @value_methods = new_value end end |
.values(context = GraphQL::Query::NullContext.instance) ⇒ Object
Returns
Hash<String => GraphQL::Schema::EnumValue>— Possible values of this enum, keyed by name.
:call-seq:
values(context:) -> Hash[String, GraphQL::Schema::EnumValue]
182 183 184 |
# File 'lib/graphql/schema/enum.rb', line 182 def values(context = GraphQL::Query::NullContext.instance) enum_values(context).each_with_object({}) { |val, obj| obj[val.graphql_name] = val } end |