-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from magento/32-cms-block-graphql-support
#32 CMS block coverage
- Loading branch information
Showing
6 changed files
with
176 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CmsGraphQl\Model\Resolver; | ||
|
||
use Magento\CmsGraphQl\Model\Resolver\DataProvider\Block as BlockDataProvider; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* CMS blocks field resolver, used for GraphQL request processing | ||
*/ | ||
class Blocks implements ResolverInterface | ||
{ | ||
/** | ||
* @var BlockDataProvider | ||
*/ | ||
private $blockDataProvider; | ||
|
||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param BlockDataProvider $blockDataProvider | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
BlockDataProvider $blockDataProvider, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->blockDataProvider = $blockDataProvider; | ||
$this->valueFactory = $valueFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) : Value { | ||
|
||
$result = function () use ($args) { | ||
$blockIdentifiers = $this->getBlockIdentifiers($args); | ||
$blocksData = $this->getBlocksData($blockIdentifiers); | ||
|
||
$resultData = [ | ||
'items' => $blocksData, | ||
]; | ||
return $resultData; | ||
}; | ||
return $this->valueFactory->create($result); | ||
} | ||
|
||
/** | ||
* @param array $args | ||
* @return string[] | ||
* @throws GraphQlInputException | ||
*/ | ||
private function getBlockIdentifiers(array $args): array | ||
{ | ||
if (!isset($args['identifiers']) || !is_array($args['identifiers']) || count($args['identifiers']) === 0) { | ||
throw new GraphQlInputException(__('"identifiers" of CMS blocks should be specified')); | ||
} | ||
|
||
return $args['identifiers']; | ||
} | ||
|
||
/** | ||
* @param array $blockIdentifiers | ||
* @return array | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
private function getBlocksData(array $blockIdentifiers): array | ||
{ | ||
$blocksData = []; | ||
try { | ||
foreach ($blockIdentifiers as $blockIdentifier) { | ||
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier); | ||
} | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} | ||
return $blocksData; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.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,53 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CmsGraphQl\Model\Resolver\DataProvider; | ||
|
||
use Magento\Cms\Api\BlockRepositoryInterface; | ||
use Magento\Cms\Api\Data\BlockInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
|
||
/** | ||
* Cms block data provider | ||
*/ | ||
class Block | ||
{ | ||
/** | ||
* @var BlockRepositoryInterface | ||
*/ | ||
private $blockRepository; | ||
|
||
/** | ||
* @param BlockRepositoryInterface $blockRepository | ||
*/ | ||
public function __construct( | ||
BlockRepositoryInterface $blockRepository | ||
) { | ||
$this->blockRepository = $blockRepository; | ||
} | ||
|
||
/** | ||
* @param string $blockIdentifier | ||
* @return array | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function getData(string $blockIdentifier): array | ||
{ | ||
$block = $this->blockRepository->getById($blockIdentifier); | ||
|
||
if (false === $block->isActive()) { | ||
throw new NoSuchEntityException(); | ||
} | ||
|
||
$blockData = [ | ||
BlockInterface::IDENTIFIER => $block->getIdentifier(), | ||
BlockInterface::TITLE => $block->getTitle(), | ||
BlockInterface::CONTENT => $block->getContent(), | ||
]; | ||
return $blockData; | ||
} | ||
} |
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
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