Skip to content

Commit

Permalink
LYNX-158: Replace uid input with combinations of entity_type, attribu…
Browse files Browse the repository at this point in the history
…te_code in attributesMetadata query (#103)

* LYNX-158: Replace uid input with combinations of entity_type, attribute_code in attributesMetadata query
  • Loading branch information
eliseacornejo authored Apr 21, 2023
1 parent a1de60d commit eb5ca28
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Magento\Customer\Api\MetadataInterface;
use Magento\EavGraphQl\Model\GetAttributesFormInterface;
use Magento\EavGraphQl\Model\Uid;

/**
* Attributes form provider for customer
Expand All @@ -21,25 +20,18 @@ class GetAttributesForm implements GetAttributesFormInterface
*/
private MetadataInterface $entity;

/**
* @var Uid
*/
private Uid $uid;

/**
* @var string
*/
private string $type;

/**
* @param MetadataInterface $metadata
* @param Uid $uid
* @param string $type
*/
public function __construct(MetadataInterface $metadata, Uid $uid, string $type)
public function __construct(MetadataInterface $metadata, string $type)
{
$this->entity = $metadata;
$this->uid = $uid;
$this->type = $type;
}

Expand All @@ -50,7 +42,7 @@ public function execute(string $formCode): ?array
{
$attributes = [];
foreach ($this->entity->getAttributes($formCode) as $attribute) {
$attributes[] = $this->uid->encode($this->type, $attribute->getAttributeCode());
$attributes[] = ['entity_type' => $this->type, 'attribute_code' => $attribute->getAttributeCode()];
}
return $attributes;
}
Expand Down
27 changes: 5 additions & 22 deletions app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
use Magento\Framework\Api\SearchCriteriaBuilderFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;

/**
* Retrieve EAV attributes details
*/
class GetAttributesMetadata
{
/**
* @var Uid
*/
private Uid $uid;

/**
* @var AttributeRepositoryInterface
*/
Expand All @@ -43,48 +37,37 @@ class GetAttributesMetadata
/**
* @param AttributeRepositoryInterface $attributeRepository
* @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory
* @param Uid $uid
* @param GetAttributeDataInterface $getAttributeData
*/
public function __construct(
AttributeRepositoryInterface $attributeRepository,
SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory,
Uid $uid,
GetAttributeDataInterface $getAttributeData
) {
$this->attributeRepository = $attributeRepository;
$this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory;
$this->uid = $uid;
$this->getAttributeData = $getAttributeData;
}

/**
* Get attribute metadata details
*
* @param string[] $uids
* @param array $attributesInputs
* @param int $storeId
* @return array
* @throws RuntimeException
*/
public function execute(array $uids, int $storeId): array
public function execute(array $attributesInputs, int $storeId): array
{
if (empty($uids)) {
if (empty($attributesInputs)) {
return [];
}

$codes = [];
$errors = [];

foreach ($uids as $uid) {
try {
list($entityType, $attributeCode) = $this->uid->decode($uid);
$codes[$entityType][] = $attributeCode;
} catch (GraphQlInputException $exception) {
$errors[] = [
'type' => 'INCORRECT_UID',
'message' => $exception->getMessage()
];
}
foreach ($attributesInputs as $attributeInput) {
$codes[$attributeInput['entity_type']][] = $attributeInput['attribute_code'];
}

$items = [];
Expand Down
23 changes: 19 additions & 4 deletions app/code/Magento/EavGraphQl/Model/Resolver/AttributesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Load EAV attributes by UIDs
* Load EAV attributes by attribute_code and entity_type
*/
class AttributesMetadata implements ResolverInterface
{
Expand Down Expand Up @@ -42,12 +42,27 @@ public function resolve(
array $value = null,
array $args = null
) {
if (empty($args['input']['uids']) || !is_array($args['input']['uids'])) {
throw new GraphQlInputException(__('Required parameter "%1" of type array.', 'uids'));
$attributeInputs = $args['attributes'];

if (empty($attributeInputs)) {
throw new GraphQlInputException(
__(
'Required parameters "attribute_code" and "entity_type" of type String.'
)
);
}

foreach ($attributeInputs as $attributeInput) {
if (!isset($attributeInput['attribute_code'])) {
throw new GraphQlInputException(__('The attribute_code is required to retrieve the metadata'));
}
if (!isset($attributeInput['entity_type'])) {
throw new GraphQlInputException(__('The entity_type is required to retrieve the metadata'));
}
}

return $this->getAttributesMetadata->execute(
$args['input']['uids'],
$attributeInputs,
(int) $context->getExtensionAttributes()->getStore()->getId()
);
}
Expand Down
7 changes: 1 addition & 6 deletions app/code/Magento/EavGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

type Query {
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")
attributesMetadata(input: AttributesMetadataInput!): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.")
attributesMetadata(attributes: [AttributeInput!]): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.")
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.")
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)
}
Expand Down Expand Up @@ -45,10 +45,6 @@ input AttributeInput @doc(description: "Defines the attribute characteristics to
entity_type: String @doc(description: "The type of entity that defines the attribute.")
}

input AttributesMetadataInput @doc(description: "attributesMetadata query input.") {
uids: [ID!]! @doc(description: "UIDs of attributes to query.")
}

type AttributesMetadataOutput @doc(description: "Metadata of EAV attributes.") {
items: [AttributeMetadataInterface!]! @doc(description: "Requested attributes metadata.")
errors: [AttributeMetadataError!]! @doc(description: "Errors of retrieving certain attributes metadata.")
Expand All @@ -60,7 +56,6 @@ type AttributeMetadataError @doc(description: "Attribute metadata retrieval erro
}

enum AttributeMetadataErrorType @doc(description: "Attribute metadata retrieval error types.") {
INCORRECT_UID @doc(description: "The UID of the attribute is corrupted.")
ENTITY_NOT_FOUND @doc(description: "The requested entity was not found.")
ATTRIBUTE_NOT_FOUND @doc(description: "The requested attribute was not found.")
UNDEFINED @doc(description: "Not categorized error, see the error message.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BooleanTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
code
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DateTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
code
Expand Down Expand Up @@ -63,7 +63,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FileTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
code
Expand Down Expand Up @@ -62,7 +62,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ImageTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
code
Expand Down Expand Up @@ -62,7 +62,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MultilineTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
code
Expand Down Expand Up @@ -69,7 +69,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MultiselectTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
default_value
Expand Down Expand Up @@ -101,7 +101,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SelectTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
options {
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testMetadata(): void
$attribute->getAttributeCode()
);

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

$this->assertEquals(
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class StoreViewOptionsTest extends GraphQlAbstract
{
private const QUERY = <<<QRY
{
attributesMetadata(input: {uids: ["%s"]}) {
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) {
items {
uid
code
Expand Down Expand Up @@ -167,7 +167,8 @@ public function testAttributeLabelsNoStoreViews(): void
$this->graphQlQuery(
sprintf(
self::QUERY,
$uid
$attribute->getAttributeCode(),
'customer'
)
)
);
Expand Down Expand Up @@ -219,7 +220,8 @@ public function testAttributeLabelsMultipleStoreViews(): void
$this->graphQlQuery(
sprintf(
self::QUERY,
$uid
$attribute->getAttributeCode(),
'customer'
),
[],
'',
Expand Down Expand Up @@ -256,7 +258,8 @@ public function testAttributeLabelsMultipleStoreViews(): void
$this->graphQlQuery(
sprintf(
self::QUERY,
$uid
$attribute->getAttributeCode(),
'customer'
),
[],
'',
Expand Down
Loading

0 comments on commit eb5ca28

Please sign in to comment.