Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed EZP-31630: error when running location query #37

Open
wants to merge 1 commit into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=7.1",
"ext-json": "*",
"ezsystems/ezplatform-graphql": "^1.0@dev",
"ezsystems/ezpublish-kernel": "^7.0||^8.0"
"ezsystems/ezpublish-kernel": "^7.0"
},
"autoload": {
"psr-4": {
Expand All @@ -34,5 +34,8 @@
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"scripts": {
"run-specs": "@php vendor/bin/phpspec run"
}
}
14 changes: 13 additions & 1 deletion spec/API/QueryFieldServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\API\Repository\Values\Content\Query as ApiQuery;
use eZ\Publish\API\Repository\Values\Content\LocationQuery as ApiLocationQuery;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\Core\QueryType\QueryType;
use eZ\Publish\Core\QueryType\QueryTypeRegistry;
use eZ\Publish\Core\Repository\Values;
use EzSystems\EzPlatformGraphQL\GraphQL\Value\Field;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand Down Expand Up @@ -61,6 +61,7 @@ function let(
$queryType->getQuery(Argument::any())->willReturn(new ApiQuery());
// @todo this should fail. It does not.
$searchService->findContent(Argument::any())->willReturn($this->searchResult);
$searchService->findLocations(Argument::any())->willReturn($this->searchResult);
$this->beConstructedWith($searchService, $contentTypeService, $locationService, $queryTypeRegistry);
}

Expand All @@ -79,6 +80,17 @@ function it_counts_items_from_a_query_field_for_a_given_content_item()
$this->countContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER)->shouldBe($this->totalCount);
}

function it_counts_using_findLocations_if_the_Query_is_a_LocationQuery()
{
$this->loadContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER);
}

function it_loads_item_using_findLocations_if_the_Query_is_a_LocationQuery(QueryType $queryType)
{
$queryType->getQuery(Argument::any())->willReturn(new ApiLocationQuery());
$this->loadContentItems($this->getContent(), self::FIELD_DEFINITION_IDENTIFIER);
}

/**
* @return \eZ\Publish\Core\Repository\Values\Content\Content
*/
Expand Down
45 changes: 32 additions & 13 deletions src/API/QueryFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
Expand Down Expand Up @@ -59,20 +60,15 @@ public function loadContentItems(Content $content, string $fieldDefinitionIdenti
{
$query = $this->prepareQuery($content, $fieldDefinitionIdentifier);

return array_map(
function (SearchHit $searchHit) {
return $searchHit->valueObject;
},
$this->searchService->findContent($query)->searchHits
);
return $this->runQuery($query);
}

public function countContentItems(Content $content, string $fieldDefinitionIdentifier): int
{
$query = $this->prepareQuery($content, $fieldDefinitionIdentifier);
$query->limit = 0;

return $this->searchService->findContent($query)->totalCount;
return $this->runCountQuery($query);
}

public function loadContentItemsSlice(Content $content, string $fieldDefinitionIdentifier, int $offset, int $limit): iterable
Expand All @@ -81,12 +77,7 @@ public function loadContentItemsSlice(Content $content, string $fieldDefinitionI
$query->offset = $offset;
$query->limit = $limit;

return array_map(
function (SearchHit $searchHit) {
return $searchHit->valueObject;
},
$this->searchService->findContent($query)->searchHits
);
return $this->runQuery($query);
}

public function getPaginationConfiguration(Content $content, string $fieldDefinitionIdentifier): int
Expand Down Expand Up @@ -181,4 +172,32 @@ private function loadFieldDefinition(Content $content, string $fieldDefinitionId

return $fieldDefinition;
}

private function runQuery(Query $query): iterable
{
if ($query instanceof LocationQuery) {
return array_map(
function (SearchHit $searchHit) {
return $searchHit->valueObject->getContent();
},
$this->searchService->findLocations($query)->searchHits
);
} else {
return array_map(
function (SearchHit $searchHit) {
return $searchHit->valueObject;
},
$this->searchService->findContent($query)->searchHits
);
}
}

private function runCountQuery(Query $query): int
{
if ($query instanceof LocationQuery) {
return $this->searchService->findLocations($query)->totalCount;
} else {
return $this->searchService->findContent($query)->totalCount;
}
}
}