From e480c701b47f6dec131b619af3a2e23a6494bcd7 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Tue, 20 Aug 2024 10:20:59 -0400 Subject: [PATCH] Call super in NotificationsTrace --- lib/graphql/tracing/notifications_trace.rb | 4 +- .../tracing/notifications_trace_spec.rb | 47 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/graphql/tracing/notifications_trace.rb b/lib/graphql/tracing/notifications_trace.rb index 6fc8c6861c..406068aa53 100644 --- a/lib/graphql/tracing/notifications_trace.rb +++ b/lib/graphql/tracing/notifications_trace.rb @@ -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 diff --git a/spec/graphql/tracing/notifications_trace_spec.rb b/spec/graphql/tracing/notifications_trace_spec.rb index 25d25190e6..50c42283e8 100644 --- a/spec/graphql/tracing/notifications_trace_spec.rb +++ b/spec/graphql/tracing/notifications_trace_spec.rb @@ -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', @@ -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