Skip to content

Commit b33fb64

Browse files
committed
LYNX-100: CR changes; bugfixing; refactoring
1 parent 910f286 commit b33fb64

File tree

4 files changed

+60
-44
lines changed

4 files changed

+60
-44
lines changed

app/code/Magento/EavGraphQl/Model/Output/GetAttributeData.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function execute(
6262
): array {
6363
return [
6464
'uid' => $this->attributeUid->encode($entityType, $attribute->getAttributeCode()),
65-
'code' => $attribute->getAttributeCode(),
65+
'attribute_code' => $attribute->getAttributeCode(),
6666
'label' => $attribute->getStoreLabel($storeId),
6767
'sort_order' => $attribute->getPosition(),
6868
'entity_type' => $this->enumLookup->getEnumValueFromField(
@@ -71,7 +71,7 @@ public function execute(
7171
),
7272
'frontend_input' => $this->enumLookup->getEnumValueFromField(
7373
'AttributeFrontendInputEnum',
74-
$attribute->getFrontendInput()
74+
$attribute->getFrontendInput() ?? ""
7575
),
7676
'is_required' => $attribute->getIsRequired(),
7777
'default_value' => $attribute->getDefaultValue(),
@@ -89,6 +89,8 @@ public function execute(
8989
*/
9090
private function getOptions(AttributeInterface $attribute): array
9191
{
92+
return [];
93+
9294
if (!$attribute->getOptions()) {
9395
return [];
9496
}
@@ -102,7 +104,7 @@ function (AttributeOptionInterface $option) use ($attribute) {
102104
'uid' => $this->uid->encode($option->getValue()),
103105
'label' => $option->getLabel(),
104106
'value' => $option->getValue(),
105-
'is_default' => in_array($option->getValue(), explode(',', $attribute->getDefaultValue()))
107+
'is_default' => $attribute->getDefaultValue() ? in_array($option->getValue(), explode(',', $attribute->getDefaultValue())) : null
106108
];
107109
},
108110
$attribute->getOptions()

app/code/Magento/EavGraphQl/Model/Resolver/EntityTypeAttributesList.php renamed to app/code/Magento/EavGraphQl/Model/Resolver/AttributesList.php

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,35 @@
1414
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17-
use Magento\Framework\Phrase;
17+
use Magento\EavGraphQl\Model\Output\GetAttributeData;
18+
use Magento\Framework\GraphQl\Query\Uid;
19+
use Magento\EavGraphQl\Model\Uid as AttributeUid;
1820

1921
/**
2022
* Resolve attribute options data for custom attribute.
2123
*/
22-
class EntityTypeAttributesList implements ResolverInterface
24+
class AttributesList implements ResolverInterface
2325
{
2426
/**
2527
* @var AttributeRepository
2628
*/
2729
private AttributeRepository $attributeRepository;
2830

31+
/**
32+
* @var Uid
33+
*/
34+
private Uid $uid;
35+
36+
/**
37+
* @var AttributeUid
38+
*/
39+
private AttributeUid $attributeUid;
40+
41+
/**
42+
* @var GetAttributeData
43+
*/
44+
private GetAttributeData $getAttributeData;
45+
2946
/**
3047
* @var SearchCriteriaBuilder
3148
*/
@@ -45,18 +62,25 @@ class EntityTypeAttributesList implements ResolverInterface
4562
* @param AttributeRepository $attributeRepository
4663
* @param SearchCriteriaBuilder $searchCriteriaBuilder
4764
* @param EnumLookup $enumLookup
65+
* @param Uid $uid
66+
* @param AttributeUid $attributeUid
4867
* @param array $resolvers
4968
*/
5069
public function __construct(
5170
AttributeRepository $attributeRepository,
5271
SearchCriteriaBuilder $searchCriteriaBuilder,
5372
EnumLookup $enumLookup,
73+
Uid $uid,
74+
AttributeUid $attributeUid,
5475
array $resolvers = []
5576
) {
5677
$this->attributeRepository = $attributeRepository;
5778
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
5879
$this->enumLookup = $enumLookup;
80+
$this->uid = $uid;
81+
$this->attributeUid = $attributeUid;
5982
$this->resolvers = $resolvers;
83+
$this->getAttributeData = new GetAttributeData($this->attributeUid, $this->uid, $this->enumLookup);
6084
}
6185

6286
/**
@@ -68,29 +92,27 @@ public function resolve(
6892
ResolveInfo $info,
6993
array $value = null,
7094
array $args = null
71-
): mixed {
72-
$errors = [];
73-
95+
): array {
7496
if (!$args['entity_type']) {
7597
throw new GraphQlInputException(__('Required parameter "%1" of type string.', 'entity_type'));
7698
}
7799

100+
$errors = [];
101+
$storeId = (int) $context->getExtensionAttributes()->getStore()->getId();
78102
$entityType = $this->enumLookup->getEnumValueFromField(
79103
'AttributeEntityTypeEnum',
80104
mb_strtolower($args['entity_type'])
81105
);
82106

83107
$searchCriteria = $this->searchCriteriaBuilder;
84-
85108
foreach ($this->resolvers as $resolver) {
86109
$searchCriteria->addFilter($resolver['name'], $resolver['object']->execute());
87110
}
88111
$searchCriteria = $searchCriteria->create();
89112

90113
$attributesList = $this->attributeRepository->getList(mb_strtolower($entityType), $searchCriteria)->getItems();
91-
92114
return [
93-
'items' => $this->getAtrributesMetadata($attributesList),
115+
'items' => $this->getAtrributesMetadata($attributesList, $entityType, $storeId),
94116
'errors' => $errors
95117
];
96118
}
@@ -99,22 +121,15 @@ public function resolve(
99121
* Returns formatted list of attributes
100122
*
101123
* @param array $attributesList
124+
* @param string $entityType
125+
* @param int $storeId
126+
*
102127
* @return array
103128
*/
104-
private function getAtrributesMetadata($attributesList)
129+
private function getAtrributesMetadata(array $attributesList, string $entityType, int $storeId)
105130
{
106-
return array_map(function ($attribute) {
107-
return [
108-
'uid' => $attribute->getAttributeId(),
109-
'attribute_code' => $attribute->getData('attribute_code'),
110-
'frontend_label' => $attribute->getData('frontend_label'),
111-
'entity_type_id' => $attribute->getData('entity_type_id'),
112-
'frontend_input' => $attribute->getData('frontend_input'),
113-
'is_required' => $attribute->getData('is_required'),
114-
'default_value' => $attribute->getData('default_value'),
115-
'is_unique' => $attribute->getIsUnique(),
116-
'options' => $attribute->getData('options')
117-
];
131+
return array_map(function ($attribute) use ($entityType, $storeId) {
132+
return $this->getAttributeData->execute($attribute, mb_strtolower($entityType), $storeId);
118133
}, $attributesList);
119134
}
120135
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
type Query {
55
customAttributeMetadata(attributes: [AttributeInput!]! @doc(description: "An input object that specifies the attribute code and entity type to search.")): CustomAttributeMetadata @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\CustomAttributeMetadata") @doc(description: "Return the attribute type, given an attribute code and entity type.") @cache(cacheable: false)
66
attributesMetadata(input: AttributesMetadataInput!): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.")
7-
entityTypeAttributesList(entity_type: AttributeEntityTypeEnum! @doc(description: "Entity type.")): AttributesMetadataOutput @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\EntityTypeAttributesList") @doc(description: "Returns list of atributes metadata for given entity type.") @cache(cacheable: false)
7+
attributesList(entity_type: AttributeEntityTypeEnum! @doc(description: "Entity type.")): AttributesMetadataOutput @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesList") @doc(description: "Returns list of atributes metadata for given entity type.") @cache(cacheable: false)
88
}
99

1010
type CustomAttributeMetadata @doc(description: "Defines an array of custom attributes.") {
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Test EAV attributes metadata retrieval for entity type via GraphQL API
1919
*/
20-
class EntityTypeAttributesListTest extends GraphQlAbstract
20+
class AttributesListTest extends GraphQlAbstract
2121
{
2222
private const ATTRIBUTE_NOT_FOUND_ERROR = "Attribute was not found in query result";
2323

@@ -61,8 +61,7 @@ class EntityTypeAttributesListTest extends GraphQlAbstract
6161
'attribute_code' => 'attribute_4'
6262
],
6363
'attribute4'
64-
)
65-
,
64+
),
6665
DataFixture(
6766
Attribute::class,
6867
[
@@ -72,11 +71,11 @@ class EntityTypeAttributesListTest extends GraphQlAbstract
7271
'attribute5'
7372
)
7473
]
75-
public function testEntityTypeAttributesList(): void
74+
public function testAttributesList(): void
7675
{
7776
$queryResult = $this->graphQlQuery(<<<QRY
7877
{
79-
entityTypeAttributesList(entity_type: CUSTOMER) {
78+
attributesList(entity_type: CUSTOMER) {
8079
items {
8180
uid
8281
attribute_code
@@ -89,33 +88,33 @@ public function testEntityTypeAttributesList(): void
8988
}
9089
QRY);
9190

92-
$this->assertArrayHasKey('items', $queryResult['entityTypeAttributesList'], 'Query result does not contain items');
93-
$this->assertGreaterThanOrEqual(3, count($queryResult['entityTypeAttributesList']['items']));
91+
$this->assertArrayHasKey('items', $queryResult['attributesList'], 'Query result does not contain items');
92+
$this->assertGreaterThanOrEqual(3, count($queryResult['attributesList']['items']));
9493

9594
$this->assertEquals(
9695
'attribute_0',
97-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_0')['attribute_code'],
96+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_0')['attribute_code'],
9897
self::ATTRIBUTE_NOT_FOUND_ERROR
9998
);
100-
99+
101100
$this->assertEquals(
102101
'attribute_1',
103-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_1')['attribute_code'],
102+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_1')['attribute_code'],
104103
self::ATTRIBUTE_NOT_FOUND_ERROR
105104
);
106105
$this->assertEquals(
107106
'attribute_2',
108-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_2')['attribute_code'],
107+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_2')['attribute_code'],
109108
self::ATTRIBUTE_NOT_FOUND_ERROR
110109
);
111110
$this->assertEquals(
112111
[],
113-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_5')
112+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_5')
114113
);
115114

116115
$queryResult = $this->graphQlQuery(<<<QRY
117116
{
118-
entityTypeAttributesList(entity_type: CATALOG_PRODUCT) {
117+
attributesList(entity_type: CATALOG_PRODUCT) {
119118
items {
120119
uid
121120
attribute_code
@@ -127,22 +126,22 @@ public function testEntityTypeAttributesList(): void
127126
}
128127
}
129128
QRY);
130-
$this->assertArrayHasKey('items', $queryResult['entityTypeAttributesList'], 'Query result does not contain items');
131-
$this->assertGreaterThanOrEqual(2, count($queryResult['entityTypeAttributesList']['items']));
129+
$this->assertArrayHasKey('items', $queryResult['attributesList'], 'Query result does not contain items');
130+
$this->assertGreaterThanOrEqual(2, count($queryResult['attributesList']['items']));
132131

133132
$this->assertEquals(
134133
'attribute_3',
135-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_3')['attribute_code'],
134+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_3')['attribute_code'],
136135
self::ATTRIBUTE_NOT_FOUND_ERROR
137136
);
138137
$this->assertEquals(
139138
'attribute_4',
140-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_4')['attribute_code'],
139+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_4')['attribute_code'],
141140
self::ATTRIBUTE_NOT_FOUND_ERROR
142141
);
143142
$this->assertEquals(
144143
[],
145-
$this->getAttributeByCode($queryResult['entityTypeAttributesList']['items'], 'attribute_5')
144+
$this->getAttributeByCode($queryResult['attributesList']['items'], 'attribute_5')
146145
);
147146
}
148147

@@ -153,7 +152,7 @@ public function testEntityTypeAttributesList(): void
153152
* @param string $attribute_code
154153
* @return array
155154
*/
156-
private function getAttributeByCode($items, $attribute_code)
155+
private function getAttributeByCode(array $items, string $attribute_code): array
157156
{
158157
$attribute = array_filter($items, function ($item) use ($attribute_code) {
159158
return $item['attribute_code'] == $attribute_code;

0 commit comments

Comments
 (0)