Class: GraphQL::Execution::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/execution/interpreter.rb,
lib/graphql/execution/interpreter/resolve.rb,
lib/graphql/execution/interpreter/runtime.rb,
lib/graphql/execution/interpreter/hash_response.rb,
lib/graphql/execution/interpreter/execution_errors.rb

Defined Under Namespace

Modules: Resolve Classes: ExecutionErrors, HashResponse, Runtime

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInterpreter

Returns a new instance of Interpreter



10
11
# File 'lib/graphql/execution/interpreter.rb', line 10

def initialize
end

Class Method Details

.begin_multiplex(multiplex) ⇒ Object



33
34
35
36
37
# File 'lib/graphql/execution/interpreter.rb', line 33

def self.begin_multiplex(multiplex)
  # Since this is basically the batching context,
  # share it for a whole multiplex
  multiplex.context[:interpreter_instance] ||= self.new
end

.begin_query(query, multiplex) ⇒ Object



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

def self.begin_query(query, multiplex)
  # The batching context is shared by the multiplex,
  # so fetch it out and use that instance.
  interpreter =
    query.context.namespace(:interpreter)[:interpreter_instance] =
    multiplex.context[:interpreter_instance]
  interpreter.evaluate(query)
  query
end

.finish_multiplex(_results, multiplex) ⇒ Object



49
50
51
52
# File 'lib/graphql/execution/interpreter.rb', line 49

def self.finish_multiplex(_results, multiplex)
  interpreter = multiplex.context[:interpreter_instance]
  interpreter.sync_lazies(multiplex: multiplex)
end

.finish_query(query, _multiplex) ⇒ Object



54
55
56
57
58
# File 'lib/graphql/execution/interpreter.rb', line 54

def self.finish_query(query, _multiplex)
  {
    "data" => query.context.namespace(:interpreter)[:runtime].final_value
  }
end

.use(schema_defn) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/graphql/execution/interpreter.rb', line 20

def self.use(schema_defn)
  schema_defn.target.interpreter = true
  # Reach through the legacy objects for the actual class defn
  schema_class = schema_defn.target.class
  # This is not good, since both of these are holding state now,
  # we have to update both :(
  [schema_class, schema_defn].each do |schema_config|
    schema_config.query_execution_strategy(GraphQL::Execution::Interpreter)
    schema_config.mutation_execution_strategy(GraphQL::Execution::Interpreter)
    schema_config.subscription_execution_strategy(GraphQL::Execution::Interpreter)
  end
end

Instance Method Details

#evaluate(query) ⇒ Interpreter::Runtime

Run the eager part of query



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/graphql/execution/interpreter.rb', line 62

def evaluate(query)
  # Although queries in a multiplex _share_ an Interpreter instance,
  # they also have another item of state, which is private to that query
  # in particular, assign it here:
  runtime = Runtime.new(
    query: query,
    response: HashResponse.new,
  )
  query.context.namespace(:interpreter)[:runtime] = runtime

  query.trace("execute_query", {query: query}) do
    runtime.run_eager
  end

  runtime
end

#execute(_operation, _root_type, query) ⇒ Object

Support Executor :S



14
15
16
17
18
# File 'lib/graphql/execution/interpreter.rb', line 14

def execute(_operation, _root_type, query)
  runtime = evaluate(query)
  sync_lazies(query: query)
  runtime.final_value
end

#sync_lazies(query: nil, multiplex: nil) ⇒ void

This method returns an undefined value.

Run the lazy part of query or multiplex.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/graphql/execution/interpreter.rb', line 81

def sync_lazies(query: nil, multiplex: nil)
  tracer = query || multiplex
  if query.nil? && multiplex.queries.length == 1
    query = multiplex.queries[0]
  end
  queries = multiplex ? multiplex.queries : [query]
  final_values = queries.map do |query|
    runtime = query.context.namespace(:interpreter)[:runtime]
    # it might not be present if the query has an error
    runtime ? runtime.final_value : nil
  end
  final_values.compact!
  tracer.trace("execute_query_lazy", {multiplex: multiplex, query: query}) do
    Interpreter::Resolve.resolve_all(final_values)
  end
end