Skip to content

Commit

Permalink
Build default trace modes as late as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Oct 12, 2023
1 parent 9e80a48 commit ef53f44
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,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, build: false)
def trace_class_for(mode)
own_trace_modes[mode] ||
(superclass.respond_to?(:trace_class_for) ? superclass.trace_class_for(mode) : nil)
(superclass.respond_to?(:trace_class_for) ? superclass.trace_class_for(mode) : (own_trace_modes[mode] = build_trace_mode(mode)))
end

# Configure `trace_class` to be used whenever `context: { trace_mode: mode_name }` is requested.
Expand Down Expand Up @@ -921,7 +921,6 @@ def resolve_type(type, obj, ctx)

def inherited(child_class)
if self == GraphQL::Schema
child_class.own_trace_modes[:default] = child_class.build_trace_mode(:default)
child_class.directives(default_directives.values)
end
# Make sure the child class has these built out, so that
Expand Down
7 changes: 5 additions & 2 deletions spec/graphql/tracing/trace_modes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ def execute_query(query:)

# Initialize the trace class, make sure no legacy tracers are present at this point:
refute_includes child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers

tracer_class = Class.new
# add a legacy tracer
GraphQL::Schema.tracer(Class.new)
GraphQL::Schema.tracer(tracer_class)
# A newly created child class gets the right setup:
new_child_class = Class.new(GraphQL::Schema)
assert_includes new_child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers
# But what about an already-created child class?
assert_includes child_class.trace_class_for(:default).ancestors, GraphQL::Tracing::CallLegacyTracers
ensure
GraphQL::Schema.send(:own_tracers).delete(tracer_class)
GraphQL::Schema.own_trace_modes.delete(:default)
end
end

Expand Down

0 comments on commit ef53f44

Please sign in to comment.