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

Commit a617b80

Browse files
author
Roman Glushko
committed
#31 Inited a new module and implement a basic support of CMS page entry point for GraphQL
1 parent f947cad commit a617b80

File tree

8 files changed

+305
-0
lines changed

8 files changed

+305
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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\Cms;
9+
10+
use Magento\Cms\Api\Data\PageInterface as CmsPageInterface;
11+
use Magento\Cms\Api\PageRepositoryInterface as CmsPageRepositoryInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Serialize\SerializerInterface;
15+
use Magento\Framework\Webapi\ServiceOutputProcessor;
16+
17+
/**
18+
* Cms field data provider, used for GraphQL request processing.
19+
*/
20+
class CmsPageDataProvider
21+
{
22+
/**
23+
* @var ServiceOutputProcessor
24+
*/
25+
private $serviceOutputProcessor;
26+
27+
/**
28+
* @var SerializerInterface
29+
*/
30+
private $jsonSerializer;
31+
32+
/**
33+
* @var CmsPageRepositoryInterface
34+
*/
35+
private $cmsPageRepository;
36+
37+
/**
38+
* @param CmsPageRepositoryInterface $cmsPageRepository
39+
* @param ServiceOutputProcessor $serviceOutputProcessor
40+
* @param SerializerInterface $jsonSerializer
41+
*/
42+
public function __construct(
43+
CmsPageRepositoryInterface $cmsPageRepository,
44+
ServiceOutputProcessor $serviceOutputProcessor,
45+
SerializerInterface $jsonSerializer
46+
) {
47+
$this->serviceOutputProcessor = $serviceOutputProcessor;
48+
$this->jsonSerializer = $jsonSerializer;
49+
$this->cmsPageRepository = $cmsPageRepository;
50+
}
51+
52+
/**
53+
* Get CMS page data by Id
54+
*
55+
* @param int $cmsPageId
56+
* @return array
57+
* @throws NoSuchEntityException|LocalizedException
58+
*/
59+
public function getCmsPageById(int $cmsPageId) : array
60+
{
61+
try {
62+
$cmsPageModel = $this->cmsPageRepository->getById($cmsPageId);
63+
} catch (NoSuchEntityException $e) {
64+
// No error should be thrown, null result should be returned
65+
return [];
66+
}
67+
68+
return $this->processCmsPage($cmsPageModel);
69+
}
70+
71+
/**
72+
* Transform single CMS page data from object to in array format
73+
*
74+
* @param CmsPageInterface $cmsPageModel
75+
* @return array
76+
*/
77+
private function processCmsPage(CmsPageInterface $cmsPageModel) : array
78+
{
79+
$cmsPageData = [
80+
'id' => $cmsPageModel->getId(),
81+
'url_key' => $cmsPageModel->getIdentifier(),
82+
'page_title' => $cmsPageModel->getTitle(),
83+
'page_content' => $cmsPageModel->getContent(),
84+
'content_heading' => $cmsPageModel->getContentHeading(),
85+
'layout' => $cmsPageModel->getPageLayout(),
86+
'mate_title' => $cmsPageModel->getMetaTitle(),
87+
'mate_description' => $cmsPageModel->getMetaDescription(),
88+
'mate_keywords' => $cmsPageModel->getMetaKeywords(),
89+
];
90+
91+
if (isset($cmsPageData['extension_attributes'])) {
92+
$cmsPageData = array_merge($cmsPageData, $cmsPageData['extension_attributes']);
93+
}
94+
95+
if (isset($cmsPageData['custom_attributes'])) {
96+
$cmsPageData = array_merge($cmsPageData, $this->processCustomAttributes($cmsPageData['custom_attributes']));
97+
}
98+
99+
return $cmsPageData;
100+
}
101+
102+
/**
103+
* @param array $customAttributes
104+
*
105+
* @return array
106+
*/
107+
private function processCustomAttributes(array $customAttributes) : array
108+
{
109+
$processedCustomAttributes = [];
110+
111+
foreach ($customAttributes as $customAttribute) {
112+
$isArray = false;
113+
$customAttributeCode = $customAttribute['attribute_code'];
114+
$customAttributeValue = $customAttribute['attribute_code'];
115+
116+
if (is_array($customAttributeValue)) {
117+
$isArray = true;
118+
119+
foreach ($customAttributeValue as $attributeValue) {
120+
if (is_array($attributeValue)) {
121+
$processedCustomAttributes[$customAttributeCode] = $this->jsonSerializer->serialize(
122+
$customAttributeValue
123+
);
124+
continue;
125+
}
126+
$processedCustomAttributes[$customAttributeCode] = implode(',', $customAttributeValue);
127+
continue;
128+
}
129+
}
130+
131+
if ($isArray) {
132+
continue;
133+
}
134+
135+
$processedCustomAttributes[$customAttributeCode] = $customAttributeValue;
136+
}
137+
138+
return $processedCustomAttributes;
139+
}
140+
141+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\Authorization\Model\UserContextInterface;
11+
use Magento\CmsGraphQl\Model\Resolver\Cms\CmsPageDataProvider;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14+
use Magento\CustomerGraphQl\Model\Resolver\Customer\CustomerDataProvider;
15+
use Magento\Framework\Exception\NoSuchEntityException;
16+
use Magento\Framework\GraphQl\Config\Element\Field;
17+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
18+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
19+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
20+
use Magento\Framework\GraphQl\Query\Resolver\Value;
21+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
22+
use Magento\Framework\GraphQl\Query\ResolverInterface;
23+
24+
/**
25+
* CMS page field resolver, used for GraphQL request processing.
26+
*/
27+
class CmsPage implements ResolverInterface
28+
{
29+
/**
30+
* @var CmsPageDataProvider
31+
*/
32+
private $cmsPageDataProvider;
33+
34+
/**
35+
* @var ValueFactory
36+
*/
37+
private $valueFactory;
38+
39+
/**
40+
* @param CmsPageDataProvider $cmsPageDataProvider
41+
* @param ValueFactory $valueFactory
42+
*/
43+
public function __construct(
44+
CmsPageDataProvider $cmsPageDataProvider,
45+
ValueFactory $valueFactory
46+
) {
47+
$this->valueFactory = $valueFactory;
48+
$this->cmsPageDataProvider = $cmsPageDataProvider;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function resolve(
55+
Field $field,
56+
$context,
57+
ResolveInfo $info,
58+
array $value = null,
59+
array $args = null
60+
) : Value {
61+
62+
$cmsPageId = $this->getCmsPageId($args);
63+
64+
try {
65+
$cmsPageData = $this->cmsPageDataProvider->getCmsPageById($cmsPageId);
66+
67+
$result = function () use ($cmsPageData) {
68+
return !empty($cmsPageData) ? $cmsPageData : [];
69+
};
70+
71+
return $this->valueFactory->create($result);
72+
} catch (NoSuchEntityException $exception) {
73+
throw new GraphQlNoSuchEntityException(__('CMS page with ID %1 does not exist.', [$cmsPageId]));
74+
}
75+
}
76+
77+
/**
78+
* Retrieve CMS page ID
79+
*
80+
* @param array $args
81+
* @return int
82+
* @throws GraphQlInputException
83+
*/
84+
private function getCmsPageId($args)
85+
{
86+
if (!isset($args['id'])) {
87+
throw new GraphQlInputException(__('"id for category should be specified'));
88+
}
89+
90+
return (int) $args['id'];
91+
}
92+
}

app/code/Magento/CmsGraphQl/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# CmsGraphQl
2+
3+
**CmsGraphQl** provides type information for the GraphQl module
4+
to generate CMS fields for cms information endpoints.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "magento/module-cms-graph-ql",
3+
"description": "N/A",
4+
"type": "magento2-module",
5+
"require": {
6+
"php": "~7.1.3||~7.2.0",
7+
"magento/framework": "*",
8+
"magento/module-store": "*",
9+
"magento/module-cms": "*"
10+
},
11+
"license": [
12+
"OSL-3.0",
13+
"AFL-3.0"
14+
],
15+
"autoload": {
16+
"files": [
17+
"registration.php"
18+
],
19+
"psr-4": {
20+
"Magento\\CmsGraphQl\\": ""
21+
}
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
9+
<module name="Magento_CmsGraphQl">
10+
<sequence>
11+
<module name="Magento_Cms"/>
12+
<module name="Magento_Authorization"/>
13+
<module name="Magento_GraphQl"/>
14+
</sequence>
15+
</module>
16+
</config>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright © Magento, Inc. All rights reserved.
2+
# See COPYING.txt for license details.
3+
4+
type Query {
5+
cmsPage (
6+
id: Int @doc(description: "Id of the CMS page")
7+
): CmsPage @resolver(class: "Magento\\CmsGraphQl\\Model\\Resolver\\CmsPage") @doc(description: "The CMS page query returns information about a CMS page")
8+
}
9+
10+
type CmsPage @doc(description: "CMS page defines all CMS page information") {
11+
url_key: String @doc(description: "URL key of CMS page")
12+
page_title: String @doc(description: "CMS page title")
13+
page_content: String @doc(description: "CMS page content")
14+
content_heading: String @doc(description: "CMS page content heading")
15+
layout: String @doc(description: "CMS page content heading")
16+
mate_title: String @doc(description: "CMS page meta title")
17+
mate_description: String @doc(description: "CMS page meta description")
18+
mate_keywords: String @doc(description: "CMS page meta keywords")
19+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Magento_CmsGraphQl', __DIR__);

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
"magento/module-url-rewrite-graph-ql": "*",
159159
"magento/module-cms-url-rewrite-graph-ql": "*",
160160
"magento/module-weee-graph-ql": "*",
161+
"magento/module-cms-graph-ql": "*",
161162
"magento/module-grouped-import-export": "*",
162163
"magento/module-grouped-product": "*",
163164
"magento/module-grouped-product-graph-ql": "*",

0 commit comments

Comments
 (0)