From 7674eb1713c7f348cd7fc8b8bc3c7643b49a3638 Mon Sep 17 00:00:00 2001 From: Artem Klimov Date: Sat, 30 Jun 2018 17:46:48 +0300 Subject: [PATCH] 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") {