Skip to content

Commit

Permalink
[perf] use the Content service for ID loading if possible
Browse files Browse the repository at this point in the history
When find or findSingle have one or more ids, and only ids, as a criterion,
use the content service to load the items. It includes multi-load when applicable.

Example: reloation list load
  • Loading branch information
Bertrand Dunogier committed Apr 2, 2019
1 parent 41a1e0f commit 5505643
Showing 1 changed file with 54 additions and 3 deletions.
57 changes: 54 additions & 3 deletions src/GraphQL/DataLoader/SearchContentLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace EzSystems\EzPlatformGraphQL\GraphQL\DataLoader;

use eZ\Publish\API\Repository\ContentService;
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\Exception\NotFoundException;
use eZ\Publish\API\Repository\Exceptions as ApiException;
use eZ\Publish\API\Repository\SearchService;
Expand All @@ -23,10 +24,15 @@ class SearchContentLoader implements ContentLoader
* @var SearchService
*/
private $searchService;
/**
* @var \eZ\Publish\API\Repository\ContentService
*/
private $contentService;

public function __construct(SearchService $searchService)
public function __construct(SearchService $searchService, ContentService $contentService)
{
$this->searchService = $searchService;
$this->contentService = $contentService;
}

/**
Expand All @@ -40,6 +46,10 @@ public function __construct(SearchService $searchService)
*/
public function find(Query $query): array
{
if ($results = $this->runIdSearch($query)) {
return (array)$results;
}

return array_map(
function (SearchHit $searchHit) {
return $searchHit->valueObject;
Expand All @@ -60,7 +70,11 @@ function (SearchHit $searchHit) {
public function findSingle(Criterion $filter): Content
{
try {
return $this->searchService->findSingle($filter);
if ($results = $this->runIdSearch($filter)) {
return $results;
} else {
return $this->searchService->findSingle($filter);
}
} catch (ApiException\InvalidArgumentException $e) {
} catch (ApiException\NotFoundException $e) {
throw new NotFoundException($e->getMessage(), $e->getCode(), $e);
Expand Down Expand Up @@ -88,4 +102,41 @@ public function count(Query $query)
throw new NotFoundException($e->getMessage(), $e->getCode(), $e);
}
}
}

/**
* If $filter only contains an ID criterion, use the content service for loading.
*
* @param $filter
*
* @return \eZ\Publish\API\Repository\Values\Content\Content|\eZ\Publish\API\Repository\Values\Content\Content[]
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
private function runIdSearch($filter)
{
if ($filter instanceof Criterion\ContentId) {
$idArgument = $filter->value;
if (is_array($idArgument) && count($idArgument) > 1) {
return $this->contentService->loadContentListByContentInfo(
$this->contentService->loadContentInfoList($idArgument)
);
} else {
return $this->contentService->loadContent($this->getOneId($idArgument));
}
}
}

private function getOneId($value)
{
if (is_array($value)) {
if (count($value) === 1) {
return $value[0];
}
throw new \InvalidArgumentException("the id argument is an array with more than one ids");
} else if (is_numeric($value)) {
return [$this->contentService->loadContent($value)];
} else {
throw new \InvalidArgumentException("the id argument is an array with more than one ids");
}
}
}

0 comments on commit 5505643

Please sign in to comment.