Skip to content

Commit

Permalink
Merge pull request #187 from zpencerq/fix/sideload_clear
Browse files Browse the repository at this point in the history
Clear Sideload resources after context block returns
  • Loading branch information
richmolj authored Oct 30, 2019
2 parents 237d388 + 46453e7 commit ac783d0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/graphiti.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/graphiti/sideload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/sideloading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ac783d0

Please sign in to comment.