From 0c8de3a75ad2cdc83077017fe2a9a4458dd2484b Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Fri, 15 Jan 2021 08:37:17 +1030 Subject: [PATCH] Remove ability to have custom relationship pre-loaders for now --- docs/relationships.md | 24 ---------- src/Endpoint/Concerns/IncludesData.php | 50 ++++++++++----------- src/Schema/Relationship.php | 9 ++-- tests/feature/RelationshipInclusionTest.php | 5 --- 4 files changed, 26 insertions(+), 62 deletions(-) diff --git a/docs/relationships.md b/docs/relationships.md index 0398657..e6b2ac9 100644 --- a/docs/relationships.md +++ b/docs/relationships.md @@ -67,30 +67,6 @@ $type->hasOne('user') ->dontLoad(); ``` -### Custom Loading Logic - -Instead of using the adapter's eager-loading logic, you may wish to define your own for a relationship. You can do so using the `load` method. - -Beware that this can be complicated as eager-loading always takes place on the set of models at the root level of the document; these are passed as the first parameter. The second parameter is an array of the `Relationship` objects that make up the nested inclusion trail leading to the current relationship. - -So, for example, if a request was made to `GET /categories?include=latestPost.user`, then the custom loading logic for the `user` relationship might look like this: - -```php -$api->resource('categories', new EloquentAdapter(Models\Category::class), function (Type $type) { - $type->hasOne('latestPost')->type('posts')->includable(); // 1 -}); - -$api->resource('posts', new EloquentAdapter(Models\Post::class), function (Type $type) { - $type->hasOne('user') // 2 - ->includable() - ->load(function (array $models, array $relationships, Context $context) { - // Since this request is to the `GET /categories` endpoint, $models - // will be an array of Category models, and $relationships will be - // an array containing the objects [1, 2] above. - }); -}); -``` - ## Polymorphic Relationships Define a polymorphic relationship using the `polymorphic` method. Optionally you may provide an array of allowed resource types: diff --git a/src/Endpoint/Concerns/IncludesData.php b/src/Endpoint/Concerns/IncludesData.php index f28d588..1462bd5 100644 --- a/src/Endpoint/Concerns/IncludesData.php +++ b/src/Endpoint/Concerns/IncludesData.php @@ -103,40 +103,36 @@ trait IncludesData $nextRelationshipPath = array_merge($relationshipPath, [$field]); - if ($load = $field->getLoad()) { + if ($field->shouldLoad()) { $type = $field->getType(); - if (is_callable($load)) { - $load($models, $nextRelationshipPath, $field->hasLinkage(), $context); + if (is_string($type)) { + $relatedResource = $this->api->getResource($type); + $scope = function ($query) use ($context, $field, $relatedResource) { + run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]); + run_callbacks($field->getListeners('scope'), [$query, $context]); + }; } else { - if (is_string($type)) { - $relatedResource = $this->api->getResource($type); - $scope = function ($query) use ($context, $field, $relatedResource) { - run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]); - run_callbacks($field->getListeners('scope'), [$query, $context]); - }; - } else { - $relatedResources = is_array($type) ? array_map(function ($type) { - return $this->api->getResource($type); - }, $type) : $this->api->getResources(); + $relatedResources = is_array($type) ? array_map(function ($type) { + return $this->api->getResource($type); + }, $type) : $this->api->getResources(); - $scope = array_combine( - array_map(function ($relatedResource) { - return $relatedResource->getType(); - }, $relatedResources), + $scope = array_combine( + array_map(function ($relatedResource) { + return $relatedResource->getType(); + }, $relatedResources), - array_map(function ($relatedResource) use ($context, $field) { - return function ($query) use ($context, $field, $relatedResource) { - run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]); - run_callbacks($field->getListeners('scope'), [$query, $context]); - }; - }, $relatedResources) - ); - } - - $adapter->load($models, $nextRelationshipPath, $scope, $field->hasLinkage()); + array_map(function ($relatedResource) use ($context, $field) { + return function ($query) use ($context, $field, $relatedResource) { + run_callbacks($relatedResource->getSchema()->getListeners('scope'), [$query, $context]); + run_callbacks($field->getListeners('scope'), [$query, $context]); + }; + }, $relatedResources) + ); } + $adapter->load($models, $nextRelationshipPath, $scope, $field->hasLinkage()); + if (isset($include[$name]) && is_string($type)) { $relatedResource = $this->api->getResource($type); diff --git a/src/Schema/Relationship.php b/src/Schema/Relationship.php index 413544d..4369ade 100644 --- a/src/Schema/Relationship.php +++ b/src/Schema/Relationship.php @@ -70,13 +70,10 @@ abstract class Relationship extends Field /** * Allow the relationship data to be eager-loaded into the model collection. - * - * This is used to prevent the n+1 query problem. If null, the adapter will - * be used to eager-load relationship data into the model collection. */ - public function load(callable $callback = null) + public function load() { - $this->load = $callback ?: true; + $this->load = true; return $this; } @@ -159,7 +156,7 @@ abstract class Relationship extends Field /** * @return bool|callable */ - public function getLoad() + public function shouldLoad() { return $this->load; } diff --git a/tests/feature/RelationshipInclusionTest.php b/tests/feature/RelationshipInclusionTest.php index bb2af27..d2c22e8 100644 --- a/tests/feature/RelationshipInclusionTest.php +++ b/tests/feature/RelationshipInclusionTest.php @@ -40,11 +40,6 @@ class RelationshipInclusionTest extends AbstractTestCase $this->markTestIncomplete(); } - public function test_to_one_relationships_can_have_custom_preloaders() - { - $this->markTestIncomplete(); - } - public function test_to_one_relationships_can_be_not_loadable() { $this->markTestIncomplete();