diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php new file mode 100644 index 0000000000000..ec387013ecfe6 --- /dev/null +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -0,0 +1,101 @@ +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; + } +} diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php new file mode 100644 index 0000000000000..6b848d633f25b --- /dev/null +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/app/code/Magento/CmsGraphQl/Model/PageDataProvider.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php similarity index 90% rename from app/code/Magento/CmsGraphQl/Model/PageDataProvider.php rename to app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php index 9c5b55b741e69..c9b16873345ac 100644 --- a/app/code/Magento/CmsGraphQl/Model/PageDataProvider.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php @@ -5,16 +5,16 @@ */ declare(strict_types=1); -namespace Magento\CmsGraphQl\Model; +namespace Magento\CmsGraphQl\Model\Resolver\DataProvider; use Magento\Cms\Api\Data\PageInterface; use Magento\Cms\Api\PageRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; /** - * Get CMS page data by Id + * Cms page data provider */ -class PageDataProvider +class Page { /** * @var PageRepositoryInterface @@ -35,7 +35,7 @@ public function __construct( * @return array * @throws NoSuchEntityException */ - public function getData(int $pageId) : array + public function getData(int $pageId): array { $page = $this->pageRepository->getById($pageId); diff --git a/app/code/Magento/CmsGraphQl/Model/PageResolver.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php similarity index 93% rename from app/code/Magento/CmsGraphQl/Model/PageResolver.php rename to app/code/Magento/CmsGraphQl/Model/Resolver/Page.php index 707bf516ba30d..4c96ae26f6b7d 100644 --- a/app/code/Magento/CmsGraphQl/Model/PageResolver.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Page.php @@ -5,8 +5,9 @@ */ declare(strict_types=1); -namespace Magento\CmsGraphQl\Model; +namespace Magento\CmsGraphQl\Model\Resolver; +use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; @@ -19,7 +20,7 @@ /** * CMS page field resolver, used for GraphQL request processing */ -class PageResolver implements ResolverInterface +class Page implements ResolverInterface { /** * @var PageDataProvider diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index a8e5b63d4c8c9..997bbf920a09e 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -4,7 +4,10 @@ type Query { cmsPage ( id: Int @doc(description: "Id of the CMS page") - ): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\PageResolver") @doc(description: "The CMS page query returns information about a CMS page") + ): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Page") @doc(description: "The CMS page query returns information about a CMS page") + cmsBlocks ( + identifiers: [String] @doc(description: "Identifiers of the CMS blocks") + ): CmsBlocks @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Blocks") @doc(description: "The CMS block query returns information about CMS blocks") } type CmsPage @doc(description: "CMS page defines all CMS page information") { @@ -16,4 +19,14 @@ type CmsPage @doc(description: "CMS page defines all CMS page information") { meta_title: String @doc(description: "CMS page meta title") meta_description: String @doc(description: "CMS page meta description") meta_keywords: String @doc(description: "CMS page meta keywords") +} + +type CmsBlocks @doc(description: "CMS blocks information") { + items: [CmsBlock] @doc(description: "An array of CMS blocks") +} + +type CmsBlock @doc(description: "CMS block defines all CMS block information") { + identifier: String @doc(description: "CMS block identifier") + title: String @doc(description: "CMS block title") + content: String @doc(description: "CMS block content") } \ No newline at end of file diff --git a/composer.json b/composer.json index ed6e2585bf947..fd297b03b4779 100644 --- a/composer.json +++ b/composer.json @@ -162,6 +162,7 @@ "magento/module-grouped-import-export": "*", "magento/module-grouped-product": "*", "magento/module-grouped-product-graph-ql": "*", + "magento/module-cms-graph-ql": "*", "magento/module-import-export": "*", "magento/module-indexer": "*", "magento/module-instant-purchase": "*",