Class: GraphQL::Dataloader::NullDataloader

Inherits:
GraphQL::Dataloader show all
Defined in:
lib/graphql/dataloader/null_dataloader.rb

Overview

GraphQL-Ruby uses this when Dataloader isn’t enabled.

It runs execution code inline and gathers lazy objects (eg. Promises) and resolves them during #run.

Instance Attribute Summary

Attributes inherited from GraphQL::Dataloader

#fiber_limit

Instance Method Summary collapse

Methods inherited from GraphQL::Dataloader

#cleanup_fiber, #get_fiber_variables, #lazy_at_depth, #merge_records, #nonblocking?, #run_fiber, #set_fiber_variables, #spawn_fiber, use, with_dataloading

Constructor Details

#initializeNullDataloader

Returns a new instance of NullDataloader.



10
11
12
# File 'lib/graphql/dataloader/null_dataloader.rb', line 10

def initialize(*)
  @lazies_at_depth = Hash.new { |h,k| h[k] = [] }
end

Instance Method Details

#append_job(callable = nil) ⇒ Object



55
56
57
58
# File 'lib/graphql/dataloader/null_dataloader.rb', line 55

def append_job(callable = nil)
  callable ? callable.call : yield
  nil
end

#clear_cacheObject



49
# File 'lib/graphql/dataloader/null_dataloader.rb', line 49

def clear_cache; end

#freezeObject



14
15
16
17
18
# File 'lib/graphql/dataloader/null_dataloader.rb', line 14

def freeze
  @lazies_at_depth.default_proc = nil
  @lazies_at_depth.freeze
  super
end

#run(trace_query_lazy: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/dataloader/null_dataloader.rb', line 20

def run(trace_query_lazy: nil)
  with_trace_query_lazy(trace_query_lazy) do
    while !@lazies_at_depth.empty?
      smallest_depth = nil
      @lazies_at_depth.each_key do |depth_key|
        smallest_depth ||= depth_key
        if depth_key < smallest_depth
          smallest_depth = depth_key
        end
      end

      if smallest_depth
        lazies = @lazies_at_depth.delete(smallest_depth)
        lazies.each(&:value) # resolve these Lazy instances
      end
    end
  end
end

#run_isolatedObject



39
40
41
42
43
44
45
46
47
# File 'lib/graphql/dataloader/null_dataloader.rb', line 39

def run_isolated
  new_dl = self.class.new
  res = nil
  new_dl.append_job {
    res = yield
  }
  new_dl.run
  res
end

#withObject

Raises:



60
61
62
# File 'lib/graphql/dataloader/null_dataloader.rb', line 60

def with(*)
  raise GraphQL::Error, "GraphQL::Dataloader is not running -- add `use GraphQL::Dataloader` to your schema to use Dataloader sources."
end

#yield(_source) ⇒ Object

Raises:



51
52
53
# File 'lib/graphql/dataloader/null_dataloader.rb', line 51

def yield(_source)
  raise GraphQL::Error, "GraphQL::Dataloader is not running -- add `use GraphQL::Dataloader` to your schema to use Dataloader sources."
end