Skip to content

Commit

Permalink
Added lookup of location by URL Alias
Browse files Browse the repository at this point in the history
```
location(urlAlias: "/") {}
```
  • Loading branch information
Bertrand Dunogier authored and bdunogier committed Sep 30, 2019
1 parent d0e998f commit d07aa8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/GraphQL/DataLoader/LocationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function findById($id): Location;
*/
public function findByRemoteId($remoteId): Location;

/**
* @param string $remoteId A location remote id
*
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function findByUrlAlias(string $urlAlias): Location;

/**
* Counts the results of a query.
*
Expand Down
19 changes: 18 additions & 1 deletion src/GraphQL/DataLoader/SearchLocationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
namespace EzSystems\EzPlatformGraphQL\GraphQL\DataLoader;

use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\URLAliasService;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Values\Content\URLAlias;
use EzSystems\EzPlatformGraphQL\GraphQL\DataLoader\Exception\ArgumentsException;
use eZ\Publish\API\Repository\Exceptions as ApiException;
use eZ\Publish\API\Repository\SearchService;
Expand All @@ -30,10 +32,16 @@ class SearchLocationLoader implements LocationLoader
*/
private $locationService;

public function __construct(SearchService $searchService, LocationService $locationService)
/**
* @var \eZ\Publish\API\Repository\URLAliasService
*/
private $urlAliasService;

public function __construct(SearchService $searchService, LocationService $locationService, URLAliasService $urlAliasService)
{
$this->searchService = $searchService;
$this->locationService = $locationService;
$this->urlAliasService = $urlAliasService;
}

public function find(LocationQuery $query): array
Expand Down Expand Up @@ -66,6 +74,15 @@ public function findByRemoteId($id): Location
}
}

public function findByUrlAlias(string $urlAlias): Location
{
$alias = $this->urlAliasService->lookup($urlAlias);

return ($alias->type == URLAlias::LOCATION)
? $this->locationService->loadLocation($alias->destination)
: null;
}

/**
* Counts the results of a query.
*
Expand Down
2 changes: 2 additions & 0 deletions src/GraphQL/Resolver/LocationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function resolveLocation($args)
return $this->locationLoader->findById($args['locationId']);
} elseif (isset($args['remoteId'])) {
return $this->locationLoader->findByRemoteId($args['remoteId']);
} elseif (isset($args['urlAlias'])) {
return $this->locationLoader->findByUrlAlias($args['urlAlias']);
}

throw new ArgumentsException('Requires one and only one of remoteId or locationId');
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/graphql/Repository.types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Repository:
remoteId:
description: "A location remote id"
type: "Int"
urlAlias:
description: "A location url alias: 'path/to/content-item'"
type: "String"
resolve: "@=resolver('Location', [args])"
contentType:
type: "ContentType"
Expand Down

0 comments on commit d07aa8e

Please sign in to comment.