Skip to content

Commit 96e0f20

Browse files
Lee Richmondrichmolj
authored andcommitted
Don't blow up when missing/unexpected context
1 parent 5230f44 commit 96e0f20

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lib/graphiti/sideload.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,16 @@ def link_filter(parents)
137137
end
138138

139139
def link_extra_fields
140+
return unless context&.respond_to?(:params)
141+
140142
extra_fields_name = [association_name, resource.type].find { |param|
141143
context.params.dig(:extra_fields, param)
142144
}
143145

144-
{resource.type => context.params.dig(:extra_fields, extra_fields_name)} if extra_fields_name
146+
if extra_fields_name
147+
extra_fields = context.params.dig(:extra_fields, extra_fields_name)
148+
{resource.type => extra_fields}
149+
end
145150
end
146151

147152
# The parent resource is a remote,

lib/graphiti/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Graphiti
2-
VERSION = "1.2.39"
2+
VERSION = "1.2.40"
33
end

spec/extra_fields_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,45 @@ def attributes
2929
.to match_array(%w[first_name last_name age stack_ranking])
3030
end
3131

32+
context "when sideloading/linking" do
33+
before do
34+
allow_any_instance_of(PORO::Employee)
35+
.to receive(:linked_positions) { [] }
36+
resource.has_many :linked_positions,
37+
resource: PORO::PositionResource,
38+
link: true
39+
params[:include] = "linked_positions"
40+
end
41+
42+
context "and the context is missing" do
43+
around do |e|
44+
Graphiti.with_context nil do
45+
e.run
46+
end
47+
end
48+
49+
it "does not blow up" do
50+
params[:extra_fields] = {positions: "foo"}
51+
expect { render }.to_not raise_error
52+
end
53+
end
54+
55+
context "and the context does not respond to params" do
56+
around do |e|
57+
# current_user referenced in resource fixtures
58+
ctx = Struct.new(:current_user).new
59+
Graphiti.with_context ctx do
60+
e.run
61+
end
62+
end
63+
64+
it "does not blow up" do
65+
params[:extra_fields] = {positions: "foo"}
66+
expect { render }.to_not raise_error
67+
end
68+
end
69+
end
70+
3271
context "when multiple extra attributes" do
3372
context "added with blocks" do
3473
before do

0 commit comments

Comments
 (0)