Skip to content

Commit eb5ca28

Browse files
LYNX-158: Replace uid input with combinations of entity_type, attribute_code in attributesMetadata query (#103)
* LYNX-158: Replace uid input with combinations of entity_type, attribute_code in attributesMetadata query
1 parent a1de60d commit eb5ca28

File tree

14 files changed

+77
-80
lines changed

14 files changed

+77
-80
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/GetAttributesForm.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Customer\Api\MetadataInterface;
1111
use Magento\EavGraphQl\Model\GetAttributesFormInterface;
12-
use Magento\EavGraphQl\Model\Uid;
1312

1413
/**
1514
* Attributes form provider for customer
@@ -21,25 +20,18 @@ class GetAttributesForm implements GetAttributesFormInterface
2120
*/
2221
private MetadataInterface $entity;
2322

24-
/**
25-
* @var Uid
26-
*/
27-
private Uid $uid;
28-
2923
/**
3024
* @var string
3125
*/
3226
private string $type;
3327

3428
/**
3529
* @param MetadataInterface $metadata
36-
* @param Uid $uid
3730
* @param string $type
3831
*/
39-
public function __construct(MetadataInterface $metadata, Uid $uid, string $type)
32+
public function __construct(MetadataInterface $metadata, string $type)
4033
{
4134
$this->entity = $metadata;
42-
$this->uid = $uid;
4335
$this->type = $type;
4436
}
4537

@@ -50,7 +42,7 @@ public function execute(string $formCode): ?array
5042
{
5143
$attributes = [];
5244
foreach ($this->entity->getAttributes($formCode) as $attribute) {
53-
$attributes[] = $this->uid->encode($this->type, $attribute->getAttributeCode());
45+
$attributes[] = ['entity_type' => $this->type, 'attribute_code' => $attribute->getAttributeCode()];
5446
}
5547
return $attributes;
5648
}

app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,12 @@
1313
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
1414
use Magento\Framework\Exception\LocalizedException;
1515
use Magento\Framework\Exception\RuntimeException;
16-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1716

1817
/**
1918
* Retrieve EAV attributes details
2019
*/
2120
class GetAttributesMetadata
2221
{
23-
/**
24-
* @var Uid
25-
*/
26-
private Uid $uid;
27-
2822
/**
2923
* @var AttributeRepositoryInterface
3024
*/
@@ -43,48 +37,37 @@ class GetAttributesMetadata
4337
/**
4438
* @param AttributeRepositoryInterface $attributeRepository
4539
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
46-
* @param Uid $uid
4740
* @param GetAttributeDataInterface $getAttributeData
4841
*/
4942
public function __construct(
5043
AttributeRepositoryInterface $attributeRepository,
5144
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
52-
Uid $uid,
5345
GetAttributeDataInterface $getAttributeData
5446
) {
5547
$this->attributeRepository = $attributeRepository;
5648
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
57-
$this->uid = $uid;
5849
$this->getAttributeData = $getAttributeData;
5950
}
6051

6152
/**
6253
* Get attribute metadata details
6354
*
64-
* @param string[] $uids
55+
* @param array $attributesInputs
6556
* @param int $storeId
6657
* @return array
6758
* @throws RuntimeException
6859
*/
69-
public function execute(array $uids, int $storeId): array
60+
public function execute(array $attributesInputs, int $storeId): array
7061
{
71-
if (empty($uids)) {
62+
if (empty($attributesInputs)) {
7263
return [];
7364
}
7465

7566
$codes = [];
7667
$errors = [];
7768

78-
foreach ($uids as $uid) {
79-
try {
80-
list($entityType, $attributeCode) = $this->uid->decode($uid);
81-
$codes[$entityType][] = $attributeCode;
82-
} catch (GraphQlInputException $exception) {
83-
$errors[] = [
84-
'type' => 'INCORRECT_UID',
85-
'message' => $exception->getMessage()
86-
];
87-
}
69+
foreach ($attributesInputs as $attributeInput) {
70+
$codes[$attributeInput['entity_type']][] = $attributeInput['attribute_code'];
8871
}
8972

9073
$items = [];

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1515

1616
/**
17-
* Load EAV attributes by UIDs
17+
* Load EAV attributes by attribute_code and entity_type
1818
*/
1919
class AttributesMetadata implements ResolverInterface
2020
{
@@ -42,12 +42,27 @@ public function resolve(
4242
array $value = null,
4343
array $args = null
4444
) {
45-
if (empty($args['input']['uids']) || !is_array($args['input']['uids'])) {
46-
throw new GraphQlInputException(__('Required parameter "%1" of type array.', 'uids'));
45+
$attributeInputs = $args['attributes'];
46+
47+
if (empty($attributeInputs)) {
48+
throw new GraphQlInputException(
49+
__(
50+
'Required parameters "attribute_code" and "entity_type" of type String.'
51+
)
52+
);
53+
}
54+
55+
foreach ($attributeInputs as $attributeInput) {
56+
if (!isset($attributeInput['attribute_code'])) {
57+
throw new GraphQlInputException(__('The attribute_code is required to retrieve the metadata'));
58+
}
59+
if (!isset($attributeInput['entity_type'])) {
60+
throw new GraphQlInputException(__('The entity_type is required to retrieve the metadata'));
61+
}
4762
}
4863

4964
return $this->getAttributesMetadata->execute(
50-
$args['input']['uids'],
65+
$attributeInputs,
5166
(int) $context->getExtensionAttributes()->getStore()->getId()
5267
);
5368
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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(cacheIdentity: "Magento\\EavGraphQl\\Model\\Resolver\\Cache\\CustomAttributeMetadataIdentity")
6-
attributesMetadata(input: AttributesMetadataInput!): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.")
6+
attributesMetadata(attributes: [AttributeInput!]): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.")
77
attributesForm(type: String! @doc(description: "Form type")): AttributesFormOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesForm") @doc(description: "Retrieve EAV attributes associated to a frontend form.")
88
attributesList(entityType: 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)
99
}
@@ -45,10 +45,6 @@ input AttributeInput @doc(description: "Defines the attribute characteristics to
4545
entity_type: String @doc(description: "The type of entity that defines the attribute.")
4646
}
4747

48-
input AttributesMetadataInput @doc(description: "attributesMetadata query input.") {
49-
uids: [ID!]! @doc(description: "UIDs of attributes to query.")
50-
}
51-
5248
type AttributesMetadataOutput @doc(description: "Metadata of EAV attributes.") {
5349
items: [AttributeMetadataInterface!]! @doc(description: "Requested attributes metadata.")
5450
errors: [AttributeMetadataError!]! @doc(description: "Errors of retrieving certain attributes metadata.")
@@ -60,7 +56,6 @@ type AttributeMetadataError @doc(description: "Attribute metadata retrieval erro
6056
}
6157

6258
enum AttributeMetadataErrorType @doc(description: "Attribute metadata retrieval error types.") {
63-
INCORRECT_UID @doc(description: "The UID of the attribute is corrupted.")
6459
ENTITY_NOT_FOUND @doc(description: "The requested entity was not found.")
6560
ATTRIBUTE_NOT_FOUND @doc(description: "The requested attribute was not found.")
6661
UNDEFINED @doc(description: "Not categorized error, see the error message.")

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/BooleanTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BooleanTest extends GraphQlAbstract
2323
{
2424
private const QUERY = <<<QRY
2525
{
26-
attributesMetadata(input: {uids: ["%s"]}) {
26+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
2727
items {
2828
uid
2929
code
@@ -68,7 +68,7 @@ public function testMetadata(): void
6868
$attribute->getAttributeCode()
6969
);
7070

71-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
71+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
7272

7373
$this->assertEquals(
7474
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/DateTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DateTest extends GraphQlAbstract
2323
{
2424
private const QUERY = <<<QRY
2525
{
26-
attributesMetadata(input: {uids: ["%s"]}) {
26+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
2727
items {
2828
uid
2929
code
@@ -63,7 +63,7 @@ public function testMetadata(): void
6363
$attribute->getAttributeCode()
6464
);
6565

66-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
66+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
6767

6868
$this->assertEquals(
6969
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/FileTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FileTest extends GraphQlAbstract
2323
{
2424
private const QUERY = <<<QRY
2525
{
26-
attributesMetadata(input: {uids: ["%s"]}) {
26+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
2727
items {
2828
uid
2929
code
@@ -62,7 +62,7 @@ public function testMetadata(): void
6262
$attribute->getAttributeCode()
6363
);
6464

65-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
65+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
6666

6767
$this->assertEquals(
6868
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/ImageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ImageTest extends GraphQlAbstract
2323
{
2424
private const QUERY = <<<QRY
2525
{
26-
attributesMetadata(input: {uids: ["%s"]}) {
26+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
2727
items {
2828
uid
2929
code
@@ -62,7 +62,7 @@ public function testMetadata(): void
6262
$attribute->getAttributeCode()
6363
);
6464

65-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
65+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
6666

6767
$this->assertEquals(
6868
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultilineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class MultilineTest extends GraphQlAbstract
2323
{
2424
private const QUERY = <<<QRY
2525
{
26-
attributesMetadata(input: {uids: ["%s"]}) {
26+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
2727
items {
2828
uid
2929
code
@@ -69,7 +69,7 @@ public function testMetadata(): void
6969
$attribute->getAttributeCode()
7070
);
7171

72-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
72+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
7373

7474
$this->assertEquals(
7575
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultiselectTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MultiselectTest extends GraphQlAbstract
2626
{
2727
private const QUERY = <<<QRY
2828
{
29-
attributesMetadata(input: {uids: ["%s"]}) {
29+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
3030
items {
3131
uid
3232
default_value
@@ -101,7 +101,7 @@ public function testMetadata(): void
101101
$attribute->getAttributeCode()
102102
);
103103

104-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
104+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
105105

106106
$this->assertEquals(
107107
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/SelectTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SelectTest extends GraphQlAbstract
2727
{
2828
private const QUERY = <<<QRY
2929
{
30-
attributesMetadata(input: {uids: ["%s"]}) {
30+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
3131
items {
3232
uid
3333
options {
@@ -86,7 +86,7 @@ public function testMetadata(): void
8686
$attribute->getAttributeCode()
8787
);
8888

89-
$result = $this->graphQlQuery(sprintf(self::QUERY, $uid));
89+
$result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer'));
9090

9191
$this->assertEquals(
9292
[

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/StoreViewOptionsTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class StoreViewOptionsTest extends GraphQlAbstract
9999
{
100100
private const QUERY = <<<QRY
101101
{
102-
attributesMetadata(input: {uids: ["%s"]}) {
102+
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
103103
items {
104104
uid
105105
code
@@ -167,7 +167,8 @@ public function testAttributeLabelsNoStoreViews(): void
167167
$this->graphQlQuery(
168168
sprintf(
169169
self::QUERY,
170-
$uid
170+
$attribute->getAttributeCode(),
171+
'customer'
171172
)
172173
)
173174
);
@@ -219,7 +220,8 @@ public function testAttributeLabelsMultipleStoreViews(): void
219220
$this->graphQlQuery(
220221
sprintf(
221222
self::QUERY,
222-
$uid
223+
$attribute->getAttributeCode(),
224+
'customer'
223225
),
224226
[],
225227
'',
@@ -256,7 +258,8 @@ public function testAttributeLabelsMultipleStoreViews(): void
256258
$this->graphQlQuery(
257259
sprintf(
258260
self::QUERY,
259-
$uid
261+
$attribute->getAttributeCode(),
262+
'customer'
260263
),
261264
[],
262265
'',

0 commit comments

Comments
 (0)