Skip to content

Avoid DB hits for JSONAPI relationships #1325

Closed
@richmolj

Description

@richmolj

I've been searching through the issues, and this may be relevant to bringing filter to 0.10.x, but I couldn't find anything on this specifically.

If I use JSONAPI adapter, and put has_many :comments in my PostSerializer, there will be a DB hit to fill out all the ids for the comment relationships. Even when no include is specified.

I thought of avoiding this by something like

def comments
  object.comments.loaded ? object.comments : []
end

Unfortunately the key comments will still appear in the response. This means if my client (Ember) has already loaded the post with its comments, then hits this separate endpoint, it will think all the comments for the post have been deleted (versus just not included in the response).

Currently I've been creating a base serializer for every model, then subclassing and adding relationships for each specific endpoint. This not only gets really tedious, but every time I add a relationship I am also specifying includes, which makes me think the default should be to not load relationships unless include is specified. Finally, it makes it quite hard to support an API like /posts/1?include=comments since I need to conditionally select a serializer that actually includes the requested relationships.

Is the correct solution here to wait for filter, do the subclassing-style, or something else? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions