Skip to content

Commit

Permalink
Merge pull request #164 from rmosolgo/use-trace-with
Browse files Browse the repository at this point in the history
Use trace_with to start and end batches
  • Loading branch information
swalkinshaw committed Jan 17, 2024
2 parents c5fd954 + 5873e66 commit 1cd22b8
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 1cd22b8

Please sign in to comment.