Skip to content

Commit

Permalink
Use trace_with to start and end batches
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Jan 15, 2024
1 parent 8f5b102 commit 81bb4dc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/graphql/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def self.batch(executor_class: GraphQL::Batch::Executor)
end

def self.use(schema_defn, executor_class: GraphQL::Batch::Executor)
instrumentation = GraphQL::Batch::SetupMultiplex.new(schema_defn, executor_class: executor_class)
schema_defn.instrument(:multiplex, instrumentation)
if schema_defn.respond_to?(:trace_with)
schema_defn.trace_with(GraphQL::Batch::SetupMultiplex::Trace, executor_class: executor_class)
else
instrumentation = GraphQL::Batch::SetupMultiplex.new(schema_defn, executor_class: executor_class)
schema_defn.instrument(:multiplex, instrumentation)
end

if schema_defn.mutation
require_relative "batch/mutation_field_extension"
Expand Down
14 changes: 14 additions & 0 deletions lib/graphql/batch/setup_multiplex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,19 @@ def before_multiplex(multiplex)
def after_multiplex(multiplex)
GraphQL::Batch::Executor.end_batch
end

module Trace
def initialize(executor_class:, **_rest)
@executor_class = executor_class
super
end

def execute_multiplex(multiplex:)
GraphQL::Batch::Executor.start_batch(@executor_class)
super
ensure
GraphQL::Batch::Executor.end_batch
end
end
end
end

0 comments on commit 81bb4dc

Please sign in to comment.