Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom trace mode using default base class #4693

Merged
merged 1 commit into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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