Skip to content

Commit ab5976e

Browse files
ENGCOM-2840: 87 Fetch attribute values and labels for customAttributeMetadata #108
- Merge Pull Request magento/graphql-ce#108 from magento/graphql-ce:87-Fetch-attribute-values-and-labels-for-customAttributeMetadata - Merged commits: 1. 7674eb1 2. 4aa808e 3. 3af924b 4. a9361b9 5. b77f96e 6. a84e6d8
2 parents 6d3dab2 + a84e6d8 commit ab5976e

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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;
9+
10+
use Magento\EavGraphQl\Model\Resolver\DataProvider\AttributeOptions as AttributeOptionsDataProvider;
11+
use Magento\Framework\Exception\InputException;
12+
use Magento\Framework\Exception\StateException;
13+
use Magento\Framework\GraphQl\Config\Element\Field;
14+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
16+
use Magento\Framework\GraphQl\Query\Resolver\Value;
17+
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
18+
use Magento\Framework\GraphQl\Query\ResolverInterface;
19+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
20+
21+
/**
22+
* Resolve attribute options data for custom attribute.
23+
*/
24+
class AttributeOptions implements ResolverInterface
25+
{
26+
/**
27+
* @var AttributeOptionsDataProvider
28+
*/
29+
private $attributeOptionsDataProvider;
30+
31+
/**
32+
* @var AttributeOptions
33+
*/
34+
private $valueFactory;
35+
36+
/**
37+
* @param AttributeOptionsDataProvider $attributeOptionsDataProvider
38+
* @param ValueFactory $valueFactory
39+
*/
40+
public function __construct(
41+
AttributeOptionsDataProvider $attributeOptionsDataProvider,
42+
ValueFactory $valueFactory
43+
) {
44+
$this->attributeOptionsDataProvider = $attributeOptionsDataProvider;
45+
$this->valueFactory = $valueFactory;
46+
}
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
public function resolve(
52+
Field $field,
53+
$context,
54+
ResolveInfo $info,
55+
array $value = null,
56+
array $args = null
57+
) : Value {
58+
59+
return $this->valueFactory->create(function () use ($value) {
60+
$entityType = $this->getEntityType($value);
61+
$attributeCode = $this->getAttributeCode($value);
62+
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'));
77+
}
78+
79+
return (int)$value['entity_type'];
80+
}
81+
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'));
91+
}
92+
93+
return $value['attribute_code'];
94+
}
95+
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;
113+
}
114+
}
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+
}

app/code/Magento/EavGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "magento2-module",
55
"require": {
66
"php": "~7.1.3||~7.2.0",
7-
"magento/framework": "*"
7+
"magento/framework": "*",
8+
"magento/module-eav": "*"
89
},
910
"suggest": {
1011
"magento/module-graph-ql": "*"

app/code/Magento/EavGraphQl/etc/schema.graphqls

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ type Attribute @doc(description: "Attribute contains the attribute_type of the s
1313
attribute_code: String @doc(description: "The unique identifier for an attribute code. This value should be in lowercase letters without spaces.")
1414
entity_type: String @doc(description: "The type of entity that defines the attribute")
1515
attribute_type: String @doc(description: "The data type of the attribute")
16+
attribute_options: [AttributeOption] @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributeOptions") @doc(description: "Attribute options list.")
17+
}
18+
19+
type AttributeOption @doc(description: "Attribute option.") {
20+
label: String @doc(description: "Attribute option label.")
21+
value: String @doc(description: "Attribute option value.")
1622
}
1723

1824
input AttributeInput @doc(description: "AttributeInput specifies the attribute_code and entity_type to search") {

0 commit comments

Comments
 (0)