Module: Desiru::GraphQL::TypeBuilder

Extended by:
TypeBuilder
Included in:
TypeBuilder
Defined in:
lib/desiru/graphql/type_builder.rb

Overview

Handles GraphQL type generation and caching

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cache_mutexObject

Returns the value of attribute cache_mutex.



17
18
19
# File 'lib/desiru/graphql/type_builder.rb', line 17

def cache_mutex
  @cache_mutex
end

.type_cacheObject

Returns the value of attribute type_cache.



17
18
19
# File 'lib/desiru/graphql/type_builder.rb', line 17

def type_cache
  @type_cache
end

Instance Method Details

#build_output_type(signature) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/desiru/graphql/type_builder.rb', line 26

def build_output_type(signature)
  # Create a stable cache key based on signature structure
  cache_key = generate_type_cache_key('Output', signature.output_fields)

  # Check cache first
  @cache_mutex.synchronize do
    return @type_cache[cache_key] if @type_cache[cache_key]
  end

  output_field_defs = build_field_definitions(signature.output_fields)

  output_type = Class.new(::GraphQL::Schema::Object) do
    graphql_name "Output#{cache_key.hash.abs}"
    description 'Generated output type'

    output_field_defs.each do |field_name, field_def|
      field field_name, field_def[:type],
            null: field_def[:null],
            description: field_def[:description]
    end
  end

  # Store in cache
  @cache_mutex.synchronize do
    @type_cache[cache_key] = output_type
  end

  output_type
end

#clear_type_cache!Object



20
21
22
23
24
# File 'lib/desiru/graphql/type_builder.rb', line 20

def clear_type_cache!
  @cache_mutex.synchronize do
    @type_cache.clear
  end
end

#graphql_type_for_field(field) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/desiru/graphql/type_builder.rb', line 56

def graphql_type_for_field(field)
  case field.type
  when :int, :integer
    ::GraphQL::Types::Int
  when :float
    ::GraphQL::Types::Float
  when :bool, :boolean
    ::GraphQL::Types::Boolean
  when :list
    # Handle list types
    element_type = graphql_type_for_element(field.element_type)
    [element_type]
  when :literal
    # Create enum type for literal values
    EnumBuilder.create_enum_type(field, @type_cache, @cache_mutex)
  else
    ::GraphQL::Types::String
  end
end