Skip to content

Commit

Permalink
Call super in NotificationsTrace
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Aug 20, 2024
1 parent 951cb16 commit e480c70
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/graphql/tracing/notifications_trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def initialize(engine:, **rest)
"resolve_type_lazy" => "resolve_type.graphql",
}.each do |trace_method, platform_key|
module_eval <<-RUBY, __FILE__, __LINE__
def #{trace_method}(**metadata, &blk)
@notifications_engine.instrument("#{platform_key}", metadata, &blk)
def #{trace_method}(**metadata, &block)
@notifications_engine.instrument("#{platform_key}", metadata) { super(**metadata, &block) }
end
RUBY
end
Expand Down
47 changes: 30 additions & 17 deletions spec/graphql/tracing/notifications_trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,40 @@ def int
end
end

class DummyEngine
def self.dispatched_events
@dispatched_events ||= []
end

def self.instrument(event, payload)
dispatched_events << [event, payload]
yield if block_given?
end
end

module OtherTrace
def execute_query(query:)
query.context[:other_trace_ran] = true
super
end
end

class Schema < GraphQL::Schema
query Query
trace_with OtherTrace
trace_with GraphQL::Tracing::NotificationsTrace, engine: DummyEngine
end
end

describe "Observing" do
it "dispatches the event to the notifications engine with suffixed key" do
dispatched_events = trigger_fake_notifications_tracer(NotificationsTraceTest::Schema).to_h
before do
NotificationsTraceTest::DummyEngine.dispatched_events.clear
end

assert dispatched_events.length > 0

describe "Observing" do
it "dispatches the event to the notifications engine with suffixed key" do
NotificationsTraceTest::Schema.execute "query X { int }"
dispatched_events = NotificationsTraceTest::DummyEngine.dispatched_events.to_h
expected_event_keys = [
'execute_multiplex.graphql',
'analyze_multiplex.graphql',
Expand All @@ -43,20 +66,10 @@ class Schema < GraphQL::Schema
assert payload.is_a?(Hash)
end
end
end

def trigger_fake_notifications_tracer(schema)
dispatched_events = []
engine = Object.new

engine.define_singleton_method(:instrument) do |event, payload, &blk|
dispatched_events << [event, payload]
blk.call if blk
it "works with other tracers" do
res = NotificationsTraceTest::Schema.execute "query X { int }"
assert res.context[:other_trace_ran]
end

schema.trace_with GraphQL::Tracing::NotificationsTrace, engine: engine
schema.execute "query X { int }"

dispatched_events
end
end

0 comments on commit e480c70

Please sign in to comment.