Class: GraphWeaver::Codegen::EnumType

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_weaver/codegen/enum_type.rb

Overview

How one GraphQL enum maps onto an app-owned T::Enum, so generated code speaks YOUR enum instead of generating one per module:

 class PetKind < T::Enum
   enums { Cat = new("cat"); Dog = new("dog") }
 end

 GraphWeaver.register_enum("Species", PetKind)

The wire mapping is inferred by name ("CAT" <-> PetKind::Cat, case/underscore-insensitive against each member's serialized value); map: pins renames explicitly and merges over inference. Every wire value the schema declares must resolve — generation fails naming the gaps — unless fallback: names a member to absorb unknown values (forward-compat for servers that add members; inputs stay strict).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graphql_name, type, map: nil, fallback: nil, requires: nil) ⇒ EnumType

Returns a new instance of EnumType.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graph_weaver/codegen/enum_type.rb', line 23

def initialize(graphql_name, type, map: nil, fallback: nil, requires: nil)
  @graphql_name = graphql_name.to_s
  unless type.is_a?(Class) && type < T::Enum
    raise ArgumentError, "type: must be a T::Enum subclass, got #{type.inspect}"
  end
  unless type.name
    raise ArgumentError, "type: must be a named constant (anonymous classes can't appear in generated source)"
  end

  @type = type
  @map = map || {}
  @fallback = fallback
  @requires = Array(requires)

  if fallback && !type.values.include?(fallback)
    raise ArgumentError, "fallback: must be a #{type} member, got #{fallback.inspect}"
  end
end

Instance Attribute Details

#fallbackObject (readonly)

Returns the value of attribute fallback.



21
22
23
# File 'lib/graph_weaver/codegen/enum_type.rb', line 21

def fallback
  @fallback
end

#graphql_nameObject (readonly)

Returns the value of attribute graphql_name.



21
22
23
# File 'lib/graph_weaver/codegen/enum_type.rb', line 21

def graphql_name
  @graphql_name
end

#requiresObject (readonly)

Returns the value of attribute requires.



21
22
23
# File 'lib/graph_weaver/codegen/enum_type.rb', line 21

def requires
  @requires
end

#typeObject (readonly)

Returns the value of attribute type.



21
22
23
# File 'lib/graph_weaver/codegen/enum_type.rb', line 21

def type
  @type
end

Instance Method Details

#mapping_for(wire_values) ⇒ Object

wire value => member for every value the schema declares; raises naming the unmappable ones (unless fallback: absorbs them)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/graph_weaver/codegen/enum_type.rb', line 44

def mapping_for(wire_values)
  mapping = {}
  missing = []

  wire_values.each do |wire|
    member = @map[wire] || infer(wire)
    member ? mapping[wire] = member : missing << wire
  end

  if missing.any? && !fallback
    raise GraphWeaver::Error,
      "#{type} has no member for #{graphql_name} value(s) #{missing.join(", ")}" \
      "add them, pin with map:, or absorb with fallback:"
  end

  mapping
end