Class: Desiru::GraphQL::Executor

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

Overview

Custom GraphQL executor with batch loading support

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, data_loader: nil) ⇒ Executor

Returns a new instance of Executor.



11
12
13
14
# File 'lib/desiru/graphql/executor.rb', line 11

def initialize(schema, data_loader: nil)
  @schema = schema
  @data_loader = data_loader || DataLoader.new
end

Instance Attribute Details

#data_loaderObject (readonly)

Returns the value of attribute data_loader.



9
10
11
# File 'lib/desiru/graphql/executor.rb', line 9

def data_loader
  @data_loader
end

#schemaObject (readonly)

Returns the value of attribute schema.



9
10
11
# File 'lib/desiru/graphql/executor.rb', line 9

def schema
  @schema
end

Instance Method Details

#execute(query_string, variables: {}, context: {}, operation_name: nil) ⇒ Object

Execute a GraphQL query with batch loading



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/desiru/graphql/executor.rb', line 17

def execute(query_string, variables: {}, context: {}, operation_name: nil)
  # Add data loader to context
  context[:data_loader] = @data_loader

  # Wrap execution with batch loading
  result = nil
  batch_execute do
    result = @schema.execute(
      query_string,
      variables: variables,
      context: context,
      operation_name: operation_name
    )
  end

  result
end

#execute_batch(queries) ⇒ Object

Execute multiple queries in a single batch



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/desiru/graphql/executor.rb', line 36

def execute_batch(queries)
  results = []

  batch_execute do
    queries.each do |query_params|
      query_params[:context] ||= {}
      query_params[:context][:data_loader] = @data_loader

      results << @schema.execute(
        query_params[:query],
        variables: query_params[:variables] || {},
        context: query_params[:context],
        operation_name: query_params[:operation_name]
      )
    end
  end

  results
end

#execute_with_lazy_loading(query_string, variables: {}, context: {}, operation_name: nil) ⇒ Object

Execute with automatic lazy loading support



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

def execute_with_lazy_loading(query_string, variables: {}, context: {}, operation_name: nil)
  context[:data_loader] = @data_loader

  # Use GraphQL's built-in lazy execution
  @schema.execute(
    query_string,
    variables: variables,
    context: context,
    operation_name: operation_name
  ) do |schema_query|
    # Configure lazy loading behavior
    schema_query.after_lazy_resolve do |value|
      # Trigger batch loading after each lazy resolution
      @data_loader.perform_loads
      value
    end
  end
end