diff --git a/lib/graphiti.rb b/lib/graphiti.rb index c4806b27..fc5659d8 100644 --- a/lib/graphiti.rb +++ b/lib/graphiti.rb @@ -32,6 +32,10 @@ def self.with_context(obj, namespace = nil) yield ensure self.context = prior + + resources.each { |resource_class| + resource_class.sideloads.values.each(&:clear_resources) + } end def self.config diff --git a/lib/graphiti/sideload.rb b/lib/graphiti/sideload.rb index 62bfa28e..2d99444e 100644 --- a/lib/graphiti/sideload.rb +++ b/lib/graphiti/sideload.rb @@ -224,6 +224,12 @@ def parent_resource @parent_resource ||= parent_resource_class.new end + # See https://github.com/graphiti-api/graphiti/issues/186 + def clear_resources + @resource = nil + @parent_resource = nil + end + def assign(parents, children) track_associated = type == :has_one associated = [] if track_associated diff --git a/spec/sideloading_spec.rb b/spec/sideloading_spec.rb index 093f9a23..e676d7ef 100644 --- a/spec/sideloading_spec.rb +++ b/spec/sideloading_spec.rb @@ -1044,6 +1044,21 @@ def department render }.to_not raise_error end + + describe "across requests" do + it "uses a different sideloaded resource" do + ctx = double(current_user: :admin) + sl1 = Graphiti.with_context ctx do + resource.all(params).query.sideloads.values[0].resource + end + + sl2 = Graphiti.with_context ctx do + resource.all(params).query.sideloads.values[0].resource + end + + expect(sl1).to_not be sl2 + end + end end context "when a required filter on the sideloaded resource" do