Skip to content

Commit dce6c11

Browse files
author
Valeriy Naida
authored
Merge pull request #109 from magento/32-cms-block-graphql-support
#32 CMS block coverage
2 parents eb1ed7a + 02ab4bf commit dce6c11

File tree

6 files changed

+176
-7
lines changed

6 files changed

+176
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CmsGraphQl\Model\Resolver;
9+
10+
use Magento\CmsGraphQl\Model\Resolver\DataProvider\Block as BlockDataProvider;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
use Magento\Framework\GraphQl\Query\Resolver\Value;
16+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
17+
use Magento\Framework\GraphQl\Query\ResolverInterface;
18+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
19+
20+
/**
21+
* CMS blocks field resolver, used for GraphQL request processing
22+
*/
23+
class Blocks implements ResolverInterface
24+
{
25+
/**
26+
* @var BlockDataProvider
27+
*/
28+
private $blockDataProvider;
29+
30+
/**
31+
* @var ValueFactory
32+
*/
33+
private $valueFactory;
34+
35+
/**
36+
* @param BlockDataProvider $blockDataProvider
37+
* @param ValueFactory $valueFactory
38+
*/
39+
public function __construct(
40+
BlockDataProvider $blockDataProvider,
41+
ValueFactory $valueFactory
42+
) {
43+
$this->blockDataProvider = $blockDataProvider;
44+
$this->valueFactory = $valueFactory;
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function resolve(
51+
Field $field,
52+
$context,
53+
ResolveInfo $info,
54+
array $value = null,
55+
array $args = null
56+
) : Value {
57+
58+
$result = function () use ($args) {
59+
$blockIdentifiers = $this->getBlockIdentifiers($args);
60+
$blocksData = $this->getBlocksData($blockIdentifiers);
61+
62+
$resultData = [
63+
'items' => $blocksData,
64+
];
65+
return $resultData;
66+
};
67+
return $this->valueFactory->create($result);
68+
}
69+
70+
/**
71+
* @param array $args
72+
* @return string[]
73+
* @throws GraphQlInputException
74+
*/
75+
private function getBlockIdentifiers(array $args): array
76+
{
77+
if (!isset($args['identifiers']) || !is_array($args['identifiers']) || count($args['identifiers']) === 0) {
78+
throw new GraphQlInputException(__('"identifiers" of CMS blocks should be specified'));
79+
}
80+
81+
return $args['identifiers'];
82+
}
83+
84+
/**
85+
* @param array $blockIdentifiers
86+
* @return array
87+
* @throws GraphQlNoSuchEntityException
88+
*/
89+
private function getBlocksData(array $blockIdentifiers): array
90+
{
91+
$blocksData = [];
92+
try {
93+
foreach ($blockIdentifiers as $blockIdentifier) {
94+
$blocksData[$blockIdentifier] = $this->blockDataProvider->getData($blockIdentifier);
95+
}
96+
} catch (NoSuchEntityException $e) {
97+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
98+
}
99+
return $blocksData;
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CmsGraphQl\Model\Resolver\DataProvider;
9+
10+
use Magento\Cms\Api\BlockRepositoryInterface;
11+
use Magento\Cms\Api\Data\BlockInterface;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
14+
/**
15+
* Cms block data provider
16+
*/
17+
class Block
18+
{
19+
/**
20+
* @var BlockRepositoryInterface
21+
*/
22+
private $blockRepository;
23+
24+
/**
25+
* @param BlockRepositoryInterface $blockRepository
26+
*/
27+
public function __construct(
28+
BlockRepositoryInterface $blockRepository
29+
) {
30+
$this->blockRepository = $blockRepository;
31+
}
32+
33+
/**
34+
* @param string $blockIdentifier
35+
* @return array
36+
* @throws NoSuchEntityException
37+
*/
38+
public function getData(string $blockIdentifier): array
39+
{
40+
$block = $this->blockRepository->getById($blockIdentifier);
41+
42+
if (false === $block->isActive()) {
43+
throw new NoSuchEntityException();
44+
}
45+
46+
$blockData = [
47+
BlockInterface::IDENTIFIER => $block->getIdentifier(),
48+
BlockInterface::TITLE => $block->getTitle(),
49+
BlockInterface::CONTENT => $block->getContent(),
50+
];
51+
return $blockData;
52+
}
53+
}

app/code/Magento/CmsGraphQl/Model/PageDataProvider.php renamed to app/code/Magento/CmsGraphQl/Model/Resolver/DataProvider/Page.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CmsGraphQl\Model;
8+
namespace Magento\CmsGraphQl\Model\Resolver\DataProvider;
99

1010
use Magento\Cms\Api\Data\PageInterface;
1111
use Magento\Cms\Api\PageRepositoryInterface;
1212
use Magento\Framework\Exception\NoSuchEntityException;
1313

1414
/**
15-
* Get CMS page data by Id
15+
* Cms page data provider
1616
*/
17-
class PageDataProvider
17+
class Page
1818
{
1919
/**
2020
* @var PageRepositoryInterface
@@ -35,7 +35,7 @@ public function __construct(
3535
* @return array
3636
* @throws NoSuchEntityException
3737
*/
38-
public function getData(int $pageId) : array
38+
public function getData(int $pageId): array
3939
{
4040
$page = $this->pageRepository->getById($pageId);
4141

app/code/Magento/CmsGraphQl/Model/PageResolver.php renamed to app/code/Magento/CmsGraphQl/Model/Resolver/Page.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CmsGraphQl\Model;
8+
namespace Magento\CmsGraphQl\Model\Resolver;
99

10+
use Magento\CmsGraphQl\Model\Resolver\DataProvider\Page as PageDataProvider;
1011
use Magento\Framework\Exception\NoSuchEntityException;
1112
use Magento\Framework\GraphQl\Config\Element\Field;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -19,7 +20,7 @@
1920
/**
2021
* CMS page field resolver, used for GraphQL request processing
2122
*/
22-
class PageResolver implements ResolverInterface
23+
class Page implements ResolverInterface
2324
{
2425
/**
2526
* @var PageDataProvider

app/code/Magento/CmsGraphQl/etc/schema.graphqls

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
type Query {
55
cmsPage (
66
id: Int @doc(description: "Id of the CMS page")
7-
): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\PageResolver") @doc(description: "The CMS page query returns information about a CMS page")
7+
): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Page") @doc(description: "The CMS page query returns information about a CMS page")
8+
cmsBlocks (
9+
identifiers: [String] @doc(description: "Identifiers of the CMS blocks")
10+
): CmsBlocks @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\Blocks") @doc(description: "The CMS block query returns information about CMS blocks")
811
}
912

1013
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") {
1619
meta_title: String @doc(description: "CMS page meta title")
1720
meta_description: String @doc(description: "CMS page meta description")
1821
meta_keywords: String @doc(description: "CMS page meta keywords")
22+
}
23+
24+
type CmsBlocks @doc(description: "CMS blocks information") {
25+
items: [CmsBlock] @doc(description: "An array of CMS blocks")
26+
}
27+
28+
type CmsBlock @doc(description: "CMS block defines all CMS block information") {
29+
identifier: String @doc(description: "CMS block identifier")
30+
title: String @doc(description: "CMS block title")
31+
content: String @doc(description: "CMS block content")
1932
}

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
"magento/module-grouped-import-export": "*",
163163
"magento/module-grouped-product": "*",
164164
"magento/module-grouped-product-graph-ql": "*",
165+
"magento/module-cms-graph-ql": "*",
165166
"magento/module-import-export": "*",
166167
"magento/module-indexer": "*",
167168
"magento/module-instant-purchase": "*",

0 commit comments

Comments
 (0)