Class: Desiru::GraphQL::LazyFieldExtension

Inherits:
GraphQL::Schema::FieldExtension
  • Object
show all
Defined in:
lib/desiru/graphql/executor.rb

Overview

GraphQL field extension for lazy loading

Instance Method Summary collapse

Instance Method Details

#resolve(object:, arguments:, context:) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/desiru/graphql/executor.rb', line 102

def resolve(object:, arguments:, context:)
  result = yield(object, arguments)

  # If result is a promise, handle it appropriately
  if result.respond_to?(:then) && result.respond_to?(:fulfilled?)
    if result.fulfilled?
      result.value
    else
      # Create a lazy resolver that integrates with DataLoader
      ::GraphQL::Execution::Lazy.new do
        data_loader = context[:data_loader]

        # Ensure batch loads are performed before accessing value
        data_loader.perform_loads if data_loader && !result.fulfilled?

        result.value
      end
    end
  else
    result
  end
end