lib/graphql/execution/multiplex.rb in graphql-1.6.1 vs lib/graphql/execution/multiplex.rb in graphql-1.6.2
- old
+ new
@@ -38,11 +38,31 @@
def run_all(schema, query_options, *rest)
queries = query_options.map { |opts| GraphQL::Query.new(schema, nil, opts) }
run_queries(schema, queries, *rest)
end
+ # @param schema [GraphQL::Schema]
+ # @param queries [Array<GraphQL::Query>]
+ # @param context [Hash]
+ # @param max_complexity [Integer]
+ # @return [Array<Hash>] One result per query
def run_queries(schema, queries, context: {}, max_complexity: nil)
+ has_custom_strategy = schema.query_execution_strategy || schema.mutation_execution_strategy || schema.subscription_execution_strategy
+ if has_custom_strategy
+ if queries.length == 1
+ return [run_one_legacy(schema, queries.first)]
+ else
+ raise ArgumentError, "Multiplexing doesn't support custom execution strategies, run one query at a time instead"
+ end
+ else
+ run_as_multiplex(schema, queries, context: context, max_complexity: max_complexity)
+ end
+ end
+
+ private
+
+ def run_as_multiplex(schema, queries, context:, max_complexity:)
query_instrumenters = schema.instrumenters[:query]
multiplex_instrumenters = schema.instrumenters[:multiplex]
multiplex = self.new(schema: schema, queries: queries, context: context)
# First, run multiplex instrumentation, then query instrumentation for each query
@@ -78,12 +98,10 @@
query_instrumenters.reverse_each { |i| i.after_query(query) }
end
multiplex_instrumenters.reverse_each { |i| i.after_multiplex(multiplex) }
end
- private
-
# @param query [GraphQL::Query]
# @return [Hash] The initial result (may not be finished if there are lazy values)
def begin_query(query)
operation = query.selected_operation
if operation.nil? || !query.valid?
@@ -125,9 +143,27 @@
result["errors"] = error_result
end
result
end
+ end
+
+ # use the old `query_execution_strategy` etc to run this query
+ def run_one_legacy(schema, query)
+ instrumenters = schema.instrumenters[:query]
+ instrumenters.each { |i| i.before_query(query) }
+ query.result = if !query.valid?
+ all_errors = query.validation_errors + query.analysis_errors + query.context.errors
+ if all_errors.any?
+ { "errors" => all_errors.map(&:to_h) }
+ else
+ nil
+ end
+ else
+ GraphQL::Query::Executor.new(query).result
+ end
+ ensure
+ instrumenters.reverse_each { |i| i.after_query(query) }
end
end
end
end
end