JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Add a hook for retrieving ActiveRecord fragments for included relationships

Open
#958 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

For example, if you have a resource UserResource with a method like this

def friends
   @model.friends.size
end

It will load user.friends separately for each model in the list. So you can optimise the query like this:

class self.records
    _model_class.all.includes(:friends)
end

Then when requesting GET /users/ it now works well. However, say if you request something like GET /groups/?include=users, the records method on UserResource is never called, so there is no query optimisation.

Instead of overriding self.apply_includes(records, options = {}) on each resource that has a relationship to UserResource, it would be nice to have a way to do this only once per resource.

@lgebhardt as discussed on Gitter

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how GET /groups/?include=users reaches UserResource, and compare the records and apply_includes paths described in the issue. The work is done when one resource-level hook can optimize included relationships without overriding apply_includes on every related resource, while avoiding separate queries for each model.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend, database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.