Class: GraphWeaver::Codegen::MappedEnum
- Inherits:
-
Object
- Object
- GraphWeaver::Codegen::MappedEnum
- Defined in:
- lib/graph_weaver/codegen/nodes.rb
Overview
A GraphQL enum mapped onto an app-owned T::Enum (see EnumType): no
generated enum class — instead module-level
Instance Attribute Summary collapse
-
#graphql_name ⇒ Object
readonly
Returns the value of attribute graphql_name.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
Instance Method Summary collapse
- #bare_type ⇒ Object
- #cast(expr, _depth) ⇒ Object
- #coerce(expr) ⇒ Object
-
#coerce? ⇒ Boolean
kwargs and hash fields accept the member or its wire value; unlike casting, bad input raises even with a fallback (a typo'd input is our bug, not server drift).
- #coerce_input_type ⇒ Object
- #const_prefix ⇒ Object
- #fallback_const ⇒ Object
- #hash_coerce(expr, _depth) ⇒ Object
- #hash_coerce_identity? ⇒ Boolean
- #identity? ⇒ Boolean
-
#initialize(enum_type, wire_values) ⇒ MappedEnum
constructor
A new instance of MappedEnum.
- #nested ⇒ Object
- #non_null? ⇒ Boolean
- #prop_type ⇒ Object
- #serialize(expr, _depth) ⇒ Object
- #serialize_identity? ⇒ Boolean
Constructor Details
#initialize(enum_type, wire_values) ⇒ MappedEnum
Returns a new instance of MappedEnum.
217 218 219 220 221 222 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 217 def initialize(enum_type, wire_values) @graphql_name = enum_type.graphql_name @type_name = enum_type.type.name @fallback = enum_type.fallback @mapping = enum_type.mapping_for(wire_values) end |
Instance Attribute Details
#graphql_name ⇒ Object (readonly)
Returns the value of attribute graphql_name.
215 216 217 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 215 def graphql_name @graphql_name end |
#mapping ⇒ Object (readonly)
Returns the value of attribute mapping.
215 216 217 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 215 def mapping @mapping end |
Instance Method Details
#bare_type ⇒ Object
227 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 227 def = @type_name |
#cast(expr, _depth) ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 233 def cast(expr, _depth) if @fallback "#{const_prefix}_FROM_WIRE.fetch(#{expr}) { #{fallback_const} }" else "#{const_prefix}_FROM_WIRE.fetch(#{expr})" end end |
#coerce(expr) ⇒ Object
254 255 256 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 254 def coerce(expr) "(#{expr}.is_a?(#{@type_name}) ? #{expr} : #{const_prefix}_FROM_WIRE.fetch(#{expr}))" end |
#coerce? ⇒ Boolean
kwargs and hash fields accept the member or its wire value; unlike casting, bad input raises even with a fallback (a typo'd input is our bug, not server drift)
252 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 252 def coerce? = true |
#coerce_input_type ⇒ Object
258 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 258 def coerce_input_type = "T.any(#{@type_name}, String)" |
#const_prefix ⇒ Object
224 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 224 def const_prefix = GraphWeaver::Inflect.underscore(@graphql_name).upcase |
#fallback_const ⇒ Object
225 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 225 def fallback_const = @fallback && "#{@type_name}.deserialize(#{@fallback.serialize.to_s.inspect})" |
#hash_coerce(expr, _depth) ⇒ Object
259 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 259 def hash_coerce(expr, _depth) = coerce(expr) |
#hash_coerce_identity? ⇒ Boolean
260 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 260 def hash_coerce_identity? = false |
#identity? ⇒ Boolean
241 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 241 def identity? = false |
#nested ⇒ Object
262 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 262 def nested = nil |
#non_null? ⇒ Boolean
261 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 261 def non_null? = false |
#prop_type ⇒ Object
229 230 231 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 229 def prop_type "T.nilable(#{})" end |
#serialize(expr, _depth) ⇒ Object
243 244 245 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 243 def serialize(expr, _depth) "#{const_prefix}_TO_WIRE.fetch(#{expr})" end |
#serialize_identity? ⇒ Boolean
247 |
# File 'lib/graph_weaver/codegen/nodes.rb', line 247 def serialize_identity? = false |