Skip to content

Commit

Permalink
Merge pull request #4379 from rmosolgo/tracer-inheritance-fix
Browse files Browse the repository at this point in the history
Fix tracers on parent schema classes
  • Loading branch information
rmosolgo committed Mar 14, 2023
2 parents 64949c2 + 1df8f2f commit ded8d6c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ def trace_class(new_class = nil)
if new_class
@trace_class = new_class
elsif !defined?(@trace_class)
@trace_class = Class.new(GraphQL::Tracing::Trace)
parent_trace_class = if superclass.respond_to?(:trace_class)
superclass.trace_class
else
GraphQL::Tracing::Trace
end
@trace_class = Class.new(parent_trace_class)
end
@trace_class
end
Expand Down Expand Up @@ -955,14 +960,17 @@ def tracers
# @param options [Hash] Keywords that will be passed to the tracing class during `#initialize`
# @return [void]
def trace_with(trace_mod, **options)
@trace_options ||= {}
@trace_options.merge!(options)
trace_options.merge!(options)
trace_class.include(trace_mod)
end

def trace_options
@trace_options ||= superclass.respond_to?(:trace_options) ? superclass.trace_options.dup : {}
end

def new_trace(**options)
if defined?(@trace_options)
options = @trace_options.merge(options)
options = trace_options.merge(options)
end
trace_class.new(**options)
end
Expand Down
35 changes: 35 additions & 0 deletions spec/graphql/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,41 @@ def foobar; 1337; end
end
end

describe ".new_trace" do
module NewTrace1
def initialize(**opts)
@trace_opts = opts
end

attr_reader :trace_opts
end

module NewTrace2
end

it "returns an instance of the configured trace_class with trace_options" do
parent_schema = Class.new(GraphQL::Schema) do
trace_with NewTrace1, a: 1
end

child_schema = Class.new(parent_schema) do
trace_with NewTrace2, b: 2
end

parent_trace = parent_schema.new_trace
assert_equal({a: 1}, parent_trace.trace_opts)
assert_kind_of NewTrace1, parent_trace
refute_kind_of NewTrace2, parent_trace
assert_kind_of GraphQL::Tracing::Trace, parent_trace

child_trace = child_schema.new_trace
assert_equal({a: 1, b: 2}, child_trace.trace_opts)
assert_kind_of NewTrace1, child_trace
assert_kind_of NewTrace2, child_trace
assert_kind_of GraphQL::Tracing::Trace, child_trace
end
end

describe ".possible_types" do
it "returns a single item for objects" do
assert_equal [Dummy::Cheese], Dummy::Schema.possible_types(Dummy::Cheese)
Expand Down
38 changes: 38 additions & 0 deletions spec/graphql/tracing/legacy_trace_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Tracing::LegacyTrace do

it "calls tracers on a parent schema class" do
custom_tracer = Module.new do
def self.trace(key, data)
if key == "execute_query"
data[:query].context[:trace_ran] = true
end
yield
end
end

query_type = Class.new(GraphQL::Schema::Object) do
graphql_name("Query")
field :int, Integer
def int
4
end
end

parent_schema = Class.new(GraphQL::Schema) do
query(query_type)
tracer(custom_tracer)
end

child_schema = Class.new(parent_schema)


res1 = parent_schema.execute("{ int }")
assert_equal true, res1.context[:trace_ran]

res2 = child_schema.execute("{ int }")
assert_equal true, res2.context[:trace_ran]
end
end

0 comments on commit ded8d6c

Please sign in to comment.