JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Related resources not backed by an association

Open
#545 4 comments 1 reaction 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

Hello, I'm curious to see if anyone is interested in allowing a resource to have a relationship that isn't necessarily an association to the underlying model. For example:

A Video has_many :tags. I've created an instance method (Video#related_videos) that pulls related videos based on a video's set of tags.

video.related_videos
=> [related_video_1, related_video_2]

What I'm interested in is having the corresponding VideoResource aware that this isn't an association it should be looking for, but rather public method. This would allow the following requests:

videos/:id?include=related_videos
videos/:id/relationships/related_videos

Currently, an InvalidInclude exception will be raised since "related_videos is not a valid relationship of videos." What if we were to pass a flag to relationship / has_many / has_one? Maybe there's a better way, but I was thinking something like:

VideoResource < JSONAPI::Resource
  has_many :related_videos, instance_method: true
end

Would something like this be worth exploring? I'd be happy to work on a PR to support something like this if there's any interest.

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 reviewing the resource relationship declarations (relationship, has_many, and has_one) and the InvalidInclude path described in the issue. Determine how a public method such as Video#related_videos could be represented and exposed through the two example requests; done criteria require agreement on the API and validation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.