Class: GraphQL::Execution::Interpreter::ArgumentsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/interpreter/arguments_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ ArgumentsCache

Returns a new instance of ArgumentsCache.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphql/execution/interpreter/arguments_cache.rb', line 7

def initialize(query)
  @query = query
  @dataloader = query.context.dataloader
  @storage = Hash.new do |h, ast_node|
    h[ast_node] = Hash.new do |h2, arg_owner|
      h2[arg_owner] = Hash.new do |h3, parent_object|
        dataload_for(ast_node, arg_owner, parent_object) do |kwarg_arguments|
          h3[parent_object] = @query.schema.after_lazy(kwarg_arguments) do |resolved_args|
            h3[parent_object] = resolved_args
          end
        end

        if !h3.key?(parent_object)
          # TODO should i bother putting anything here?
          h3[parent_object] = NO_ARGUMENTS
        else
          h3[parent_object]
        end
      end
    end
  end
end

Instance Method Details

#dataload_for(ast_node, argument_owner, parent_object) {|Interpreter::Arguments, Lazy<Interpreter::Arguments>| ... } ⇒ Object

Yields:



46
47
48
49
50
51
# File 'lib/graphql/execution/interpreter/arguments_cache.rb', line 46

def dataload_for(ast_node, argument_owner, parent_object, &block)
  # First, normalize all AST or Ruby values to a plain Ruby hash
  args_hash = self.class.prepare_args_hash(@query, ast_node)
  argument_owner.coerce_arguments(parent_object, args_hash, @query.context, &block)
  nil
end

#fetch(ast_node, argument_owner, parent_object) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/graphql/execution/interpreter/arguments_cache.rb', line 30

def fetch(ast_node, argument_owner, parent_object)
  @storage[ast_node][argument_owner][parent_object]
  # If any jobs were enqueued, run them now,
  # since this might have been called outside of execution.
  # (The jobs are responsible for updating `result` in-place.)
  @dataloader.run
  # Ack, the _hash_ is updated, but the key is eventually
  # overridden with an immutable arguments instance.
  # The first call queues up the job,
  # then this call fetches the result.
  # TODO this should be better, find a solution
  # that works with merging the runtime.rb code
  @storage[ast_node][argument_owner][parent_object]
end