Skip to content

Commit

Permalink
Fix custom trace mode using default base class
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Nov 1, 2023
1 parent 4c6e804 commit 99a2faa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def trace_class(new_class = nil)
end

# @return [Class] Return the trace class to use for this mode, looking one up on the superclass if this Schema doesn't have one defined.
def trace_class_for(mode)
def trace_class_for(mode, build: true)
own_trace_modes[mode] ||
(superclass.respond_to?(:trace_class_for) ? superclass.trace_class_for(mode) : (own_trace_modes[mode] = build_trace_mode(mode)))
(superclass.respond_to?(:trace_class_for) ? superclass.trace_class_for(mode, build: build) : (build ? (own_trace_modes[mode] = build_trace_mode(mode)) : nil))
end

# Configure `trace_class` to be used whenever `context: { trace_mode: mode_name }` is requested.
Expand Down Expand Up @@ -218,7 +218,7 @@ def build_trace_mode(mode)
else
# First, see if the superclass has a custom-defined class for this.
# Then, if it doesn't, use this class's default trace
base_class = (superclass.respond_to?(:trace_class_for) && superclass.trace_class_for(mode)) || trace_class_for(:default)
base_class = (superclass.respond_to?(:trace_class_for) && superclass.trace_class_for(mode, build: false)) || trace_class_for(:default)
# Prepare the default trace class if it hasn't been initialized yet
base_class ||= (own_trace_modes[:default] = build_trace_mode(:default))
mods = trace_modules_for(mode)
Expand Down
25 changes: 25 additions & 0 deletions spec/graphql/tracing/trace_modes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ def execute_query(query:)
end
end

describe "inheriting from a custom default trace class" do
class CustomBaseTraceParentSchema < GraphQL::Schema
class CustomTrace < GraphQL::Tracing::Trace
end

trace_class CustomTrace
end

class CustomBaseTraceSubclassSchema < CustomBaseTraceParentSchema
trace_with Module.new, mode: :special_with_base_class
end

it "uses the default trace class for default mode" do
assert_equal CustomBaseTraceParentSchema::CustomTrace, CustomBaseTraceParentSchema.trace_class_for(:default)
assert_equal CustomBaseTraceParentSchema::CustomTrace, CustomBaseTraceSubclassSchema.trace_class_for(:default).superclass

assert_instance_of CustomBaseTraceParentSchema::CustomTrace, CustomBaseTraceParentSchema.new_trace
assert_kind_of CustomBaseTraceParentSchema::CustomTrace, CustomBaseTraceSubclassSchema.new_trace
end

it "uses the default trace class for special modes" do
assert_includes CustomBaseTraceSubclassSchema.trace_class_for(:special_with_base_class).ancestors, CustomBaseTraceParentSchema::CustomTrace
assert_kind_of CustomBaseTraceParentSchema::CustomTrace, CustomBaseTraceSubclassSchema.new_trace(mode: :special_with_base_class)
end
end

describe "custom default trace mode" do
class CustomDefaultSchema < TraceModesTest::ParentSchema
Expand Down

0 comments on commit 99a2faa

Please sign in to comment.