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..6ccd610bead0d --- /dev/null +++ b/app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php @@ -0,0 +1,114 @@ +attributeOptionsDataProvider = $attributeOptionsDataProvider; + $this->valueFactory = $valueFactory; + } + + /** + * @inheritDoc + */ + public function resolve( + Field $field, + $context, + ResolveInfo $info, + array $value = null, + array $args = null + ) : Value { + + return $this->valueFactory->create(function () use ($value) { + $entityType = $this->getEntityType($value); + $attributeCode = $this->getAttributeCode($value); + + $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')); + } + + return (int)$value['entity_type']; + } + + /** + * @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')); + } + + return $value['attribute_code']; + } + + /** + * @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; + } +} diff --git a/app/code/Magento/EavGraphQl/composer.json b/app/code/Magento/EavGraphQl/composer.json index 6da27ed27cf36..a2c2d025a3d9d 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/module-eav": "*" }, "suggest": { "magento/module-graph-ql": "*" 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") {