lib/graphql/execution/multiplex.rb in graphql-1.12.3 vs lib/graphql/execution/multiplex.rb in graphql-1.12.4

- old
+ new

@@ -33,11 +33,11 @@ def initialize(schema:, queries:, context:, max_complexity:) @schema = schema @queries = queries @queries.each { |q| q.multiplex = self } @context = context - @context[:dataloader] = @dataloader = @schema.dataloader_class.new(context) + @context[:dataloader] = @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 @@ -72,28 +72,44 @@ end end end end + # @param query [GraphQL::Query] + 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 + # These were checked to be the same in `#supports_multiplexing?` + 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 + private def run_as_multiplex(multiplex) multiplex.schema.query_execution_strategy.begin_multiplex(multiplex) queries = multiplex.queries # Do as much eager evaluation of the query as possible results = [] queries.each_with_index do |query, idx| - multiplex.dataloader.enqueue { - results[idx] = begin_query(query, multiplex) - } + 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.enqueue { + 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 @@ -108,26 +124,9 @@ 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 - - # @param query [GraphQL::Query] - # @return [Hash] The initial result (may not be finished if there are lazy values) - def begin_query(query, multiplex) - operation = query.selected_operation - if operation.nil? || !query.valid? || query.context.errors.any? - NO_OPERATION - else - begin - # These were checked to be the same in `#supports_multiplexing?` - query.schema.query_execution_strategy.begin_query(query, multiplex) - rescue GraphQL::ExecutionError => err - query.context.errors << err - NO_OPERATION - end - end end # @param data_result [Hash] The result for the "data" key, if any # @param query [GraphQL::Query] The query which was run # @return [Hash] final result of this query, including all values and errors