From 7674eb1713c7f348cd7fc8b8bc3c7643b49a3638 Mon Sep 17 00:00:00 2001 From: Artem Klimov Date: Sat, 30 Jun 2018 17:46:48 +0300 Subject: [PATCH 1/5] 87 Fetch attribute values and labels for customAttributeMetadata --- .../Model/Resolver/AttributeOptions.php | 88 +++++++++++++++++++ .../Magento/EavGraphQl/etc/schema.graphqls | 6 ++ 2 files changed, 94 insertions(+) create mode 100644 app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php new file mode 100644 index 0000000000000..1c341012083b9 --- /dev/null +++ b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php @@ -0,0 +1,88 @@ +optionManager = $optionManager; + $this->valueFactory = $valueFactory; + } + + /** + * {@inheritDoc} + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) : Value { + $options = []; + + $entityType = !empty($value['entity_type']) ? $value['entity_type'] : ''; + $attributeCode = !empty($value['attribute_code']) ? $value['attribute_code'] : ''; + + try { + /** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $attributeOptions */ + $attributeOptions = $this->optionManager->getItems($entityType, $attributeCode); + } catch (\Exception $e) { + $attributeOptions = []; + } + + if (is_array($attributeOptions)) { + /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */ + foreach ($attributeOptions as $option) { + if (!$option->getValue()) { + continue; + } + + $options[] = [ + 'label' => $option->getLabel(), + 'value' => $option->getValue() + ]; + } + } + + $result = function () use ($options) { + return $options; + }; + + return $this->valueFactory->create($result); + } +} diff --git a/app/code/Magento/EavGraphQl/etc/schema.graphqls b/app/code/Magento/EavGraphQl/etc/schema.graphqls index 7799498c40409..adada3030f501 100644 --- a/app/code/Magento/EavGraphQl/etc/schema.graphqls +++ b/app/code/Magento/EavGraphQl/etc/schema.graphqls @@ -13,6 +13,12 @@ type Attribute @doc(description: "Attribute contains the attribute_type of the s attribute_code: String @doc(description: "The unique identifier for an attribute code. This value should be in lowercase letters without spaces.") entity_type: String @doc(description: "The type of entity that defines the attribute") attribute_type: String @doc(description: "The data type of the attribute") + attribute_options: [AttributeOption] @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributeOptions") @doc(description: "Attribute options list.") +} + +type AttributeOption @doc(description: "Attribute option.") { + label: String @doc(description: "Attribute option label.") + value: String @doc(description: "Attribute option value.") } input AttributeInput @doc(description: "AttributeInput specifies the attribute_code and entity_type to search") { From 4aa808e537bf7526d81a3bc18910ad2ddcc8400f Mon Sep 17 00:00:00 2001 From: Artem Klimov Date: Sun, 22 Jul 2018 21:02:59 +0300 Subject: [PATCH 2/5] Fixed skipping of empty option --- app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php index 1c341012083b9..72f483b7445bb 100644 --- a/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php +++ b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php @@ -68,7 +68,7 @@ public function resolve( if (is_array($attributeOptions)) { /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */ foreach ($attributeOptions as $option) { - if (!$option->getValue()) { + if ($option->getValue() === '') { continue; } From a9361b9b1c8df62246112263ee066c02a43734bd Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 20 Aug 2018 15:33:04 +0300 Subject: [PATCH 3/5] GraphQL-87: Fetch attribute values and labels for customAttributeMetadata --- .../Model/Resolver/AttributeOptions.php | 94 ++++++++++++------- .../DataProvider/AttributeOptions.php | 54 +++++++++++ 2 files changed, 114 insertions(+), 34 deletions(-) create mode 100644 app/code/Magento/EavGraphQl/Model/Resolver/DataProvider/AttributeOptions.php diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php index 72f483b7445bb..6ccd610bead0d 100644 --- a/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php +++ b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php @@ -7,8 +7,12 @@ namespace Magento\EavGraphQl\Model\Resolver; -use Magento\Eav\Api\AttributeOptionManagementInterface; +use Magento\EavGraphQl\Model\Resolver\DataProvider\AttributeOptions as AttributeOptionsDataProvider; +use Magento\Framework\Exception\InputException; +use Magento\Framework\Exception\StateException; use Magento\Framework\GraphQl\Config\Element\Field; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; use Magento\Framework\GraphQl\Query\Resolver\Value; use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -20,31 +24,29 @@ class AttributeOptions implements ResolverInterface { /** - * @var AttributeOptionManagementInterface + * @var AttributeOptionsDataProvider */ - protected $optionManager; + private $attributeOptionsDataProvider; /** - * @var ValueFactory + * @var AttributeOptions */ - protected $valueFactory; + private $valueFactory; /** - * AttributeOptions constructor. - * - * @param AttributeOptionManagementInterface $optionManager + * @param AttributeOptionsDataProvider $attributeOptionsDataProvider * @param ValueFactory $valueFactory */ public function __construct( - AttributeOptionManagementInterface $optionManager, + AttributeOptionsDataProvider $attributeOptionsDataProvider, ValueFactory $valueFactory ) { - $this->optionManager = $optionManager; + $this->attributeOptionsDataProvider = $attributeOptionsDataProvider; $this->valueFactory = $valueFactory; } /** - * {@inheritDoc} + * @inheritDoc */ public function resolve( Field $field, @@ -53,36 +55,60 @@ public function resolve( array $value = null, array $args = null ) : Value { - $options = []; - $entityType = !empty($value['entity_type']) ? $value['entity_type'] : ''; - $attributeCode = !empty($value['attribute_code']) ? $value['attribute_code'] : ''; + return $this->valueFactory->create(function () use ($value) { + $entityType = $this->getEntityType($value); + $attributeCode = $this->getAttributeCode($value); - try { - /** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $attributeOptions */ - $attributeOptions = $this->optionManager->getItems($entityType, $attributeCode); - } catch (\Exception $e) { - $attributeOptions = []; + $optionsData = $this->getAttributeOptionsData($entityType, $attributeCode); + return $optionsData; + }); + } + + /** + * @param array $value + * @return int + * @throws GraphQlInputException + */ + private function getEntityType(array $value): int + { + if (!isset($value['entity_type'])) { + throw new GraphQlInputException(__('"Entity type should be specified')); } - if (is_array($attributeOptions)) { - /** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */ - foreach ($attributeOptions as $option) { - if ($option->getValue() === '') { - continue; - } + return (int)$value['entity_type']; + } - $options[] = [ - 'label' => $option->getLabel(), - 'value' => $option->getValue() - ]; - } + /** + * @param array $value + * @return string + * @throws GraphQlInputException + */ + private function getAttributeCode(array $value): string + { + if (!isset($value['attribute_code'])) { + throw new GraphQlInputException(__('"Attribute code should be specified')); } - $result = function () use ($options) { - return $options; - }; + return $value['attribute_code']; + } - return $this->valueFactory->create($result); + /** + * @param int $entityType + * @param string $attributeCode + * @return array + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + private function getAttributeOptionsData(int $entityType, string $attributeCode): array + { + try { + $optionsData = $this->attributeOptionsDataProvider->getData($entityType, $attributeCode); + } catch (InputException $e) { + throw new GraphQlInputException(__($e->getMessage()), $e); + } catch (StateException $e) { + throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); + } + return $optionsData; } } diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/DataProvider/AttributeOptions.php b/app/code/Magento/EavGraphQl/Model/Resolver/DataProvider/AttributeOptions.php new file mode 100644 index 0000000000000..900a31c1093ed --- /dev/null +++ b/app/code/Magento/EavGraphQl/Model/Resolver/DataProvider/AttributeOptions.php @@ -0,0 +1,54 @@ +optionManager = $optionManager; + } + + /** + * @param int $entityType + * @param string $attributeCode + * @return array + */ + public function getData(int $entityType, string $attributeCode): array + { + $options = $this->optionManager->getItems($entityType, $attributeCode); + + $optionsData = []; + foreach ($options as $option) { + // without empty option @see \Magento\Eav\Model\Entity\Attribute\Source\Table::getAllOptions + if ($option->getValue() === '') { + continue; + } + + $optionsData[] = [ + 'label' => $option->getLabel(), + 'value' => $option->getValue() + ]; + } + return $optionsData; + } +} From b77f96e83998e3b305390ef0ba0902b7a2d90211 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 20 Aug 2018 16:01:22 +0300 Subject: [PATCH 4/5] GraphQL-87: Fetch attribute values and labels for customAttributeMetadata --- app/code/Magento/EavGraphQl/composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/EavGraphQl/composer.json b/app/code/Magento/EavGraphQl/composer.json index 6da27ed27cf36..b878a0ec3aa68 100644 --- a/app/code/Magento/EavGraphQl/composer.json +++ b/app/code/Magento/EavGraphQl/composer.json @@ -4,7 +4,8 @@ "type": "magento2-module", "require": { "php": "~7.1.3||~7.2.0", - "magento/framework": "*" + "magento/framework": "*", + "magento/eav": "*" }, "suggest": { "magento/module-graph-ql": "*" From a84e6d85fd0cb6f2b9c8541a2cefc5a2d087aef9 Mon Sep 17 00:00:00 2001 From: Valeriy Nayda Date: Mon, 20 Aug 2018 16:30:24 +0300 Subject: [PATCH 5/5] GraphQL-87: Fetch attribute values and labels for customAttributeMetadata --- app/code/Magento/EavGraphQl/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/EavGraphQl/composer.json b/app/code/Magento/EavGraphQl/composer.json index b878a0ec3aa68..a2c2d025a3d9d 100644 --- a/app/code/Magento/EavGraphQl/composer.json +++ b/app/code/Magento/EavGraphQl/composer.json @@ -5,7 +5,7 @@ "require": { "php": "~7.1.3||~7.2.0", "magento/framework": "*", - "magento/eav": "*" + "magento/module-eav": "*" }, "suggest": { "magento/module-graph-ql": "*"