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.



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

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.



204
205
206
# File 'lib/graph_weaver/codegen/nodes.rb', line 204

def graphql_name
  @graphql_name
end

#mappingObject (readonly)

Returns the value of attribute mapping.



204
205
206
# File 'lib/graph_weaver/codegen/nodes.rb', line 204

def mapping
  @mapping
end

Instance Method Details

#bare_typeObject



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

def bare_type = @type_name

#cast(expr, _depth) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/graph_weaver/codegen/nodes.rb', line 218

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



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

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)


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

def coerce? = true

#coerce_input_typeObject



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

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

#const_prefixObject



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

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

#fallback_constObject



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

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

#hash_coerce(expr, _depth) ⇒ Object



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

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

#serialize(expr, _depth) ⇒ Object



226
227
228
# File 'lib/graph_weaver/codegen/nodes.rb', line 226

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