Skip to content

Commit ced743e

Browse files
CGA1123richmolj
authored andcommitted
Propagate nested extra_fields to links
Propagates the `extra_fields` attributes for a nested resource to the relationship links created. e.g. `GET http://example.com/books/1?extra_fields[authors]=biography` should return a link to the `author` looking like `http://example.com/authors/2?extra_fields[authors]=biography`, propagating the `extra_fields` that apply for the nexted `authors` resource. Closes #328
1 parent 1f76a61 commit ced743e

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22

33
Features:
4+
- [329](https://github.com/graphiti-api/graphiti/pull/329) Propagate `extra_fields` to related resource links.
45
- [242](https://github.com/graphiti-api/graphiti/pull/242) Bump `jsonapi-renderer` to `~0.2.2` now that (https://github.com/jsonapi-rb/jsonapi-renderer/pull/36) is fixed.
56
- [158](https://github.com/graphiti-api/graphiti/pull/158) Filters options `allow_nil: true`
67
Option can be set at the resource level `Resource.filters_accept_nil_by_default = true`.

lib/graphiti/sideload.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ def link_filter(parents)
136136
base_filter(parents)
137137
end
138138

139+
def link_extra_fields
140+
extra_fields_name = [association_name, resource.type].find { |param|
141+
context.params.dig(:extra_fields, param)
142+
}
143+
144+
{resource.type => context.params.dig(:extra_fields, extra_fields_name)} if extra_fields_name
145+
end
146+
139147
# The parent resource is a remote,
140148
# AND the sideload is a remote to the same endpoint
141149
def shared_remote?

lib/graphiti/util/link.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def params
6363
params[:filter] = @sideload.link_filter([@model])
6464
end
6565

66+
if (extra_fields = @sideload.link_extra_fields)
67+
params[:extra_fields] ||= extra_fields
68+
end
69+
6670
@sideload.params_proc&.call(params, [@model], context)
6771
end
6872
end

spec/serialization_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,66 @@ def positions
13221322
end
13231323
end
13241324

1325+
context "with nested extra attribute" do
1326+
context "when referencing the type" do
1327+
let!(:employee) { PORO::Employee.create(classification_id: 789) }
1328+
1329+
before do
1330+
resource.belongs_to :classification
1331+
params[:extra_fields] = {classifications: "description"}
1332+
end
1333+
1334+
def classification
1335+
json["data"][0]["relationships"]["classification"]
1336+
end
1337+
1338+
it "links correctly" do
1339+
render
1340+
expect(classification["links"]["related"])
1341+
.to eq("/poro/classifications/789?extra_fields[classifications]=description")
1342+
end
1343+
end
1344+
1345+
context "when referencing the association name" do
1346+
before do
1347+
resource.has_many :positions, as: :senior_positions
1348+
params[:extra_fields] = {senior_positions: "score"}
1349+
end
1350+
1351+
it "links correctly" do
1352+
render
1353+
expect(positions["links"]["related"])
1354+
.to eq("/poro/positions?extra_fields[positions]=score&filter[employee_id]=1")
1355+
end
1356+
end
1357+
1358+
context "when referencing a polymorphic_belongs_to" do
1359+
let!(:employee) { PORO::Employee.create(credit_card_id: 1123, credit_card_type: "Visa") }
1360+
1361+
before do
1362+
resource.polymorphic_belongs_to :credit_card do
1363+
group_by(:credit_card_type) do
1364+
on(:Visa)
1365+
on(:GoldVisa)
1366+
on(:Mastercard)
1367+
end
1368+
end
1369+
1370+
params[:extra_fields] = {credit_card: "foo"}
1371+
end
1372+
1373+
def credit_card
1374+
json["data"][0]["relationships"]["credit_card"]
1375+
end
1376+
1377+
it "links correctly" do
1378+
render
1379+
expect(credit_card["links"]["related"])
1380+
.to eq("/poro/visas/1123?extra_fields[visas]=foo")
1381+
end
1382+
end
1383+
end
1384+
13251385
context "with runtime params" do
13261386
xit "links correctly" do
13271387
end

0 commit comments

Comments
 (0)