diff --git a/app/code/Magento/EavGraphQl/Model/Output/Value/Options/GetCustomSelectedOptionAttributes.php b/app/code/Magento/EavGraphQl/Model/Output/Value/Options/GetCustomSelectedOptionAttributes.php index c84a7bdd5134d..75ae85f107cc3 100644 --- a/app/code/Magento/EavGraphQl/Model/Output/Value/Options/GetCustomSelectedOptionAttributes.php +++ b/app/code/Magento/EavGraphQl/Model/Output/Value/Options/GetCustomSelectedOptionAttributes.php @@ -1,24 +1,30 @@ attributeRepository->get($entity, $code); - $result = []; $selectedValues = explode(',', $value); - foreach ($attribute->getOptions() as $option) { - if (!in_array($option->getValue(), $selectedValues)) { - continue; + $options = $this->getAttributeOptions($entity, $code); + foreach ($selectedValues as $selectedValue) { + if (isset($options[$selectedValue])) { + $result[] = [ + 'value' => $selectedValue, + 'label' => $options[$selectedValue], + ]; } - $result[] = [ - 'value' => $option->getValue(), - 'label' => $option->getLabel() - ]; } + return $result; } + + /** + * Get cached attribute options + * + * @param string $entity + * @param string $code + * @return array + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function getAttributeOptions(string $entity, string $code): array + { + $attribute = $this->attributeRepository->get($entity, $code); + + if (!isset($this->optionsCache[$entity][$code])) { + $options = $attribute->getOptions(); + $optionsLabel = []; + foreach ($options as $option) { + $optionsLabel[$option->getValue()] = $option->getLabel(); + } + $this->optionsCache[$entity][$code] = $optionsLabel; + } + + return $this->optionsCache[$entity][$code]; + } + + /** + * @inheritDoc + */ + public function _resetState(): void + { + $this->optionsCache = []; + } } diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/EntityFieldChecker.php b/app/code/Magento/EavGraphQl/Model/Resolver/EntityFieldChecker.php index 0d852b401eac2..00d8889b84f02 100644 --- a/app/code/Magento/EavGraphQl/Model/Resolver/EntityFieldChecker.php +++ b/app/code/Magento/EavGraphQl/Model/Resolver/EntityFieldChecker.php @@ -1,7 +1,7 @@ resource = $resource; - $this->eavEntityType = $eavEntityType; + $this->eavConfig = $eavConfig; } /** @@ -42,13 +53,18 @@ public function __construct(ResourceConnection $resource, Type $eavEntityType) * @param string $entityTypeCode * @param string $field * @return bool + * @throws LocalizedException */ public function fieldBelongToEntity(string $entityTypeCode, string $field): bool { + if (isset($this->entityTables[$entityTypeCode])) { + $table = $this->entityTables[$entityTypeCode]; + } else { + $table = $this->eavConfig->getEntityType($entityTypeCode)->getAdditionalAttributeTable(); + $this->entityTables[$entityTypeCode] = $table; + } $connection = $this->resource->getConnection(); - $columns = $connection->describeTable( - $this->eavEntityType->loadByCode($entityTypeCode)->getAdditionalAttributeTable() - ); + $columns = $connection->describeTable($table); return array_key_exists($field, $columns); } diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/GetFilteredAttributes.php b/app/code/Magento/EavGraphQl/Model/Resolver/GetFilteredAttributes.php index 3b402a6e0dfb5..88485239b066c 100644 --- a/app/code/Magento/EavGraphQl/Model/Resolver/GetFilteredAttributes.php +++ b/app/code/Magento/EavGraphQl/Model/Resolver/GetFilteredAttributes.php @@ -1,7 +1,7 @@ getFilteredAttributesKey($filterArgs, $entityType); + if (isset($this->filteredAttributesCache[$key])) { + return $this->filteredAttributesCache[$key]; + } $errors = []; foreach ($filterArgs as $field => $value) { if ($this->entityFieldChecker->fieldBelongToEntity(strtolower($entityType), $field)) { @@ -79,9 +88,28 @@ public function execute(array $filterArgs, string $entityType): array $attributesList = $this->attributeRepository->getList(strtolower($entityType), $searchCriteria)->getItems(); - return [ + $this->filteredAttributesCache[$key] = [ 'items' => $attributesList, 'errors' => $errors ]; + + return $this->filteredAttributesCache[$key]; + } + + /** + * Get a key for filtered attributes cache + * + * @param array $filterArgs + * @param string $entityType + * @return string + */ + private function getFilteredAttributesKey(array $filterArgs, string $entityType): string + { + $key = $entityType; + foreach ($filterArgs as $field => $value) { + $key .= '_' . $field . '_' . $value; + } + + return sha1($key); } }