Remove ability to have custom relationship pre-loaders for now

This commit is contained in:
Toby Zerner 2021-01-15 08:37:17 +10:30
parent 5d69048ef8
commit 0c8de3a75a
4 changed files with 26 additions and 62 deletions

View File

@ -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:

View File

@ -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);

View File

@ -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;
}

View File

@ -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();