Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\EavGraphQl\Model\Resolver;

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;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolve attribute options data for custom attribute.
*/
class AttributeOptions implements ResolverInterface
{
/**
* @var AttributeOptionsDataProvider
*/
private $attributeOptionsDataProvider;

/**
* @var AttributeOptions
*/
private $valueFactory;

/**
* @param AttributeOptionsDataProvider $attributeOptionsDataProvider
* @param ValueFactory $valueFactory
*/
public function __construct(
AttributeOptionsDataProvider $attributeOptionsDataProvider,
ValueFactory $valueFactory
) {
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\EavGraphQl\Model\Resolver\DataProvider;

use Magento\Eav\Api\AttributeOptionManagementInterface;

/**
* Attribute Options data provider
*/
class AttributeOptions
{
/**
* @var AttributeOptionManagementInterface
*/
private $optionManager;

/**
* @param AttributeOptionManagementInterface $optionManager
*/
public function __construct(
AttributeOptionManagementInterface $optionManager
) {
$this->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;
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/EavGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
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