diff --git a/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php b/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php index 58557b6aa5c02..1c25ffd1e9ff7 100644 --- a/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php +++ b/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/EntityUrl.php @@ -7,17 +7,16 @@ namespace Magento\UrlRewriteGraphQl\Model\Resolver; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\GraphQl\Config\Element\Field; -use Magento\Framework\GraphQl\Query\Resolver\Value; -use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Store\Model\StoreManagerInterface; use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface; /** - * The resolver returns the canonical URL for a specified product, category or CMS page + * UrlRewrite field resolver, used for GraphQL request processing. */ class EntityUrl implements ResolverInterface { @@ -31,11 +30,6 @@ class EntityUrl implements ResolverInterface */ private $storeManager; - /** - * @var ValueFactory - */ - private $valueFactory; - /** * @var CustomUrlLocatorInterface */ @@ -44,23 +38,20 @@ class EntityUrl implements ResolverInterface /** * @param UrlFinderInterface $urlFinder * @param StoreManagerInterface $storeManager - * @param ValueFactory $valueFactory * @param CustomUrlLocatorInterface $customUrlLocator */ public function __construct( UrlFinderInterface $urlFinder, StoreManagerInterface $storeManager, - ValueFactory $valueFactory, CustomUrlLocatorInterface $customUrlLocator ) { $this->urlFinder = $urlFinder; $this->storeManager = $storeManager; - $this->valueFactory = $valueFactory; $this->customUrlLocator = $customUrlLocator; } /** - * {@inheritdoc} + * @inheritdoc */ public function resolve( Field $field, @@ -68,31 +59,27 @@ public function resolve( ResolveInfo $info, array $value = null, array $args = null - ) : Value { - $result = function () { - return null; - }; + ) { + if (!isset($args['url']) || empty(trim($args['url']))) { + throw new GraphQlInputException(__('"url" argument should be specified and not empty')); + } - if (isset($args['url'])) { - $url = $args['url']; - if (substr($url, 0, 1) === '/' && $url !== '/') { - $url = ltrim($url, '/'); - } - $customUrl = $this->customUrlLocator->locateUrl($url); - $url = $customUrl ?: $url; - $urlRewrite = $this->findCanonicalUrl($url); - if ($urlRewrite) { - $urlRewriteReturnArray = [ - 'id' => $urlRewrite->getEntityId(), - 'canonical_url' => $urlRewrite->getTargetPath(), - 'type' => $this->sanitizeType($urlRewrite->getEntityType()) - ]; - $result = function () use ($urlRewriteReturnArray) { - return $urlRewriteReturnArray; - }; - } + $result = null; + $url = $args['url']; + if (substr($url, 0, 1) === '/' && $url !== '/') { + $url = ltrim($url, '/'); + } + $customUrl = $this->customUrlLocator->locateUrl($url); + $url = $customUrl ?: $url; + $urlRewrite = $this->findCanonicalUrl($url); + if ($urlRewrite) { + $result = [ + 'id' => $urlRewrite->getEntityId(), + 'canonical_url' => $urlRewrite->getTargetPath(), + 'type' => $this->sanitizeType($urlRewrite->getEntityType()) + ]; } - return $this->valueFactory->create($result); + return $result; } /** diff --git a/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php b/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php index fc502422288a8..0c4c78b582941 100644 --- a/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php +++ b/app/code/Magento/UrlRewriteGraphQl/Model/Resolver/UrlRewrite.php @@ -7,10 +7,9 @@ namespace Magento\UrlRewriteGraphQl\Model\Resolver; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\GraphQl\Config\Element\Field; -use Magento\Framework\GraphQl\Query\Resolver\Value; -use Magento\Framework\GraphQl\Query\Resolver\ValueFactory; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\Model\AbstractModel; use Magento\UrlRewrite\Model\UrlFinderInterface; @@ -27,24 +26,16 @@ class UrlRewrite implements ResolverInterface private $urlFinder; /** - * @var ValueFactory - */ - private $valueFactory; - - /** - * @param ValueFactory $valueFactory * @param UrlFinderInterface $urlFinder */ public function __construct( - ValueFactory $valueFactory, UrlFinderInterface $urlFinder ) { - $this->valueFactory = $valueFactory; $this->urlFinder = $urlFinder; } /** - * {@inheritdoc} + * @inheritdoc */ public function resolve( Field $field, @@ -52,23 +43,20 @@ public function resolve( ResolveInfo $info, array $value = null, array $args = null - ): Value { + ): array { if (!isset($value['model'])) { - $result = function () { - return null; - }; - return $this->valueFactory->create($result); + throw new GraphQlInputException(__('"model" value should be specified')); } /** @var AbstractModel $entity */ $entity = $value['model']; $entityId = $entity->getEntityId(); - $urlRewritesCollection = $this->urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $entityId]); + $urlRewriteCollection = $this->urlFinder->findAllByData([UrlRewriteDTO::ENTITY_ID => $entityId]); $urlRewrites = []; /** @var UrlRewriteDTO $urlRewrite */ - foreach ($urlRewritesCollection as $urlRewrite) { + foreach ($urlRewriteCollection as $urlRewrite) { if ($urlRewrite->getRedirectType() !== 0) { continue; } @@ -79,11 +67,7 @@ public function resolve( ]; } - $result = function () use ($urlRewrites) { - return $urlRewrites; - }; - - return $this->valueFactory->create($result); + return $urlRewrites; } /** diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/UrlRewritesTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/UrlRewritesTest.php index e8611498d2ee5..c39b32e4bfa4f 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/UrlRewritesTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/UrlRewritesTest.php @@ -146,28 +146,4 @@ private function getUrlParameters(string $targetPath): array return $urlParameters; } - - /** - * @param array $actualResponse - * @param array $assertionMap - */ - private function assertResponseFields($actualResponse, $assertionMap) - { - foreach ($assertionMap as $key => $assertionData) { - $expectedValue = isset($assertionData['expected_value']) - ? $assertionData['expected_value'] - : $assertionData; - $responseField = isset($assertionData['response_field']) ? $assertionData['response_field'] : $key; - self::assertNotNull( - $expectedValue, - "Value of '{$responseField}' field must not be NULL" - ); - self::assertEquals( - $expectedValue, - $actualResponse[$responseField], - "Value of '{$responseField}' field in response does not match expected value: " - . var_export($expectedValue, true) - ); - } - } }