Skip to content

Commit

Permalink
add includes ending with _count as relationships counts
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Dec 14, 2023
1 parent 38eab23 commit c51e220
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/Http/ApplyFieldsToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class ApplyFieldsToQuery implements HandlesRequestQueries
/**
* Apply modifications to the query based on allowed query fragments.
*
* @param \OpenSoutheners\LaravelApiable\Http\RequestQueryObject $request
* @param \Closure(\OpenSoutheners\LaravelApiable\Http\RequestQueryObject): \Illuminate\Database\Eloquent\Builder $next
* @param \Closure(\OpenSoutheners\LaravelApiable\Http\RequestQueryObject): \Illuminate\Database\Eloquent\Builder $next
* @return \Illuminate\Database\Eloquent\Builder
*/
public function from(RequestQueryObject $request, Closure $next)
Expand Down
15 changes: 8 additions & 7 deletions src/Http/ApplyIncludesToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use OpenSoutheners\LaravelApiable\Contracts\HandlesRequestQueries;

class ApplyIncludesToQuery implements HandlesRequestQueries
{
/**
* Apply modifications to the query based on allowed query fragments.
*
* @param \OpenSoutheners\LaravelApiable\Http\RequestQueryObject $request
* @param \Closure(\OpenSoutheners\LaravelApiable\Http\RequestQueryObject): \Illuminate\Database\Eloquent\Builder $next
* @param \Closure(\OpenSoutheners\LaravelApiable\Http\RequestQueryObject): \Illuminate\Database\Eloquent\Builder $next
* @return \Illuminate\Database\Eloquent\Builder
*/
public function from(RequestQueryObject $request, Closure $next)
Expand All @@ -32,17 +32,18 @@ public function from(RequestQueryObject $request, Closure $next)
/**
* Apply array of includes to the query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $includes
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function applyIncludes(Builder $query, array $includes)
{
$eagerLoadedRelationships = $query->getEagerLoads();
$includes = array_filter($includes, fn ($include) => ! in_array($include, $eagerLoadedRelationships));

if (! empty($includes)) {
$query->with($includes);
foreach ($includes as $include) {
match (true) {
Str::endsWith($include, '_count') => $query->withCount(str_replace('_count', '', $include)),
! in_array($include, $eagerLoadedRelationships) => $query->with($include),
default => null
};
}

return $query;
Expand Down
24 changes: 20 additions & 4 deletions tests/Http/JsonApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public function testSortingBelongsToManyRelationshipFieldAsAscendant()

$response->assertJsonApi(function (AssertableJsonApi $assert) {
$assert->isCollection();

$assert->at(0)->hasAttribute('title', 'Hola mundo');
$assert->at(1)->hasAttribute('title', 'My first test');
$assert->hasAttribute('tags_count');
Expand All @@ -280,7 +280,7 @@ public function testSortingBelongsToManyRelationshipFieldAsDescendant()

$response->assertJsonApi(function (AssertableJsonApi $assert) {
$assert->isCollection();

$assert->at(0)->hasAttribute('title', 'Hello world');
$assert->at(1)->hasAttribute('title', 'Y esto en español');
$assert->hasAttribute('tags_count');
Expand All @@ -300,7 +300,7 @@ public function testSortingBelongsToRelationshipFieldAsAscendant()

$response->assertJsonApi(function (AssertableJsonApi $assert) {
$assert->isCollection();

$assert->at(0)->hasAttribute('title', 'My first test');
$assert->at(1)->hasAttribute('title', 'Y esto en español');
});
Expand All @@ -319,7 +319,7 @@ public function testSortingBelongsToRelationshipFieldAsDescendant()

$response->assertJsonApi(function (AssertableJsonApi $assert) {
$assert->isCollection();

$assert->at(0)->hasAttribute('title', 'Hello world');
$assert->at(1)->hasAttribute('title', 'Y esto en español');
});
Expand Down Expand Up @@ -439,4 +439,20 @@ public function testResponseWithModifiedQueryWithCountMethodGetsRelationshipsCou
->hasAttribute('tags_count')
);
}

public function testResponseWithAllowedIncludedEndsWithCountGetsRelationshipCountAsAttribute()
{
Route::get('/', function () {
return response()->json(
JsonApiResponse::from(Post::class)->allowInclude(['tags_count'])
);
});

$response = $this->get('/?include=tags_count', ['Accept' => 'application/vnd.api+json']);

$response->assertJsonApi(fn (AssertableJsonApi $assert) => $assert
->isCollection()
->hasAttribute('tags_count')
);
}
}

0 comments on commit c51e220

Please sign in to comment.