Skip to content
This repository has been archived by the owner on Apr 29, 2019. It is now read-only.

Commit

Permalink
GraphQL-32: CMS block coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Jul 19, 2018
1 parent 3bdd690 commit 02ab4bf
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 181 deletions.
101 changes: 101 additions & 0 deletions app/code/Magento/CmsGraphQl/Model/Resolver/Blocks.php
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;
}
}

This file was deleted.

102 changes: 0 additions & 102 deletions app/code/Magento/CmsGraphQl/Model/Resolver/CmsBlocks.php

This file was deleted.

53 changes: 53 additions & 0 deletions app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Block.php
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,7 +20,7 @@
/**
* CMS page field resolver, used for GraphQL request processing
*/
class PageResolver implements ResolverInterface
class Page implements ResolverInterface
{
/**
* @var PageDataProvider
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/CmsGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +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\\CmsBlocks") @doc(description: "The CMS block query returns information about CMS blocks")
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") {
Expand Down

0 comments on commit 02ab4bf

Please sign in to comment.