This repository has been archived by the owner on Apr 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#32 Inited a new graph QL module that enables support of CMS blocks API
- Loading branch information
Roman Glushko
committed
Jun 30, 2018
1 parent
f947cad
commit e17c536
Showing
8 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
app/code/Magento/CmsGraphQl/Model/Resolver/Cms/CmsBlockDataProvider.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,69 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CmsGraphQl\Model\Resolver\Cms; | ||
|
||
use Magento\Cms\Api\BlockRepositoryInterface as CmsBlockRepositoryInterface; | ||
use Magento\Cms\Api\Data\BlockInterface as CmsBlockInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
|
||
/** | ||
* Cms block field data provider, used for GraphQL request processing. | ||
*/ | ||
class CmsBlockDataProvider | ||
{ | ||
/** | ||
* @var CmsBlockRepositoryInterface | ||
*/ | ||
private $cmsBlockRepository; | ||
|
||
/** | ||
* @param CmsBlockRepositoryInterface $cmsBlockRepository | ||
*/ | ||
public function __construct( | ||
CmsBlockRepositoryInterface $cmsBlockRepository | ||
) { | ||
$this->cmsBlockRepository = $cmsBlockRepository; | ||
} | ||
|
||
/** | ||
* Get CMS block data by identifier | ||
* | ||
* @param string $cmsBlockIdentifier | ||
* @return array|GraphQlInputException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function getCmsBlockById(string $cmsBlockIdentifier) | ||
{ | ||
$cmsBlockModel = $this->cmsBlockRepository->getById($cmsBlockIdentifier); | ||
|
||
if (!$cmsBlockModel->isActive()) { | ||
throw new NoSuchEntityException(); | ||
} | ||
|
||
return $this->processCmsBlock($cmsBlockModel); | ||
} | ||
|
||
/** | ||
* Transform single CMS block data from object to in array format | ||
* | ||
* @param CmsBlockInterface $cmsBlockModel | ||
* @return array | ||
*/ | ||
private function processCmsBlock(CmsBlockInterface $cmsBlockModel) : array | ||
{ | ||
$cmsBlockData = [ | ||
'identifier' => $cmsBlockModel->getIdentifier(), | ||
'title' => $cmsBlockModel->getTitle(), | ||
'content' => $cmsBlockModel->getContent(), | ||
]; | ||
|
||
return $cmsBlockData; | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
app/code/Magento/CmsGraphQl/Model/Resolver/CmsBlocks.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,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\Cms\CmsBlockDataProvider; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
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 CmsBlocks implements ResolverInterface | ||
{ | ||
/** | ||
* @var CmsBlockDataProvider | ||
*/ | ||
private $cmsBlockDataProvider; | ||
|
||
/** | ||
* @var ValueFactory | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param CmsBlockDataProvider $cmsBlockDataProvider | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
CmsBlockDataProvider $cmsBlockDataProvider, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->valueFactory = $valueFactory; | ||
$this->cmsBlockDataProvider = $cmsBlockDataProvider; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) : Value { | ||
|
||
$cmsBlockListData = []; | ||
$cmsBlockIdentifiers = $this->getCmsBlockIdentifiers($args); | ||
|
||
foreach ($cmsBlockIdentifiers as $cmsBlockIdentifier) { | ||
try { | ||
$cmsBlockListData[$cmsBlockIdentifier] = $this->cmsBlockDataProvider->getCmsBlockById( | ||
$cmsBlockIdentifier | ||
); | ||
} catch (NoSuchEntityException $ex) { | ||
$cmsBlockListData[$cmsBlockIdentifier] = new GraphQlInputException( | ||
__( | ||
'CMS block with "%1" ID does not found', | ||
$cmsBlockIdentifier | ||
) | ||
); | ||
} | ||
} | ||
|
||
$cmsBlocksData = [ | ||
'items' => $cmsBlockListData | ||
]; | ||
|
||
$result = function () use ($cmsBlocksData) { | ||
return !empty($cmsBlocksData) ? $cmsBlocksData : []; | ||
}; | ||
|
||
return $this->valueFactory->create($result); | ||
} | ||
|
||
/** | ||
* Retrieve CMS block identifiers to retrieve | ||
* | ||
* @param array $args | ||
* @return string[] | ||
* @throws GraphQlInputException | ||
*/ | ||
private function getCmsBlockIdentifiers($args) | ||
{ | ||
if (!isset($args['identifiers']) && is_array($args['identifiers']) && count($args['identifiers']) > 0) { | ||
throw new GraphQlInputException(__('"identifiers" of CMS blocks should be specified')); | ||
} | ||
|
||
return (array) $args['identifiers']; | ||
} | ||
} |
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,4 @@ | ||
# CmsGraphQl | ||
|
||
**CmsGraphQl** provides type information for the GraphQl module | ||
to generate CMS fields for cms information endpoints. |
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,23 @@ | ||
{ | ||
"name": "magento/module-cms-graph-ql", | ||
"description": "N/A", | ||
"type": "magento2-module", | ||
"require": { | ||
"php": "~7.1.3||~7.2.0", | ||
"magento/framework": "*", | ||
"magento/module-store": "*", | ||
"magento/module-cms": "*" | ||
}, | ||
"license": [ | ||
"OSL-3.0", | ||
"AFL-3.0" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Magento\\CmsGraphQl\\": "" | ||
} | ||
} | ||
} |
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,16 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Magento_CmsGraphQl"> | ||
<sequence> | ||
<module name="Magento_Cms"/> | ||
<module name="Magento_Authorization"/> | ||
<module name="Magento_GraphQl"/> | ||
</sequence> | ||
</module> | ||
</config> |
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,18 @@ | ||
# Copyright © Magento, Inc. All rights reserved. | ||
# See COPYING.txt for license details. | ||
|
||
type Query { | ||
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") | ||
} | ||
|
||
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") | ||
} |
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,9 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
use Magento\Framework\Component\ComponentRegistrar; | ||
|
||
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_CmsGraphQl', __DIR__); |
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