Skip to content

Commit

Permalink
87 Fetch attribute values and labels for customAttributeMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
klimart committed Jun 30, 2018
1 parent f947cad commit 7674eb1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
88 changes: 88 additions & 0 deletions app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\EavGraphQl\Model\Resolver;

use Magento\Eav\Api\AttributeOptionManagementInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolve attribute options data for custom attribute.
*/
class AttributeOptions implements ResolverInterface
{
/**
* @var AttributeOptionManagementInterface
*/
protected $optionManager;

/**
* @var ValueFactory
*/
protected $valueFactory;

/**
* AttributeOptions constructor.
*
* @param AttributeOptionManagementInterface $optionManager
* @param ValueFactory $valueFactory
*/
public function __construct(
AttributeOptionManagementInterface $optionManager,
ValueFactory $valueFactory
) {
$this->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);
}
}
6 changes: 6 additions & 0 deletions app/code/Magento/EavGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 7674eb1

Please sign in to comment.