Skip to content

Commit

Permalink
Merge pull request #8222 from magento-lynx/eav-graphql
Browse files Browse the repository at this point in the history
  • Loading branch information
svera authored Apr 10, 2023
2 parents 9649f62 + 6b6169f commit c766d6c
Show file tree
Hide file tree
Showing 37 changed files with 2,655 additions and 6 deletions.
6 changes: 1 addition & 5 deletions app/code/Magento/Catalog/Test/Fixture/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ public function apply(array $data = []): ?DataObject
public function revert(DataObject $data): void
{
$service = $this->serviceFactory->create(ProductRepositoryInterface::class, 'deleteById');
$service->execute(
[
'sku' => $data->getSku()
]
);
$service->execute(['sku' => $data->getSku()]);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,20 @@
</argument>
</arguments>
</virtualType>
<type name="Magento\EavGraphQl\Model\TypeResolver\AttributeMetadata">
<arguments>
<argument name="entityTypes" xsi:type="array">
<item name="PRODUCT" xsi:type="string">CatalogAttributeMetadata</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
<arguments>
<argument name="map" xsi:type="array">
<item name="AttributeEntityTypeEnum" xsi:type="array">
<item name="catalog_product" xsi:type="string">catalog_product</item>
</item>
</argument>
</arguments>
</type>
</config>
4 changes: 4 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,7 @@ type SimpleWishlistItem implements WishlistItemInterface @doc(description: "Cont

type VirtualWishlistItem implements WishlistItemInterface @doc(description: "Contains a virtual product wish list item.") {
}

enum AttributeEntityTypeEnum {
CATALOG_PRODUCT
}
118 changes: 118 additions & 0 deletions app/code/Magento/Customer/Test/Fixture/CustomerAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Customer\Test\Fixture;

use Magento\Customer\Model\Attribute;
use Magento\Customer\Model\ResourceModel\Attribute as ResourceModelAttribute;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\AttributeFactory;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\InvalidArgumentException;
use Magento\TestFramework\Fixture\Api\DataMerger;
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
use Magento\TestFramework\Fixture\RevertibleDataFixtureInterface;

class CustomerAttribute implements RevertibleDataFixtureInterface
{
private const DEFAULT_DATA = [
'entity_type_id' => null,
'attribute_id' => null,
'attribute_code' => 'attribute%uniqid%',
'default_frontend_label' => 'Attribute%uniqid%',
'frontend_labels' => [],
'frontend_input' => 'text',
'backend_type' => 'varchar',
'is_required' => false,
'is_user_defined' => true,
'note' => null,
'backend_model' => null,
'source_model' => null,
'default_value' => null,
'is_unique' => '0',
'frontend_class' => null,
'used_in_forms' => [],
];

/**
* @var DataMerger
*/
private DataMerger $dataMerger;

/**
* @var ProcessorInterface
*/
private ProcessorInterface $processor;

/**
* @var AttributeFactory
*/
private AttributeFactory $attributeFactory;

/**
* @var ResourceModelAttribute
*/
private ResourceModelAttribute $resourceModelAttribute;

/**
* @var AttributeRepositoryInterface
*/
private AttributeRepositoryInterface $attributeRepository;

/**
* @param DataMerger $dataMerger
* @param ProcessorInterface $processor
* @param AttributeRepositoryInterface $attributeRepository
* @param AttributeFactory $attributeFactory
* @param ResourceModelAttribute $resourceModelAttribute
*/
public function __construct(
DataMerger $dataMerger,
ProcessorInterface $processor,
AttributeRepositoryInterface $attributeRepository,
AttributeFactory $attributeFactory,
ResourceModelAttribute $resourceModelAttribute
) {
$this->dataMerger = $dataMerger;
$this->processor = $processor;
$this->attributeFactory = $attributeFactory;
$this->resourceModelAttribute = $resourceModelAttribute;
$this->attributeRepository = $attributeRepository;
}

/**
* @inheritdoc
*/
public function apply(array $data = []): ?DataObject
{
if (empty($data['entity_type_id'])) {
throw new InvalidArgumentException(
__(
'"%field" value is required to create an attribute',
[
'field' => 'entity_type_id'
]
)
);
}

/** @var Attribute $attr */
$attr = $this->attributeFactory->createAttribute(Attribute::class, self::DEFAULT_DATA);
$mergedData = $this->processor->process($this, $this->dataMerger->merge(self::DEFAULT_DATA, $data));
$attr->setData($mergedData);
$this->resourceModelAttribute->save($attr);
return $attr;
}

/**
* @inheritdoc
*/
public function revert(DataObject $data): void
{
$this->attributeRepository->deleteById($data['attribute_id']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\MetadataInterface;
use Magento\EavGraphQl\Model\GetAttributesFormInterface;
use Magento\EavGraphQl\Model\Uid;

/**
* Attributes form provider for customer
*/
class GetAttributesForm implements GetAttributesFormInterface
{
/**
* @var MetadataInterface
*/
private MetadataInterface $entity;

/**
* @var Uid
*/
private Uid $uid;

/**
* @var string
*/
private string $type;

/**
* @param MetadataInterface $metadata
* @param Uid $uid
* @param string $type
*/
public function __construct(MetadataInterface $metadata, Uid $uid, string $type)
{
$this->entity = $metadata;
$this->uid = $uid;
$this->type = $type;
}

/**
* @inheritDoc
*/
public function execute(string $formCode): ?array
{
$attributes = [];
foreach ($this->entity->getAttributes($formCode) as $attribute) {
$attributes[] = $this->uid->encode($this->type, $attribute->getAttributeCode());
}
return $attributes;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CustomerGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"magento/module-authorization": "*",
"magento/module-customer": "*",
"magento/module-eav": "*",
"magento/module-eav-graph-ql": "*",
"magento/module-graph-ql": "*",
"magento/module-newsletter": "*",
"magento/module-integration": "*",
Expand Down
30 changes: 30 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,34 @@
</argument>
</arguments>
</type>
<type name="Magento\Framework\GraphQl\Schema\Type\Enum\DefaultDataMapper">
<arguments>
<argument name="map" xsi:type="array">
<item name="AttributeEntityTypeEnum" xsi:type="array">
<item name="customer" xsi:type="string">customer</item>
<item name="customer_address" xsi:type="string">customer_address</item>
</item>
</argument>
</arguments>
</type>
<type name="Magento\EavGraphQl\Model\GetAttributesFormComposite">
<arguments>
<argument name="providers" xsi:type="array">
<item name="customer" xsi:type="object">GetCustomerAttributesForm</item>
<item name="customer_address" xsi:type="object">GetCustomerAddressAttributesForm</item>
</argument>
</arguments>
</type>
<virtualType name="GetCustomerAttributesForm" type="Magento\CustomerGraphQl\Model\Customer\GetAttributesForm">
<arguments>
<argument name="metadata" xsi:type="object">Magento\Customer\Api\CustomerMetadataInterface</argument>
<argument name="type" xsi:type="string">customer</argument>
</arguments>
</virtualType>
<virtualType name="GetCustomerAddressAttributesForm" type="Magento\CustomerGraphQl\Model\Customer\GetAttributesForm">
<arguments>
<argument name="metadata" xsi:type="object">Magento\Customer\Api\AddressMetadataInterface</argument>
<argument name="type" xsi:type="string">customer_address</argument>
</arguments>
</virtualType>
</config>
5 changes: 5 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,8 @@ enum CountryCodeEnum @doc(description: "The list of country codes.") {
ZM @doc(description: "Zambia")
ZW @doc(description: "Zimbabwe")
}

enum AttributeEntityTypeEnum {
CUSTOMER
CUSTOMER_ADDRESS
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private function saveOption(
$options = [];
$options['value'][$optionId][0] = $optionLabel;
$options['order'][$optionId] = $option->getSortOrder();
$options['is_default'][$optionId] = $option->getIsDefault();
if (is_array($option->getStoreLabels())) {
foreach ($option->getStoreLabels() as $label) {
$options['value'][$optionId][$label->getStoreId()] = $label->getLabel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ protected function _afterDelete(AbstractModel $object)
* Returns config instance
*
* @return Config
* @deprecated 100.0.7
*/
private function getConfig()
{
Expand Down Expand Up @@ -388,6 +387,10 @@ protected function _saveOption(AbstractModel $object)
$defaultValue = $this->_processAttributeOptions($object, $option);
}

if ($object->getDefaultValue()) {
$defaultValue[] = $object->getDefaultValue();
}

$this->_saveDefaultValue($object, $defaultValue);
return $this;
}
Expand Down
Loading

0 comments on commit c766d6c

Please sign in to comment.