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
# File 'lib/graphql/execution/interpreter/arguments_cache.rb', line 7

def initialize(query)
  @query = query
  @dataloader = query.context.dataloader
  @storage = Hash.new do |h, argument_owner|
    args_by_parent = if argument_owner.arguments_statically_coercible?
      shared_values_cache = {}
      Hash.new do |h2, ignored_parent_object|
        h2[ignored_parent_object] = shared_values_cache
      end
    else
      Hash.new do |h2, parent_object|
        args_by_node = {}
        args_by_node.compare_by_identity
        h2[parent_object] = args_by_node
      end
    end
    args_by_parent.compare_by_identity
    h[argument_owner] = args_by_parent
  end
  @storage.compare_by_identity
end

Instance Method Details

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

Yields:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/graphql/execution/interpreter/arguments_cache.rb', line 42

def dataload_for(ast_node, argument_owner, parent_object, &block)
  # First, normalize all AST or Ruby values to a plain Ruby hash
  arg_storage = @storage[argument_owner][parent_object]
  if (args = arg_storage[ast_node])
    yield(args)
  else
    args_hash = self.class.prepare_args_hash(@query, ast_node)
    argument_owner.coerce_arguments(parent_object, args_hash, @query.context) do |resolved_args|
      arg_storage[ast_node] = resolved_args
      yield(resolved_args)
    end
  end
  nil
end

#fetch(ast_node, argument_owner, parent_object) ⇒ Object



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

def fetch(ast_node, argument_owner, parent_object)
  # This runs eagerly if no block is given
  @storage[argument_owner][parent_object][ast_node] ||= begin
    args_hash = self.class.prepare_args_hash(@query, ast_node)
    kwarg_arguments = argument_owner.coerce_arguments(parent_object, args_hash, @query.context)
    @query.after_lazy(kwarg_arguments) do |resolved_args|
      @storage[argument_owner][parent_object][ast_node] = resolved_args
    end
  end

end