diff --git a/src/Abstracts/Repositories/Repository.php b/src/Abstracts/Repositories/Repository.php index eb4a8dd2..1d1aa594 100644 --- a/src/Abstracts/Repositories/Repository.php +++ b/src/Abstracts/Repositories/Repository.php @@ -30,15 +30,15 @@ public function boot() { parent::boot(); - if ($this->includesEagerLoadingEnabled()) { - $this->eagerLoadRequestedRelations(); + if ($this->shouldEagerLoadIncludes()) { + $this->eagerLoadRequestedIncludes(); } } /** * Enable or disable eager loading of relations requested by the client via "include" query parameter. */ - public function includesEagerLoadingEnabled(): bool + public function shouldEagerLoadIncludes(): bool { // TODO: BC: disable it by default for v8 and enable by default for v13 return false; diff --git a/src/Services/Response.php b/src/Services/Response.php index ab067b4b..4c663683 100644 --- a/src/Services/Response.php +++ b/src/Services/Response.php @@ -24,7 +24,7 @@ class Response extends Fractal * For example, if the include query parameter is "books,children.books", this method will return: * ['books', 'children', 'children.books'] */ - public static function getRequestedIncludesAsModelRelation(): array + public static function getRequestedIncludes(): array { $requestedIncludes = request()?->input(config('fractal.auto_includes.request_key'), []); @@ -59,6 +59,10 @@ public function createData(): Scope $this->withResourceName($this->defaultResourceName()); $this->setAvailableIncludesMeta(); + // TODO: enable this and remove everything below + // After the Fractalistic PR's are accepted + // return parent::createData(); + if (is_null($this->transformer)) { throw new NoTransformerSpecified(); } diff --git a/src/Traits/CanEagerLoadTrait.php b/src/Traits/CanEagerLoadTrait.php index a0317e34..bad9e111 100644 --- a/src/Traits/CanEagerLoadTrait.php +++ b/src/Traits/CanEagerLoadTrait.php @@ -18,14 +18,15 @@ trait CanEagerLoadTrait * * @see https://apiato.atlassian.net/browse/API-905 */ - protected function eagerLoadRequestedRelations(): void + protected function eagerLoadRequestedIncludes(): void { $this->scopeQuery(function (Builder|Model $model) { if (request()?->has(config('fractal.auto_includes.request_key'))) { $validIncludes = []; // TODO: Do we need to do the same for the excludes? // TODO: Or default includes! Are they eager loaded by default? - foreach (Response::getRequestedIncludesAsModelRelation() as $includeName) { + // TODO: What if the include has parameters? e.g. include=books:limit(5|3) + foreach (Response::getRequestedIncludes() as $includeName) { $relationParts = explode('.', $includeName); $camelCasedIncludeName = $this->filterInvalidRelations($this->model, $relationParts); if ($camelCasedIncludeName) { diff --git a/tests/Unit/Abstracts/Repositories/RepositoryTest.php b/tests/Unit/Abstracts/Repositories/RepositoryTest.php index edbf1cbf..b5f5affb 100644 --- a/tests/Unit/Abstracts/Repositories/RepositoryTest.php +++ b/tests/Unit/Abstracts/Repositories/RepositoryTest.php @@ -75,7 +75,7 @@ public function testEagerLoadSingleRelationRequestedViaRequest( )->has(BookFactory::new()->count(3)) ->createOne(); $repository = new class(app()) extends UserRepository { - public function includesEagerLoadingEnabled(): bool + public function shouldEagerLoadIncludes(): bool { return true; } @@ -108,7 +108,7 @@ public function testMultipleEagerLoadAppliesAllEagerLoads(): void )->has(BookFactory::new()->count(3)) ->createOne(); $repository = new class(app()) extends UserRepository { - public function includesEagerLoadingEnabled(): bool + public function shouldEagerLoadIncludes(): bool { return true; } diff --git a/tests/Unit/Services/ResponseTest.php b/tests/Unit/Services/ResponseTest.php index acc0281d..69775350 100644 --- a/tests/Unit/Services/ResponseTest.php +++ b/tests/Unit/Services/ResponseTest.php @@ -397,7 +397,7 @@ public function testCanGetRequestedIncludes(): void { request()->merge(['include' => 'books,children.books']); - $result = Response::getRequestedIncludesAsModelRelation(); + $result = Response::getRequestedIncludes(); $this->assertEquals(['books', 'children', 'children.books'], $result); }