Class: GraphQL::Execution::Multiplex Private
- Inherits:
-
Object
- Object
- GraphQL::Execution::Multiplex
- Includes:
- Tracing::Traceable
- Defined in:
- lib/graphql/execution/multiplex.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Execute multiple queries under the same multiplex “umbrella”. They can share a batching context and reduce redundant database hits.
The flow is:
- Multiplex instrumentation setup
- Query instrumentation setup
- Analyze the multiplex + each query
- Begin each query
- Resolve lazy values, breadth-first across all queries
- Finish each query (eg, get errors)
- Query instrumentation teardown
- Multiplex instrumentation teardown
If one query raises an application error, all queries will be in undefined states.
Validation errors and GraphQL::ExecutionErrors are handled in isolation: one of these errors in one query will not affect the other queries.
Constant Summary collapse
- NO_OPERATION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Used internally to signal that the query shouldn’t be executed
{}.freeze
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
private
-
#dataloader ⇒ Object
readonly
private
-
#max_complexity ⇒ Object
readonly
private
-
#queries ⇒ Object
readonly
private
-
#schema ⇒ Object
readonly
private
Class Method Summary collapse
-
.begin_query(results, idx, query, multiplex) ⇒ Object
private
-
.run_all(schema, query_options, context: {}, max_complexity: schema.max_complexity) ⇒ Array<Hash>
private
One result per query.
Instance Method Summary collapse
-
#initialize(schema:, queries:, context:, max_complexity:) ⇒ Multiplex
constructor
private
A new instance of Multiplex.
Methods included from Tracing::Traceable
Constructor Details
#initialize(schema:, queries:, context:, max_complexity:) ⇒ Multiplex
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Multiplex.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/graphql/execution/multiplex.rb', line 33 def initialize(schema:, queries:, context:, max_complexity:) @schema = schema @queries = queries @queries.each { |q| q.multiplex = self } @context = context @dataloader = @context[:dataloader] ||= @schema.dataloader_class.new @tracers = schema.tracers + (context[:tracers] || []) # Support `context: {backtrace: true}` if context[:backtrace] && !@tracers.include?(GraphQL::Backtrace::Tracer) @tracers << GraphQL::Backtrace::Tracer end @max_complexity = max_complexity end |
Instance Attribute Details
#context ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/graphql/execution/multiplex.rb', line 32 def context @context end |
#dataloader ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/graphql/execution/multiplex.rb', line 32 def dataloader @dataloader end |
#max_complexity ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/graphql/execution/multiplex.rb', line 32 def max_complexity @max_complexity end |
#queries ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/graphql/execution/multiplex.rb', line 32 def queries @queries end |
#schema ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
32 33 34 |
# File 'lib/graphql/execution/multiplex.rb', line 32 def schema @schema end |
Class Method Details
.begin_query(results, idx, query, multiplex) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/graphql/execution/multiplex.rb', line 112 def begin_query(results, idx, query, multiplex) operation = query.selected_operation result = if operation.nil? || !query.valid? || query.context.errors.any? NO_OPERATION else begin query.schema.query_execution_strategy.begin_query(query, multiplex) rescue GraphQL::ExecutionError => err query.context.errors << err NO_OPERATION end end results[idx] = result nil end |
.run_all(schema, query_options, context: {}, max_complexity: schema.max_complexity) ⇒ Array<Hash>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns One result per query.
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 |
# File 'lib/graphql/execution/multiplex.rb', line 53 def run_all(schema, , context: {}, max_complexity: schema.max_complexity) queries = .map do |opts| case opts when Hash GraphQL::Query.new(schema, nil, **opts) when GraphQL::Query opts else raise "Expected Hash or GraphQL::Query, not #{opts.class} (#{opts.inspect})" end end multiplex = self.new(schema: schema, queries: queries, context: context, max_complexity: max_complexity) multiplex.trace("execute_multiplex", { multiplex: multiplex }) do GraphQL::Execution::Instrumentation.apply_instrumenters(multiplex) do schema = multiplex.schema multiplex_analyzers = schema.multiplex_analyzers if multiplex.max_complexity multiplex_analyzers += [GraphQL::Analysis::AST::MaxQueryComplexity] end schema.analysis_engine.analyze_multiplex(multiplex, multiplex_analyzers) begin multiplex.schema.query_execution_strategy.begin_multiplex(multiplex) # Do as much eager evaluation of the query as possible results = [] queries.each_with_index do |query, idx| multiplex.dataloader.append_job { begin_query(results, idx, query, multiplex) } end multiplex.dataloader.run # Then, work through lazy results in a breadth-first way multiplex.dataloader.append_job { multiplex.schema.query_execution_strategy.finish_multiplex(results, multiplex) } multiplex.dataloader.run # Then, find all errors and assign the result to the query object results.each_with_index do |data_result, idx| query = queries[idx] finish_query(data_result, query, multiplex) # Get the Query::Result, not the Hash results[idx] = query.result end results 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 end end end end |