Class: GraphQL::Dataloader::NullDataloader
- Inherits:
-
Dataloader
- Object
- Dataloader
- GraphQL::Dataloader::NullDataloader
- 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 Method Summary collapse
- #append_job(callable = nil) ⇒ Object
- #clear_cache ⇒ Object
- #freeze ⇒ Object
-
#initialize ⇒ NullDataloader
constructor
A new instance of NullDataloader.
- #run(trace_query_lazy: nil) ⇒ Object
- #run_isolated ⇒ Object
- #with ⇒ Object
- #yield(_source) ⇒ Object
Constructor Details
#initialize ⇒ NullDataloader
Returns a new instance of NullDataloader.
12 13 14 |
# File 'lib/graphql/dataloader/null_dataloader.rb', line 12 def initialize(*) @lazies_at_depth = Hash.new { |h,k| h[k] = [] } end |
Instance Method Details
#append_job(callable = nil) ⇒ Object
61 62 63 64 |
# File 'lib/graphql/dataloader/null_dataloader.rb', line 61 def append_job(callable = nil) callable ? callable.call : yield nil end |
#clear_cache ⇒ Object
55 |
# File 'lib/graphql/dataloader/null_dataloader.rb', line 55 def clear_cache; end |
#freeze ⇒ Object
16 17 18 19 20 |
# File 'lib/graphql/dataloader/null_dataloader.rb', line 16 def freeze @lazies_at_depth.default_proc = nil @lazies_at_depth.freeze super end |
#run(trace_query_lazy: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/graphql/dataloader/null_dataloader.rb', line 22 def run(trace_query_lazy: nil) with_trace_query_lazy(trace_query_lazy) do while !@lazies_at_depth.empty? smallest_depth = nil #: Integer? @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_isolated ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/graphql/dataloader/null_dataloader.rb', line 41 def run_isolated # Reuse this instance because execution code may already have a reference to _this_ `dataloader` inside the given block. prev_lazies_at_depth = @lazies_at_depth @lazies_at_depth = @lazies_at_depth.dup.clear res = nil #: untyped append_job { res = yield } run res ensure @lazies_at_depth = prev_lazies_at_depth end |