Class: GraphWeaver::Codegen::MappedEnum

Inherits:
Node
  • Object
show all
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 _FROM_WIRE / _TO_WIRE constants translate at the boundary. fallback: makes casting absorb unknown wire values (inputs stay strict).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#hash_coerce_identity?, #identity?, #nested, #non_null?, #prop_type, #serialize_identity?

Constructor Details

#initialize(enum_type, wire_values) ⇒ MappedEnum

Returns a new instance of MappedEnum.



210
211
212
213
214
215
# File 'lib/graph_weaver/codegen/nodes.rb', line 210

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_nameObject (readonly)

Returns the value of attribute graphql_name.



208
209
210
# File 'lib/graph_weaver/codegen/nodes.rb', line 208

def graphql_name
  @graphql_name
end

#mappingObject (readonly)

Returns the value of attribute mapping.



208
209
210
# File 'lib/graph_weaver/codegen/nodes.rb', line 208

def mapping
  @mapping
end

Instance Method Details

#bare_typeObject



220
# File 'lib/graph_weaver/codegen/nodes.rb', line 220

def bare_type = @type_name

#cast(expr, _depth) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/graph_weaver/codegen/nodes.rb', line 222

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



239
240
241
# File 'lib/graph_weaver/codegen/nodes.rb', line 239

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)

Returns:

  • (Boolean)


237
# File 'lib/graph_weaver/codegen/nodes.rb', line 237

def coerce? = true

#coerce_input_typeObject



243
# File 'lib/graph_weaver/codegen/nodes.rb', line 243

def coerce_input_type = "T.any(#{@type_name}, String)"

#const_prefixObject



217
# File 'lib/graph_weaver/codegen/nodes.rb', line 217

def const_prefix = GraphWeaver::Inflect.underscore(@graphql_name).upcase

#fallback_constObject



218
# File 'lib/graph_weaver/codegen/nodes.rb', line 218

def fallback_const = @fallback && "#{@type_name}.deserialize(#{@fallback.serialize.to_s.inspect})"

#hash_coerce(expr, _depth) ⇒ Object



244
# File 'lib/graph_weaver/codegen/nodes.rb', line 244

def hash_coerce(expr, _depth) = coerce(expr)

#serialize(expr, _depth) ⇒ Object



230
231
232
# File 'lib/graph_weaver/codegen/nodes.rb', line 230

def serialize(expr, _depth)
  "#{const_prefix}_TO_WIRE.fetch(#{expr})"
end