Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit a9361b9

Browse files
author
Valeriy Nayda
committed
GraphQL-87: Fetch attribute values and labels for customAttributeMetadata
1 parent 3af924b commit a9361b9

File tree

2 files changed

+114
-34
lines changed

2 files changed

+114
-34
lines changed

app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php

Lines changed: 60 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88
namespace Magento\EavGraphQl\Model\Resolver;
99

10-
use Magento\Eav\Api\AttributeOptionManagementInterface;
10+
use Magento\EavGraphQl\Model\Resolver\DataProvider\AttributeOptions as AttributeOptionsDataProvider;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Exception\StateException;
1113
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1216
use Magento\Framework\GraphQl\Query\Resolver\Value;
1317
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1418
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -20,31 +24,29 @@
2024
class AttributeOptions implements ResolverInterface
2125
{
2226
/**
23-
* @var AttributeOptionManagementInterface
27+
* @var AttributeOptionsDataProvider
2428
*/
25-
protected $optionManager;
29+
private $attributeOptionsDataProvider;
2630

2731
/**
28-
* @var ValueFactory
32+
* @var AttributeOptions
2933
*/
30-
protected $valueFactory;
34+
private $valueFactory;
3135

3236
/**
33-
* AttributeOptions constructor.
34-
*
35-
* @param AttributeOptionManagementInterface $optionManager
37+
* @param AttributeOptionsDataProvider $attributeOptionsDataProvider
3638
* @param ValueFactory $valueFactory
3739
*/
3840
public function __construct(
39-
AttributeOptionManagementInterface $optionManager,
41+
AttributeOptionsDataProvider $attributeOptionsDataProvider,
4042
ValueFactory $valueFactory
4143
) {
42-
$this->optionManager = $optionManager;
44+
$this->attributeOptionsDataProvider = $attributeOptionsDataProvider;
4345
$this->valueFactory = $valueFactory;
4446
}
4547

4648
/**
47-
* {@inheritDoc}
49+
* @inheritDoc
4850
*/
4951
public function resolve(
5052
Field $field,
@@ -53,36 +55,60 @@ public function resolve(
5355
array $value = null,
5456
array $args = null
5557
) : Value {
56-
$options = [];
5758

58-
$entityType = !empty($value['entity_type']) ? $value['entity_type'] : '';
59-
$attributeCode = !empty($value['attribute_code']) ? $value['attribute_code'] : '';
59+
return $this->valueFactory->create(function () use ($value) {
60+
$entityType = $this->getEntityType($value);
61+
$attributeCode = $this->getAttributeCode($value);
6062

61-
try {
62-
/** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $attributeOptions */
63-
$attributeOptions = $this->optionManager->getItems($entityType, $attributeCode);
64-
} catch (\Exception $e) {
65-
$attributeOptions = [];
63+
$optionsData = $this->getAttributeOptionsData($entityType, $attributeCode);
64+
return $optionsData;
65+
});
66+
}
67+
68+
/**
69+
* @param array $value
70+
* @return int
71+
* @throws GraphQlInputException
72+
*/
73+
private function getEntityType(array $value): int
74+
{
75+
if (!isset($value['entity_type'])) {
76+
throw new GraphQlInputException(__('"Entity type should be specified'));
6677
}
6778

68-
if (is_array($attributeOptions)) {
69-
/** @var \Magento\Eav\Api\Data\AttributeOptionInterface $option */
70-
foreach ($attributeOptions as $option) {
71-
if ($option->getValue() === '') {
72-
continue;
73-
}
79+
return (int)$value['entity_type'];
80+
}
7481

75-
$options[] = [
76-
'label' => $option->getLabel(),
77-
'value' => $option->getValue()
78-
];
79-
}
82+
/**
83+
* @param array $value
84+
* @return string
85+
* @throws GraphQlInputException
86+
*/
87+
private function getAttributeCode(array $value): string
88+
{
89+
if (!isset($value['attribute_code'])) {
90+
throw new GraphQlInputException(__('"Attribute code should be specified'));
8091
}
8192

82-
$result = function () use ($options) {
83-
return $options;
84-
};
93+
return $value['attribute_code'];
94+
}
8595

86-
return $this->valueFactory->create($result);
96+
/**
97+
* @param int $entityType
98+
* @param string $attributeCode
99+
* @return array
100+
* @throws GraphQlInputException
101+
* @throws GraphQlNoSuchEntityException
102+
*/
103+
private function getAttributeOptionsData(int $entityType, string $attributeCode): array
104+
{
105+
try {
106+
$optionsData = $this->attributeOptionsDataProvider->getData($entityType, $attributeCode);
107+
} catch (InputException $e) {
108+
throw new GraphQlInputException(__($e->getMessage()), $e);
109+
} catch (StateException $e) {
110+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
111+
}
112+
return $optionsData;
87113
}
88114
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\EavGraphQl\Model\Resolver\DataProvider;
9+
10+
use Magento\Eav\Api\AttributeOptionManagementInterface;
11+
12+
/**
13+
* Attribute Options data provider
14+
*/
15+
class AttributeOptions
16+
{
17+
/**
18+
* @var AttributeOptionManagementInterface
19+
*/
20+
private $optionManager;
21+
22+
/**
23+
* @param AttributeOptionManagementInterface $optionManager
24+
*/
25+
public function __construct(
26+
AttributeOptionManagementInterface $optionManager
27+
) {
28+
$this->optionManager = $optionManager;
29+
}
30+
31+
/**
32+
* @param int $entityType
33+
* @param string $attributeCode
34+
* @return array
35+
*/
36+
public function getData(int $entityType, string $attributeCode): array
37+
{
38+
$options = $this->optionManager->getItems($entityType, $attributeCode);
39+
40+
$optionsData = [];
41+
foreach ($options as $option) {
42+
// without empty option @see \Magento\Eav\Model\Entity\Attribute\Source\Table::getAllOptions
43+
if ($option->getValue() === '') {
44+
continue;
45+
}
46+
47+
$optionsData[] = [
48+
'label' => $option->getLabel(),
49+
'value' => $option->getValue()
50+
];
51+
}
52+
return $optionsData;
53+
}
54+
}

0 commit comments

Comments
 (0)