Skip to content

Expand/Collapse Fields #36

@pmdevita

Description

@pmdevita

Is your feature request related to a problem? Please describe.

One of the more recent API patterns I've seen is expand/collapse, where you can request an API to return a relation on a Schema that it normally would exclude for query performance.

GET /book/123

{
  "id": 123,
  "name": "Pride and Prejudice"
}
GET /book/123?expand=author

{
  "id": 123,
  "name": "Pride and Prejudice",
  "author": {
    "id": 456
     "name": "Jane Austen"
  }
}

It would be nice if Shinobi provided assistance for expanding/collapsing fields in Schema responses. Manually writing the logic for this on each endpoint that uses such a schema would make the pattern difficult to adopt and maintain.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Generally, if we're going to fetch across a relation in the first place, the user already has to specify what schema it is that will be attached.

class BookSchema(ModelSchema):
    author: AuthorSchema

    class Meta:
        model = Book
        fields = ["id", "name"]

I think it may be possible to create a type annotation that can mark this as something to expand.

class BookSchema(ModelSchema):
    author: Expand[AuthorSchema]

    class Meta:
        model = Book
        fields = ["id", "name"]

During boot and setup of the API endpoints, Shinobi can then inspect a response schema for it's expand fields (and the children of those fields as well for nested expansion) and attach those as query params to the endpoint. We already have to process all of a Schema's fields in its Metaclass so I figure we just solve it there too.

Finally, during validation from ORM to Schema, Shinobi would take the given expand params and either include or exclude those fields in the response. I think this can maybe just get passed through with context but we'll have to see, I'm hoping it doesn't slow down validation in large lists/pagination.

Alternatively, if this is on pagination, Shinobi could manipulate the query to block the loading of non-included fields (.prefetch_related(Prefetch("author", Author.objects.none())), this is a bit more risky depending on use but it would perform better. Shinobi should not do the opposite and prefetch for you though.

Collapse would work just opposite of Expand, it would be a field that would be normally included but could be optionally excluded.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions