-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing getting the results from a specific location for GraphQL
Requires ezsystem:ezplatform-graphql:^3.0
- Loading branch information
Bertrand Dunogier
committed
Nov 24, 2020
1 parent
abd1f4f
commit de9c945
Showing
7 changed files
with
160 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\EzPlatformQueryFieldType\GraphQL; | ||
|
||
use EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory; | ||
use EzSystems\EzPlatformGraphQL\GraphQL\Value\Item; | ||
use EzSystems\EzPlatformQueryFieldType\API\QueryFieldServiceInterface; | ||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use EzSystems\EzPlatformGraphQL\GraphQL\Value\Field; | ||
use Overblog\GraphQLBundle\Definition\Argument; | ||
use Overblog\GraphQLBundle\Relay\Connection\Paginator; | ||
|
||
final class QueryFieldItemResolver | ||
{ | ||
/** @var \EzSystems\EzPlatformQueryFieldType\API\QueryFieldLocationService */ | ||
private $queryFieldService; | ||
|
||
/** @var \EzSystems\EzPlatformGraphQL\GraphQL\ItemFactory */ | ||
private $itemFactory; | ||
|
||
public function __construct(QueryFieldServiceInterface $queryFieldService, ItemFactory $itemFactory) | ||
{ | ||
$this->queryFieldService = $queryFieldService; | ||
$this->itemFactory = $itemFactory; | ||
} | ||
|
||
public function resolveQueryField(Field $field, Item $item) | ||
{ | ||
return $this->queryFieldService->loadContentItems($item->getContent(), $field->fieldDefIdentifier); | ||
} | ||
|
||
public function resolveQueryFieldConnection(Argument $args, Field $field, Item $item) | ||
{ | ||
if (!isset($args['first'])) { | ||
$args['first'] = $this->queryFieldService->getPaginationConfiguration($item->getContent(), $field->fieldDefIdentifier); | ||
} | ||
|
||
$paginator = new Paginator(function ($offset, $limit) use ($item, $field) { | ||
return array_map( | ||
function (Content $content) { | ||
return $this->itemFactory->fromContent($content); | ||
}, | ||
$this->queryFieldService->loadContentItemsSliceForLocation($item->getLocation(), $field->fieldDefIdentifier, $offset, $limit) | ||
); | ||
}); | ||
|
||
return $paginator->auto( | ||
$args, | ||
function () use ($item, $field) { | ||
return $this->queryFieldService->countContentItemsForLocation($item->getLocation(), $field->fieldDefIdentifier); | ||
} | ||
); | ||
} | ||
|
||
public function resolveQueryFieldDefinitionParameters(array $parameters): array | ||
{ | ||
$return = []; | ||
|
||
foreach ($parameters as $name => $value) { | ||
$return[] = ['name' => $name, 'value' => $value]; | ||
} | ||
|
||
return $return; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/Symfony/DependencyInjection/Compiler/ItemFeaturePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
|
||
namespace EzSystems\EzPlatformQueryFieldType\Symfony\DependencyInjection\Compiler; | ||
|
||
use EzSystems\EzPlatformQueryFieldType\GraphQL\ContentQueryFieldDefinitionMapper; | ||
use EzSystems\EzPlatformQueryFieldType\GraphQL\QueryFieldItemResolver; | ||
use EzSystems\EzPlatformQueryFieldType\GraphQL\QueryFieldResolver; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Enables the Item API if it is available in GraphQL. | ||
*/ | ||
class ItemFeaturePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!class_exists('EzSystems\\EzPlatformGraphQL\\GraphQL\\Value\\Item')) { | ||
return; | ||
} | ||
|
||
$this->updateResolver($container); | ||
$this->updateFieldDefinitionMapper($container); | ||
} | ||
|
||
private function updateResolver(ContainerBuilder $container) | ||
{ | ||
$definition = $container->getDefinition(QueryFieldResolver::class); | ||
$definition->setClass(QueryFieldItemResolver::class); | ||
$container->setDefinition(QueryFieldResolver::class, $definition); | ||
} | ||
|
||
private function updateFieldDefinitionMapper(ContainerBuilder $container) | ||
{ | ||
$definition = $container->getDefinition(ContentQueryFieldDefinitionMapper::class); | ||
$definition->addMethodCall('setResolveWithItem', [true]); | ||
$container->setDefinition(ContentQueryFieldDefinitionMapper::class, $definition); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters