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

Commit

Permalink
#32 Inited a new graph QL module that enables support of CMS blocks API
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Glushko committed Jun 30, 2018
1 parent f947cad commit e17c536
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 0 deletions.
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 app/code/Magento/CmsGraphQl/Model/Resolver/CmsBlocks.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\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'];
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/CmsGraphQl/README.md
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.
23 changes: 23 additions & 0 deletions app/code/Magento/CmsGraphQl/composer.json
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\\": ""
}
}
}
16 changes: 16 additions & 0 deletions app/code/Magento/CmsGraphQl/etc/module.xml
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>
18 changes: 18 additions & 0 deletions app/code/Magento/CmsGraphQl/etc/schema.graphqls
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")
}
9 changes: 9 additions & 0 deletions app/code/Magento/CmsGraphQl/registration.php
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__);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,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": "*",
Expand Down

0 comments on commit e17c536

Please sign in to comment.