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/arguments.rb,
lib/graphql/execution/interpreter/argument_value.rb,
lib/graphql/execution/interpreter/arguments_cache.rb,
lib/graphql/execution/interpreter/execution_errors.rb,
lib/graphql/execution/interpreter/handles_raw_value.rb,
lib/graphql/execution/interpreter/runtime/graphql_result.rb

Defined Under Namespace

Modules: Resolve Classes: ArgumentValue, Arguments, ArgumentsCache, ExecutionErrors, ListResultFailedError, RawValue, Runtime

Constant Summary collapse

NO_OPERATION =

Used internally to signal that the query shouldn't be executed :nodoc:

GraphQL::EmptyObjects::EMPTY_HASH

Class Method Summary collapse

Class Method Details

.run_all(schema, query_options, context: {}, max_complexity: schema.max_complexity) ⇒ Object

Parameters

  • schema (GraphQL::Schema)
  • queries (Array<GraphQL::Query, Hash>)
  • context (Hash)
  • max_complexity (Integer, nil)

Returns

  • Array<GraphQL::Query::Result> — One result per query

:call-seq:

run_all(GraphQL::Schema schema, query_options, Hash context:, Integer | nil max_complexity:) -> Array[GraphQL::Query::Result]

: (singleton(GraphQL::Schema) schema, Array query_options, ?context: Hash[Symbol, untyped], ?max_complexity: Integer?) -> Array



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/graphql/execution/interpreter.rb', line 35

def run_all(schema, query_options, context: {}, max_complexity: schema.max_complexity)
  previous_multiplex = Fiber[:__graphql_current_multiplex]
  queries = query_options.map do |opts|
    query = case opts
    when Hash
      schema.query_class.new(schema, nil, **opts)
    when GraphQL::Query, GraphQL::Query::Partial
      opts
    else
      raise "Expected Hash or GraphQL::Query, not #{opts.class} (#{opts.inspect})"
    end
    query
  end #: Array[untyped]

  return GraphQL::EmptyObjects::EMPTY_ARRAY if queries.empty?

  multiplex = Execution::Multiplex.new(schema: schema, queries: queries, context: context, max_complexity: max_complexity)
  trace = multiplex.current_trace
  Fiber[:__graphql_current_multiplex] = multiplex
  trace.execute_multiplex(multiplex: multiplex) do
    schema = multiplex.schema #: singleton(GraphQL::Schema)
    queries = multiplex.queries #: Array[untyped]
    multiplex_analyzers = schema.multiplex_analyzers
    if multiplex.max_complexity
      multiplex_analyzers += [GraphQL::Analysis::MaxQueryComplexity]
    end

    trace.begin_analyze_multiplex(multiplex, multiplex_analyzers)
    schema.analysis_engine.analyze_multiplex(multiplex, multiplex_analyzers)
    trace.end_analyze_multiplex(multiplex, multiplex_analyzers)

    begin
      # Since this is basically the batching context,
      # share it for a whole multiplex
      multiplex.context[:interpreter_instance] ||= multiplex.schema.query_execution_strategy(deprecation_warning: false).new
      # Do as much eager evaluation of the query as possible
      results = []
      queries.each_with_index do |query, idx|
        multiplex.dataloader.append_job {
          operation = query.selected_operation
          result = if operation.nil? || !query.valid? || !query.context.errors.empty?
            NO_OPERATION
          else
            begin
              # 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)
              query.context.namespace(:interpreter_runtime)[:runtime] = runtime
              if query.subscription? && !query.subscription_update?
                schema.subscriptions.initialize_subscriptions(query)
              end
              query.current_trace.execute_query(query: query) do
                runtime.run_eager
              end
            rescue GraphQL::ExecutionError => err
              query.context.errors << err
            end
          end
          results[idx] = result
        }
      end

      multiplex.dataloader.run(trace_query_lazy: multiplex)

      # Then, find all errors and assign the result to the query object
      results.each_with_index do |data_result, idx|
        query = queries[idx]
        # Assign the result so that it can be accessed in instrumentation
        query.result_values = if data_result.equal?(NO_OPERATION)
          if !query.valid? || !query.context.errors.empty?
            # A bit weird, but `Query#static_errors` _includes_ `query.context.errors`
            { "errors" => query.static_errors.map(&:to_h) }
          else
            data_result
          end
        else
          if query.subscription?
            schema.subscriptions.finish_subscriptions(query)
          end
          result = {}

          if !query.context.errors.empty?
            error_result = query.context.errors.map(&:to_h)
            result["errors"] = error_result
          end

          result["data"] = query.context.namespace(:interpreter_runtime)[:runtime].final_result

          result
        end
        if query.context.namespace?(:__query_result_extensions__)
          query.result_values["extensions"] = query.context.namespace(:__query_result_extensions__)
        end
        # Get the Query::Result, not the Hash
        results[idx] = query.result
      end

      results
    rescue SystemStackError => err
      queries.map do |query|
        schema.query_stack_error(query, err)
        query.result_values ||= { "errors" => query.context.errors.map(&:to_h) }
        query.result
      end
    rescue Exception
      # TODO rescue at a higher level so it will catch errors in analysis, too
      # Assign values here so that the query's `@executed` becomes true
      queries.map { |q| q.result_values ||= {} }
      raise
    ensure
      queries.map { |query|
        runtime = query.context.namespace(:interpreter_runtime)[:runtime]
        if runtime
          runtime.delete_all_interpreter_context
        end
      }
    end
  end
ensure
  Fiber[:__graphql_current_multiplex] = previous_multiplex
end