EnumPluginBase.php in GraphQL 8.3
File
src/Plugin/GraphQL/Enums/EnumPluginBase.php
View source
<?php
namespace Drupal\graphql\Plugin\GraphQL\Enums;
use Drupal\Component\Plugin\PluginBase;
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
use Drupal\graphql\Plugin\SchemaBuilderInterface;
use Drupal\graphql\Plugin\TypePluginInterface;
use Drupal\graphql\Plugin\TypePluginManager;
use GraphQL\Type\Definition\EnumType;
abstract class EnumPluginBase extends PluginBase implements TypePluginInterface {
use CacheablePluginTrait;
use DescribablePluginTrait;
public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
return new EnumType([
'name' => $definition['name'],
'description' => $definition['description'],
'values' => $definition['values'],
'contexts' => $definition['contexts'],
]);
}
public function getDefinition() {
$definition = $this
->getPluginDefinition();
return [
'name' => $definition['name'],
'description' => $this
->buildDescription($definition),
'values' => $this
->buildEnumValues($definition),
'contexts' => $this
->buildCacheContexts($definition),
];
}
protected function buildEnumValues($definition) {
return array_map(function ($value) use ($definition) {
return [
'value' => $this
->buildEnumValue($value, $definition),
'description' => $this
->buildEnumDescription($value, $definition),
];
}, $definition['values']);
}
protected function buildEnumValue($value, $definition) {
return is_array($value) ? $value['value'] : $value;
}
protected function buildEnumDescription($value, $definition) {
return (string) (is_array($value) ? $value['description'] : '');
}
}