|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\DirectoryGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; |
| 12 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 13 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 14 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 15 | +use Magento\Framework\Reflection\DataObjectProcessor; |
| 16 | +use Magento\Directory\Api\CountryInformationAcquirerInterface; |
| 17 | +use Magento\Directory\Api\Data\CountryInformationInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Country field resolver, used for GraphQL request processing. |
| 21 | + */ |
| 22 | +class Country implements ResolverInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var DataObjectProcessor |
| 26 | + */ |
| 27 | + private $dataProcessor; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var CountryInformationAcquirerInterface |
| 31 | + */ |
| 32 | + private $countryInformationAcquirer; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param DataObjectProcessor $dataProcessor |
| 36 | + * @param CountryInformationAcquirerInterface $countryInformationAcquirer |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + DataObjectProcessor $dataProcessor, |
| 40 | + CountryInformationAcquirerInterface $countryInformationAcquirer |
| 41 | + ) { |
| 42 | + $this->dataProcessor = $dataProcessor; |
| 43 | + $this->countryInformationAcquirer = $countryInformationAcquirer; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritdoc |
| 48 | + */ |
| 49 | + public function resolve( |
| 50 | + Field $field, |
| 51 | + $context, |
| 52 | + ResolveInfo $info, |
| 53 | + array $value = null, |
| 54 | + array $args = null |
| 55 | + ) { |
| 56 | + try { |
| 57 | + $country = $this->countryInformationAcquirer->getCountryInfo($args['id']); |
| 58 | + } catch (NoSuchEntityException $exception) { |
| 59 | + throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception); |
| 60 | + } |
| 61 | + |
| 62 | + return $this->dataProcessor->buildOutputDataArray( |
| 63 | + $country, |
| 64 | + CountryInformationInterface::class |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
0 commit comments