graphql_compose-1.0.0-beta20/src/Plugin/GraphQLCompose/FieldType/AddressCountryItem.php
src/Plugin/GraphQLCompose/FieldType/AddressCountryItem.php
<?php
declare(strict_types=1);
namespace Drupal\graphql_compose\Plugin\GraphQLCompose\FieldType;
use Drupal\graphql_compose\Attribute\FieldType;
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql_compose\Plugin\GraphQL\DataProducer\FieldProducerItemInterface;
use Drupal\graphql_compose\Plugin\GraphQL\DataProducer\FieldProducerTrait;
use Drupal\graphql_compose\Plugin\GraphQLCompose\GraphQLComposeFieldTypeBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* {@inheritdoc}
*/
#[FieldType(
id: "address_country",
type_sdl: "AddressCountry",
)]
class AddressCountryItem extends GraphQLComposeFieldTypeBase implements FieldProducerItemInterface, ContainerFactoryPluginInterface {
use FieldProducerTrait;
/**
* Address country repository.
*
* @var \CommerceGuys\Addressing\Country\CountryRepositoryInterface
*/
protected CountryRepositoryInterface $countryRepository;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create(
$container,
$configuration,
$plugin_id,
$plugin_definition
);
$instance->countryRepository = $container->get('address.country_repository');
return $instance;
}
/**
* {@inheritdoc}
*/
public function resolveFieldItem(FieldItemInterface $item, FieldContext $context) {
$countries = $this->countryRepository->getList();
return [
'name' => $countries[$item->value] ?? $item->value ?: NULL,
'code' => $item->value ?: NULL,
];
}
}
