Skip to content

Commit c766d6c

Browse files
authored
Merge pull request #8222 from magento-lynx/eav-graphql
2 parents 9649f62 + 6b6169f commit c766d6c

37 files changed

+2655
-6
lines changed

app/code/Magento/Catalog/Test/Fixture/Product.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,7 @@ public function apply(array $data = []): ?DataObject
120120
public function revert(DataObject $data): void
121121
{
122122
$service = $this->serviceFactory->create(ProductRepositoryInterface::class, 'deleteById');
123-
$service->execute(
124-
[
125-
'sku' => $data->getSku()
126-
]
127-
);
123+
$service->execute(['sku' => $data->getSku()]);
128124
}
129125

130126
/**

app/code/Magento/CatalogGraphQl/etc/graphql/di.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,20 @@
208208
</argument>
209209
</arguments>
210210
</virtualType>
211+
<type name="Magento\EavGraphQl\Model\TypeResolver\AttributeMetadata">
212+
<arguments>
213+
<argument name="entityTypes" xsi:type="array">
214+
<item name="PRODUCT" xsi:type="string">CatalogAttributeMetadata</item>
215+
</argument>
216+
</arguments>
217+
</type>
218+
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
219+
<arguments>
220+
<argument name="map" xsi:type="array">
221+
<item name="AttributeEntityTypeEnum" xsi:type="array">
222+
<item name="catalog_product" xsi:type="string">catalog_product</item>
223+
</item>
224+
</argument>
225+
</arguments>
226+
</type>
211227
</config>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,7 @@ type SimpleWishlistItem implements WishlistItemInterface @doc(description: "Cont
531531

532532
type VirtualWishlistItem implements WishlistItemInterface @doc(description: "Contains a virtual product wish list item.") {
533533
}
534+
535+
enum AttributeEntityTypeEnum {
536+
CATALOG_PRODUCT
537+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\Customer\Test\Fixture;
9+
10+
use Magento\Customer\Model\Attribute;
11+
use Magento\Customer\Model\ResourceModel\Attribute as ResourceModelAttribute;
12+
use Magento\Eav\Api\AttributeRepositoryInterface;
13+
use Magento\Eav\Model\AttributeFactory;
14+
use Magento\Framework\DataObject;
15+
use Magento\Framework\Exception\InvalidArgumentException;
16+
use Magento\TestFramework\Fixture\Api\DataMerger;
17+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
18+
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;
19+
20+
class CustomerAttribute implements RevertibleDataFixtureInterface
21+
{
22+
private const DEFAULT_DATA = [
23+
'entity_type_id' => null,
24+
'attribute_id' => null,
25+
'attribute_code' => 'attribute%uniqid%',
26+
'default_frontend_label' => 'Attribute%uniqid%',
27+
'frontend_labels' => [],
28+
'frontend_input' => 'text',
29+
'backend_type' => 'varchar',
30+
'is_required' => false,
31+
'is_user_defined' => true,
32+
'note' => null,
33+
'backend_model' => null,
34+
'source_model' => null,
35+
'default_value' => null,
36+
'is_unique' => '0',
37+
'frontend_class' => null,
38+
'used_in_forms' => [],
39+
];
40+
41+
/**
42+
* @var DataMerger
43+
*/
44+
private DataMerger $dataMerger;
45+
46+
/**
47+
* @var ProcessorInterface
48+
*/
49+
private ProcessorInterface $processor;
50+
51+
/**
52+
* @var AttributeFactory
53+
*/
54+
private AttributeFactory $attributeFactory;
55+
56+
/**
57+
* @var ResourceModelAttribute
58+
*/
59+
private ResourceModelAttribute $resourceModelAttribute;
60+
61+
/**
62+
* @var AttributeRepositoryInterface
63+
*/
64+
private AttributeRepositoryInterface $attributeRepository;
65+
66+
/**
67+
* @param DataMerger $dataMerger
68+
* @param ProcessorInterface $processor
69+
* @param AttributeRepositoryInterface $attributeRepository
70+
* @param AttributeFactory $attributeFactory
71+
* @param ResourceModelAttribute $resourceModelAttribute
72+
*/
73+
public function __construct(
74+
DataMerger $dataMerger,
75+
ProcessorInterface $processor,
76+
AttributeRepositoryInterface $attributeRepository,
77+
AttributeFactory $attributeFactory,
78+
ResourceModelAttribute $resourceModelAttribute
79+
) {
80+
$this->dataMerger = $dataMerger;
81+
$this->processor = $processor;
82+
$this->attributeFactory = $attributeFactory;
83+
$this->resourceModelAttribute = $resourceModelAttribute;
84+
$this->attributeRepository = $attributeRepository;
85+
}
86+
87+
/**
88+
* @inheritdoc
89+
*/
90+
public function apply(array $data = []): ?DataObject
91+
{
92+
if (empty($data['entity_type_id'])) {
93+
throw new InvalidArgumentException(
94+
__(
95+
'"%field" value is required to create an attribute',
96+
[
97+
'field' => 'entity_type_id'
98+
]
99+
)
100+
);
101+
}
102+
103+
/** @var Attribute $attr */
104+
$attr = $this->attributeFactory->createAttribute(Attribute::class, self::DEFAULT_DATA);
105+
$mergedData = $this->processor->process($this, $this->dataMerger->merge(self::DEFAULT_DATA, $data));
106+
$attr->setData($mergedData);
107+
$this->resourceModelAttribute->save($attr);
108+
return $attr;
109+
}
110+
111+
/**
112+
* @inheritdoc
113+
*/
114+
public function revert(DataObject $data): void
115+
{
116+
$this->attributeRepository->deleteById($data['attribute_id']);
117+
}
118+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Customer\Api\MetadataInterface;
11+
use Magento\EavGraphQl\Model\GetAttributesFormInterface;
12+
use Magento\EavGraphQl\Model\Uid;
13+
14+
/**
15+
* Attributes form provider for customer
16+
*/
17+
class GetAttributesForm implements GetAttributesFormInterface
18+
{
19+
/**
20+
* @var MetadataInterface
21+
*/
22+
private MetadataInterface $entity;
23+
24+
/**
25+
* @var Uid
26+
*/
27+
private Uid $uid;
28+
29+
/**
30+
* @var string
31+
*/
32+
private string $type;
33+
34+
/**
35+
* @param MetadataInterface $metadata
36+
* @param Uid $uid
37+
* @param string $type
38+
*/
39+
public function __construct(MetadataInterface $metadata, Uid $uid, string $type)
40+
{
41+
$this->entity = $metadata;
42+
$this->uid = $uid;
43+
$this->type = $type;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function execute(string $formCode): ?array
50+
{
51+
$attributes = [];
52+
foreach ($this->entity->getAttributes($formCode) as $attribute) {
53+
$attributes[] = $this->uid->encode($this->type, $attribute->getAttributeCode());
54+
}
55+
return $attributes;
56+
}
57+
}

app/code/Magento/CustomerGraphQl/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"magento/module-authorization": "*",
88
"magento/module-customer": "*",
99
"magento/module-eav": "*",
10+
"magento/module-eav-graph-ql": "*",
1011
"magento/module-graph-ql": "*",
1112
"magento/module-newsletter": "*",
1213
"magento/module-integration": "*",

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,34 @@
6262
</argument>
6363
</arguments>
6464
</type>
65+
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
66+
<arguments>
67+
<argument name="map" xsi:type="array">
68+
<item name="AttributeEntityTypeEnum" xsi:type="array">
69+
<item name="customer" xsi:type="string">customer</item>
70+
<item name="customer_address" xsi:type="string">customer_address</item>
71+
</item>
72+
</argument>
73+
</arguments>
74+
</type>
75+
<type name="Magento\EavGraphQl\Model\GetAttributesFormComposite">
76+
<arguments>
77+
<argument name="providers" xsi:type="array">
78+
<item name="customer" xsi:type="object">GetCustomerAttributesForm</item>
79+
<item name="customer_address" xsi:type="object">GetCustomerAddressAttributesForm</item>
80+
</argument>
81+
</arguments>
82+
</type>
83+
<virtualType name="GetCustomerAttributesForm" type="Magento\CustomerGraphQl\Model\Customer\GetAttributesForm">
84+
<arguments>
85+
<argument name="metadata" xsi:type="object">Magento\Customer\Api\CustomerMetadataInterface</argument>
86+
<argument name="type" xsi:type="string">customer</argument>
87+
</arguments>
88+
</virtualType>
89+
<virtualType name="GetCustomerAddressAttributesForm" type="Magento\CustomerGraphQl\Model\Customer\GetAttributesForm">
90+
<arguments>
91+
<argument name="metadata" xsi:type="object">Magento\Customer\Api\AddressMetadataInterface</argument>
92+
<argument name="type" xsi:type="string">customer_address</argument>
93+
</arguments>
94+
</virtualType>
6595
</config>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,8 @@ enum CountryCodeEnum @doc(description: "The list of country codes.") {
425425
ZM @doc(description: "Zambia")
426426
ZW @doc(description: "Zimbabwe")
427427
}
428+
429+
enum AttributeEntityTypeEnum {
430+
CUSTOMER
431+
CUSTOMER_ADDRESS
432+
}

app/code/Magento/Eav/Model/Entity/Attribute/OptionManagement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private function saveOption(
139139
$options = [];
140140
$options['value'][$optionId][0] = $optionLabel;
141141
$options['order'][$optionId] = $option->getSortOrder();
142+
$options['is_default'][$optionId] = $option->getIsDefault();
142143
if (is_array($option->getStoreLabels())) {
143144
foreach ($option->getStoreLabels() as $label) {
144145
$options['value'][$optionId][$label->getStoreId()] = $label->getLabel();

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ protected function _afterDelete(AbstractModel $object)
256256
* Returns config instance
257257
*
258258
* @return Config
259-
* @deprecated 100.0.7
260259
*/
261260
private function getConfig()
262261
{
@@ -388,6 +387,10 @@ protected function _saveOption(AbstractModel $object)
388387
$defaultValue = $this->_processAttributeOptions($object, $option);
389388
}
390389

390+
if ($object->getDefaultValue()) {
391+
$defaultValue[] = $object->getDefaultValue();
392+
}
393+
391394
$this->_saveDefaultValue($object, $defaultValue);
392395
return $this;
393396
}

0 commit comments

Comments
 (0)