Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow relationships to override Eloquent adapter defaults #297

Open
lindyhopchris opened this issue Feb 6, 2019 · 0 comments
Open

Allow relationships to override Eloquent adapter defaults #297

lindyhopchris opened this issue Feb 6, 2019 · 0 comments
Labels

Comments

@lindyhopchris
Copy link
Member

At the moment an Eloquent relation (e.g. has-many) uses the same defaults as when querying a resource index.

For example, if a posts resource has default paging parameters, these will be applied both for GET /posts and GET /users/1/posts.

This is generally great as it is nice and consistent, but there are scenarios where you might want different defaults (or no defaults) for a relationship.

For example, say there's a tags resource. There might be 100s of tags, so when you're reading them from GET /tags it would make sense to have default paging params so that a client does not list all tags at once. However, when doing GET /posts/1/tags realistically there might only be a few to 10s of tags for the post... so it doesn't make sense to always force paging on the relationship endpoint. We therefore need the relationship to remove the default paging params from the tags adapter before using that adapter to process the query.

We therefore need to allow a developer to change the defaults for relationships, for the following:

  • paging
  • sort

An initial concept design would be to define these on the relationship objects. So for example, on the posts adapter:

protected function tags()
{
  return $this->hasMany()->withoutDefaultPaging();
}

Then add an equivalent method to the Eloquent adapter that returns a new instance without default paging - that the relationship can call before using the adapter to query the relationship. It is important that the method returns a new instance because adapters are singletons in the JSON API container.

The methods to add are:

  • withoutDefaultPaging()
  • withDefaultPaging($paging)
  • withoutDefaultSort()
  • withDefaultSort($sort)

Best to call them withDefault* because we are only overriding the defaults... if the client has sent page parameters, the response should be paged even if the default is not to page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant