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

Include related resources #23

Closed
stfnndrsn opened this issue Oct 17, 2016 · 10 comments
Closed

Include related resources #23

stfnndrsn opened this issue Oct 17, 2016 · 10 comments
Milestone

Comments

@stfnndrsn
Copy link
Contributor

Our app have a fair amount of hasMany relationsships - and we are including them in our json-api responses - resulting eg. 5 queries pr. entity; and with 200 entities we have a bottleneck.

I were thinking about overwrite the EloquentAdapter#find method and do my own service-provide to register with my own class.

But is it the best way to handle it? It seems as a generic problem, and a standardized way of handling relationships.

@lindyhopchris
Copy link
Member

Hi! Where are you getting the bottleneck problem... when encoding the model to JSON API (i.e. via the schema) or when processing something a client has sent you? The EloquentAdapter is only used to process incoming relationships from clients.

If you can give me some more information I can definitely help find a better solution.

@stfnndrsn
Copy link
Contributor Author

My problem is the number of SQL queries (and time spent on doing them)

'transactions' => [
    self::DATA => $resource->transactions,
],

image

@lindyhopchris
Copy link
Member

lindyhopchris commented Oct 18, 2016

Hi!

So writing your own EloquentAdapter won't fix this. The EloquentAdapter is only used to resolve JSON API resource identifiers that are incoming.

What you want to do is use eager loading, which is an Eloquent feature:
https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

E.g. in the JSON API search class for that model:

protected function filter(Builder $builder, Collection $filters)
{
  $builder->with('transactions');
}

You should see that 240x stat change to 1x

@stfnndrsn
Copy link
Contributor Author

👍 I missed that feature :) Thanks!

@lindyhopchris
Copy link
Member

No problem.

@stfnndrsn stfnndrsn reopened this Oct 18, 2016
@stfnndrsn
Copy link
Contributor Author

Hi!

Sorry to disturb you again.

Next issue I have here - what about related resources? We having a pattern here with posts/10/comments and having the hasMany relation on the 'comments' resource.

@lindyhopchris
Copy link
Member

I haven't implemented full related resources support yet - it'll come in a future version.

However, you can do this for the moment. In your PostsController overload the readRelatedResource method with the following:

$model = $this->getRecord($request);
$key = $this->keyForRelationship($request->getRelationshipName());

if ('comments' === $key) {
    $content = $model->comments()->with('...')->get();
} else {
    $content = $model->{$key};
}

return $this
    ->reply()
    ->content($content);

I think you might already be overloading this method according to this issue:
#22

So you just need to add the with() call to what was written in that issue.

@lindyhopchris lindyhopchris added this to the 0.6.0 milestone Oct 18, 2016
@lindyhopchris
Copy link
Member

Leave this issue open as I've tagged it with 0.6.0 milestone so improvements to this get sorted in that release - though the solution mentioned above will work in the meantime.

@lindyhopchris
Copy link
Member

Relationships are now better supported on the 1.x-dev branch, but need to add support for with when querying has-many relationships.

@lindyhopchris
Copy link
Member

With is now fully supported, including on relationships, in the 1.0 branch

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

No branches or pull requests

2 participants