-
Notifications
You must be signed in to change notification settings - Fork 140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow stats to be nested_on resources #239
base: master
Are you sure you want to change the base?
Conversation
b62ffce
to
572b5c1
Compare
572b5c1
to
819f697
Compare
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
end Then the |
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.
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.