-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-2840: 87 Fetch attribute values and labels for customAttribute…
…Metadata #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
- Loading branch information
Showing
4 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
app/code/Magento/EavGraphQl/Model/Resolver/AttributeOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\EavGraphQl\Model\Resolver; | ||
|
||
use Magento\EavGraphQl\Model\Resolver\DataProvider\AttributeOptions as AttributeOptionsDataProvider; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\Exception\StateException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Framework\GraphQl\Query\Resolver\Value; | ||
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
|
||
/** | ||
* Resolve attribute options data for custom attribute. | ||
*/ | ||
class AttributeOptions implements ResolverInterface | ||
{ | ||
/** | ||
* @var AttributeOptionsDataProvider | ||
*/ | ||
private $attributeOptionsDataProvider; | ||
|
||
/** | ||
* @var AttributeOptions | ||
*/ | ||
private $valueFactory; | ||
|
||
/** | ||
* @param AttributeOptionsDataProvider $attributeOptionsDataProvider | ||
* @param ValueFactory $valueFactory | ||
*/ | ||
public function __construct( | ||
AttributeOptionsDataProvider $attributeOptionsDataProvider, | ||
ValueFactory $valueFactory | ||
) { | ||
$this->attributeOptionsDataProvider = $attributeOptionsDataProvider; | ||
$this->valueFactory = $valueFactory; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function resolve( | ||
Field $field, | ||
$context, | ||
ResolveInfo $info, | ||
array $value = null, | ||
array $args = null | ||
) : Value { | ||
|
||
return $this->valueFactory->create(function () use ($value) { | ||
$entityType = $this->getEntityType($value); | ||
$attributeCode = $this->getAttributeCode($value); | ||
|
||
$optionsData = $this->getAttributeOptionsData($entityType, $attributeCode); | ||
return $optionsData; | ||
}); | ||
} | ||
|
||
/** | ||
* @param array $value | ||
* @return int | ||
* @throws GraphQlInputException | ||
*/ | ||
private function getEntityType(array $value): int | ||
{ | ||
if (!isset($value['entity_type'])) { | ||
throw new GraphQlInputException(__('"Entity type should be specified')); | ||
} | ||
|
||
return (int)$value['entity_type']; | ||
} | ||
|
||
/** | ||
* @param array $value | ||
* @return string | ||
* @throws GraphQlInputException | ||
*/ | ||
private function getAttributeCode(array $value): string | ||
{ | ||
if (!isset($value['attribute_code'])) { | ||
throw new GraphQlInputException(__('"Attribute code should be specified')); | ||
} | ||
|
||
return $value['attribute_code']; | ||
} | ||
|
||
/** | ||
* @param int $entityType | ||
* @param string $attributeCode | ||
* @return array | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
private function getAttributeOptionsData(int $entityType, string $attributeCode): array | ||
{ | ||
try { | ||
$optionsData = $this->attributeOptionsDataProvider->getData($entityType, $attributeCode); | ||
} catch (InputException $e) { | ||
throw new GraphQlInputException(__($e->getMessage()), $e); | ||
} catch (StateException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} | ||
return $optionsData; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
app/code/Magento/EavGraphQl/Model/Resolver/DataProvider/AttributeOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\EavGraphQl\Model\Resolver\DataProvider; | ||
|
||
use Magento\Eav\Api\AttributeOptionManagementInterface; | ||
|
||
/** | ||
* Attribute Options data provider | ||
*/ | ||
class AttributeOptions | ||
{ | ||
/** | ||
* @var AttributeOptionManagementInterface | ||
*/ | ||
private $optionManager; | ||
|
||
/** | ||
* @param AttributeOptionManagementInterface $optionManager | ||
*/ | ||
public function __construct( | ||
AttributeOptionManagementInterface $optionManager | ||
) { | ||
$this->optionManager = $optionManager; | ||
} | ||
|
||
/** | ||
* @param int $entityType | ||
* @param string $attributeCode | ||
* @return array | ||
*/ | ||
public function getData(int $entityType, string $attributeCode): array | ||
{ | ||
$options = $this->optionManager->getItems($entityType, $attributeCode); | ||
|
||
$optionsData = []; | ||
foreach ($options as $option) { | ||
// without empty option @see \Magento\Eav\Model\Entity\Attribute\Source\Table::getAllOptions | ||
if ($option->getValue() === '') { | ||
continue; | ||
} | ||
|
||
$optionsData[] = [ | ||
'label' => $option->getLabel(), | ||
'value' => $option->getValue() | ||
]; | ||
} | ||
return $optionsData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters