[13.x] Fix N+1 queries when including nested JSON:API relationships

Merged
XU
19d ago
+77-0

Since #61322, nested includes run one query per related model:

GET /api/users/1?include=posts.comments.author

For a user with 20 posts and 5 comments on each, this runs 122 queries. Before #61322 it was 4.

This loads the requested relationships for the whole related collection at once, instead of one model at a time. Only relationships defined on the related resource are loaded, so includes like comments.truncate are still ignored.

The response is the same, only the number of queries changes.