Class: GraphWeaver::Codegen::MappedEnum

Inherits:
Object
  • 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

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 bare_type = @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)

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

def hash_coerce_identity? = false

#identity? ⇒ Boolean

Returns:

  • (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

Returns:

  • (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(#{bare_type})"
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

Returns:

  • (Boolean)


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

def serialize_identity? = false