Closed richmolj closed 6 years ago
@wadetandy on top of https://github.com/jsonapi-suite/jsonapi_compliable/pull/113
I like the direction that you're trying to go here, but I've got some reservations about the added complexity, and performance impact; have you considered adding some benchmark code before this goes into master?
Many, many things are going to happen before merging this code to master. The point of this branch is to share my work as it happens.
Use Resource query interface to sideload (by default)
1.0 has the query interface:
Which means we can use this interface for sideloading under-the-hood:
This has a few benefits: the filter logic can be re-used, there are less concepts to understand, and makes jumping between resources with different datastores easier (no custom scope/assign required!).
All the existing (meaning 1.0 existing, not < 1.0) customizations can still be applied if you need to drop to a lower-level. If a
scope
block is given we will avoid this pattern and scope however you'd like. In fact, this is still required for many-to-many relationships, and for complex joins where it doesn't make sense to re-use filters.This does come at the cost of requiring a filterable foreign key. For now, I'd like to avoid adding this implicitly, but with a little sugar to make it easier:
As part of making this work, the
Query
class was fundamentally refactored. We now rely on methods and avoidto_hash
as much as possible.to_hash
is only used to generate the required query for the sideload (in order to support deep queries). In addition, we no longer base things on the jsonapi "type" and now walk the graph (so in the future you can load and filter the same entity differently). You can pass a jsonapi type or a relationship name, though this will go away eventually in favor of?filter[positions.title]
.Finally, a bit of a change to the query interface that impacts rendering. It's now:
Other notes:
assign
. This is because the AR adapter associates records viaa.association(:b).target=
to avoid unexpected AR side-effects. I'm thinking we'll have a special flag for this in the future, something likehas_many :things, assign: :poro
.