diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php index 962127e16f71..19f795c0c0eb 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php @@ -25,6 +25,13 @@ class Blocks implements ResolverInterface */ private $blockDataProvider; + /** + * Error message + * + * @var array + */ + private $errorMessage = []; + /** * @param BlockDataProvider $blockDataProvider */ @@ -50,11 +57,14 @@ public function resolve( $resultData = [ 'items' => $blocksData, + 'errors' => $this->errorMessage ]; return $resultData; } /** + * Get block identifiers + * * @param array $args * @return string[] * @throws GraphQlInputException @@ -69,6 +79,8 @@ private function getBlockIdentifiers(array $args): array } /** + * Get blocks data + * * @param array $blockIdentifiers * @return array * @throws GraphQlNoSuchEntityException @@ -78,11 +90,29 @@ private function getBlocksData(array $blockIdentifiers): array $blocksData = []; try { foreach ($blockIdentifiers as $blockIdentifier) { - $blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier); + $blockData = $this->blockDataProvider->getData($blockIdentifier); + if (!empty($blockData)) { + $blocksData[$blockIdentifier] = $blockData; + } else { + $this->setErrorMessage(sprintf('The CMS block with the "%s" ID is disabled.', $blockIdentifier)); + } } } catch (NoSuchEntityException $e) { - throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); + $this->setErrorMessage($e->getMessage()); } return $blocksData; } + + /** + * Set error message + * + * @param string $error + * @return array + */ + private function setErrorMessage(string $error): array + { + $this->errorMessage[]['message'] = $error; + return $this->errorMessage; + } + } diff --git a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php index 5b7e632a73cb..0d569409c452 100644 --- a/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php +++ b/app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php @@ -40,6 +40,8 @@ public function __construct( } /** + * Get block data + * * @param string $blockIdentifier * @return array * @throws NoSuchEntityException @@ -49,7 +51,7 @@ public function getData(string $blockIdentifier): array $block = $this->blockRepository->getById($blockIdentifier); if (false === $block->isActive()) { - throw new NoSuchEntityException(); + return []; } $renderedContent = $this->widgetFilter->filter($block->getContent()); diff --git a/app/code/Magento/CmsGraphQl/etc/schema.graphqls b/app/code/Magento/CmsGraphQl/etc/schema.graphqls index e8abd2201b88..8686a50af4bf 100644 --- a/app/code/Magento/CmsGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CmsGraphQl/etc/schema.graphqls @@ -32,10 +32,15 @@ type CmsPage @doc(description: "CMS page defines all CMS page information") { type CmsBlocks @doc(description: "CMS blocks information") { items: [CmsBlock] @doc(description: "An array of CMS blocks") + errors: [ErrorMessage] @doc(description: "An array of errors message") } 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") +} + +type ErrorMessage @doc(description: "Error message define all error information") { + message: String @doc(description: "Error message") } \ No newline at end of file