Skip to content

Commit 12cbbe5

Browse files
committed
Merge branch 'Hammer_QaulityBacklog_GraphQL_24052022' of github.com:magento-gl/magento2ce into Hammer_QaulityBacklog_GraphQL_24052022
2 parents 2e39fcb + 9dff65d commit 12cbbe5

File tree

4 files changed

+122
-23
lines changed

4 files changed

+122
-23
lines changed

app/code/Magento/CatalogGraphQl/Model/Config/AttributeReader.php

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,61 @@
66

77
namespace Magento\CatalogGraphQl\Model\Config;
88

9+
use Magento\Catalog\Model\Product;
10+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
11+
use Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection;
12+
use Magento\EavGraphQl\Model\Resolver\Query\Type;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
914
use Magento\Framework\Config\ReaderInterface;
1015
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1116
use Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface;
1217
use Magento\Framework\Reflection\TypeProcessor;
13-
use Magento\EavGraphQl\Model\Resolver\Query\Type;
14-
use Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection;
18+
use Magento\Store\Model\ScopeInterface;
1519

1620
/**
1721
* Adds custom/eav attribute to Catalog product types in the GraphQL config.
1822
*/
1923
class AttributeReader implements ReaderInterface
2024
{
25+
public const XML_PATH_INCLUDE_DYNAMIC_ATTRIBUTES =
26+
'web_api/graphql/include_dynamic_attributes_as_entity_type_fields';
27+
2128
/**
2229
* @var MapperInterface
2330
*/
24-
private $mapper;
31+
private MapperInterface $mapper;
2532

2633
/**
2734
* @var Type
2835
*/
29-
private $typeLocator;
36+
private Type $typeLocator;
3037

3138
/**
3239
* @var Collection
3340
*/
34-
private $collection;
41+
private Collection $collection;
42+
43+
/**
44+
* @var ScopeConfigInterface
45+
*/
46+
private ScopeConfigInterface $config;
3547

3648
/**
3749
* @param MapperInterface $mapper
3850
* @param Type $typeLocator
3951
* @param Collection $collection
52+
* @param ScopeConfigInterface $config
4053
*/
4154
public function __construct(
4255
MapperInterface $mapper,
4356
Type $typeLocator,
44-
Collection $collection
57+
Collection $collection,
58+
ScopeConfigInterface $config
4559
) {
4660
$this->mapper = $mapper;
4761
$this->typeLocator = $typeLocator;
4862
$this->collection = $collection;
63+
$this->config = $config;
4964
}
5065

5166
/**
@@ -58,22 +73,25 @@ public function __construct(
5873
*/
5974
public function read($scope = null) : array
6075
{
61-
$typeNames = $this->mapper->getMappedTypes(\Magento\Catalog\Model\Product::ENTITY);
62-
$config =[];
63-
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attribute */
64-
foreach ($this->collection->getAttributes() as $attribute) {
65-
$attributeCode = $attribute->getAttributeCode();
66-
$locatedType = $this->typeLocator->getType(
67-
$attributeCode,
68-
\Magento\Catalog\Model\Product::ENTITY
69-
) ?: 'String';
70-
$locatedType = $locatedType === TypeProcessor::NORMALIZED_ANY_TYPE ? 'String' : ucfirst($locatedType);
71-
foreach ($typeNames as $typeName) {
72-
$config[$typeName]['fields'][$attributeCode] = [
73-
'name' => $attributeCode,
74-
'type' => $locatedType,
75-
'arguments' => []
76-
];
76+
$config = [];
77+
78+
if ($this->config->isSetFlag(self::XML_PATH_INCLUDE_DYNAMIC_ATTRIBUTES, ScopeInterface::SCOPE_STORE)) {
79+
$typeNames = $this->mapper->getMappedTypes(Product::ENTITY);
80+
81+
/** @var Attribute $attribute */
82+
foreach ($this->collection->getAttributes() as $attribute) {
83+
$attributeCode = $attribute->getAttributeCode();
84+
$locatedType = $this->typeLocator->getType($attributeCode, Product::ENTITY) ?: 'String';
85+
$locatedType = TypeProcessor::NORMALIZED_ANY_TYPE === $locatedType ? 'String' : ucfirst($locatedType);
86+
87+
foreach ($typeNames as $typeName) {
88+
$config[$typeName]['fields'][$attributeCode] = [
89+
'name' => $attributeCode,
90+
'type' => $locatedType,
91+
'arguments' => [],
92+
'deprecated' => ['reason' => 'Use the `custom_attributes` field instead.'],
93+
];
94+
}
7795
}
7896
}
7997

app/code/Magento/GraphQl/etc/adminhtml/system.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
</field>
3636
</group>
3737
</section>
38+
<section id="web_api">
39+
<group id="graphql" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
40+
<label>GraphQl</label>
41+
<field id="include_dynamic_attributes_as_entity_type_fields" translate="label" type="select" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1">
42+
<label>Include dynamic attributes as entity type fields</label>
43+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
44+
<config_path>web_api/graphql/include_dynamic_attributes_as_entity_type_fields</config_path>
45+
</field>
46+
</group>
47+
</section>
3848
</system>
3949
</config>
4050

app/code/Magento/GraphQl/etc/config.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
* See COPYING.txt for license details.
66
*/
77
-->
8-
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
910
<default>
11+
<web_api>
12+
<graphql>
13+
<include_dynamic_attributes_as_entity_type_fields>1</include_dynamic_attributes_as_entity_type_fields>
14+
</graphql>
15+
</web_api>
1016
<graphql>
1117
<session>
1218
<disable>0</disable>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\CatalogGraphQl\Model\Config;
9+
10+
use Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
14+
class AttributeReaderTest extends TestCase
15+
{
16+
/** @var AttributeReader */
17+
private $attributeReader;
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
protected function setUp(): void
23+
{
24+
$objectManager = Bootstrap::getObjectManager();
25+
$mapper = $this->createMock(MapperInterface::class);
26+
$mapper->expects($this->any())
27+
->method('getMappedTypes')
28+
->willReturn([
29+
'product' => 'ProductInterface',
30+
'simple' => 'SimpleProduct',
31+
'virtual' => 'VirtualProduct',
32+
'downloadable' => 'DownloadableProduct',
33+
'bundle' => 'BundleProduct',
34+
'grouped' => 'GroupedProduct',
35+
'configurable' => 'ConfigurableProduct',
36+
]);
37+
$this->attributeReader = $objectManager->create(AttributeReader::class, ['mapper' => $mapper]);
38+
}
39+
40+
/**
41+
* @magentoConfigFixture current_store web_api/graphql/include_dynamic_attributes_as_entity_type_fields 1
42+
*/
43+
public function testReadWithIncludeDynamicAttributesOptionEnabled()
44+
{
45+
$result = $this->attributeReader->read();
46+
$this->assertCount(7, $result);
47+
48+
//Adding custom attribute dynamically to the schema is deprecated.
49+
foreach ($result as $typeName) {
50+
if (!isset($typeName['fields'])) {
51+
$this->fail('Invalid config');
52+
}
53+
54+
array_map(fn ($attribute) => $this->assertArrayHasKey('deprecated', $attribute), $typeName['fields']);
55+
}
56+
}
57+
58+
/**
59+
* @magentoConfigFixture current_store web_api/graphql/include_dynamic_attributes_as_entity_type_fields 0
60+
*/
61+
public function testReadWithIncludeDynamicAttributesOptionDisabled()
62+
{
63+
$this->assertEmpty($this->attributeReader->read());
64+
}
65+
}

0 commit comments

Comments
 (0)