InputTypePluginBase.php in GraphQL 8.3
File
src/Plugin/GraphQL/InputTypes/InputTypePluginBase.php
View source
<?php
namespace Drupal\graphql\Plugin\GraphQL\InputTypes;
use Drupal\Component\Plugin\PluginBase;
use Drupal\graphql\Plugin\GraphQL\Traits\CacheablePluginTrait;
use Drupal\graphql\Plugin\GraphQL\Traits\DescribablePluginTrait;
use Drupal\graphql\Plugin\GraphQL\Traits\TypedPluginTrait;
use Drupal\graphql\Plugin\SchemaBuilderInterface;
use Drupal\graphql\Plugin\TypePluginInterface;
use Drupal\graphql\Plugin\TypePluginManager;
use Drupal\graphql\Utility\StringHelper;
use GraphQL\Type\Definition\InputObjectType;
abstract class InputTypePluginBase extends PluginBase implements TypePluginInterface {
use CacheablePluginTrait;
use DescribablePluginTrait;
use TypedPluginTrait;
public static function createInstance(SchemaBuilderInterface $builder, TypePluginManager $manager, $definition, $id) {
return new InputObjectType([
'name' => $definition['name'],
'description' => $definition['description'],
'contexts' => $definition['contexts'],
'fields' => function () use ($builder, $definition) {
return $builder
->processArguments($definition['fields']);
},
]);
}
public function getDefinition() {
$definition = $this
->getPluginDefinition();
return [
'name' => $definition['name'],
'description' => $this
->buildDescription($definition),
'fields' => $this
->buildFields($definition),
'contexts' => $this
->buildCacheContexts($definition),
];
}
protected function buildFields($definition) {
return array_map(function ($field) use ($definition) {
return [
'type' => $this
->buildFieldType($field, $definition),
'description' => $this
->buildFieldDescription($field, $definition),
'default' => $this
->buildFieldDefault($field, $definition),
];
}, $definition['fields']);
}
protected function buildFieldType($field) {
$type = is_array($field) ? $field['type'] : $field;
return StringHelper::parseType($type);
}
protected function buildFieldDescription($field, $definition) {
return (string) (isset($field['description']) ? $field['description'] : '');
}
protected function buildFieldDefault($field, $definition) {
return isset($field['default']) ? $field['default'] : NULL;
}
}