Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Allow stats to be nested_on resources #239
Conversation
b62ffce
to
572b5c1
572b5c1
to
819f697
|
Thanks for this @evanrolfe, I definitely love to see the premise of per-resource metadata making its way into graphiti. The code itself looks pretty good, but I'm a little concerned about the interface. The first thing that jumped out to me was that the request is the same, and the server is in charge of rendering at the resource level instead of the overall One way to do this currently in the request is with The other issue I see is with N+1 queries. The way many users (including myself) currently handle this scenario is by creating a separate I guess I like the idea of resource-level metadata more than resource-level stats, specifically, for these reasons. But I'm definitely open to it, would like to get your thoughts on the above. |
|
@richmolj thanks for the thoughts and taking time to consider. For the confusion on overall vs nested stats for the requester, I would suggest that is handled by the name of the stat. We didn't really want to introduce a new parameter for a "different kind of stat" because that felt a little wrong. I don't think that the On the N+1, agree that there may be an issue with some adapters and that it would be easy to fall in to a bad place there... In our particular case, we have our own adapter which is using active resource to pull back data from a third service. This third service returns the nested stats as part of the payload of its response so for us the N+1 is little more than reading a variable in the adapter. Splitting out in to a separate resource would mean, for our case, making a second request to the external resource - which is much more expensive (and adding a caching layer behind the adapter isn't a trivial option for our case). An alternative approach which might address the N+1 in a more comprehensive way could be to do something similar to the Class.new(PORO::EmployeeResource) do
stat age: [:squared], nested_on: :employees do
squared do
calc_all do |scope, attr, context, employees|
# returns a hash of employees => value
Hash[employees.map { |employee| [employee, employee.age * employee.age] }]
end
calc_each do |scope, attr, context, employee|
employee.age * employee.age
end
end
end
endThen the |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Currently, you can only set stats for in the top-level part of the response, but we need to be able to set a meta tag with stats for each individual resource returned in the response i.e.
{ "data": [ { "id": "135", "type": "employee", "attributes": { "name": "Alice Smith", "age": 40, "createdAt": "2020-04-14T09:25:07+00:00" }, "relationships": {}, "meta": { "stats": { "age": { "squared": 1600 } } } }, { "id": "134", "type": "employee", "attributes": { "name": "Bob Smith", "age": 50, "createdAt": "2020-04-14T09:25:07+00:00" }, "relationships": {}, "meta": { "stats": { "age": { "squared": 2500 } } } } ], "meta": {} }This is my first time making any changes to the Graphiti codebase, so please let me know if there is a better way of doing this or if I have missed anything important.
Thanks, Evan.