View source
<?php
namespace Drupal\thunder_gqls\Plugin\GraphQL\SchemaExtension;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\DataProducerPluginManager;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
use Drupal\user\EntityOwnerInterface;
use Drupal\thunder_gqls\Traits\ResolverHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ThunderSchemaExtensionPluginBase extends SdlSchemaExtensionPluginBase {
use ResolverHelperTrait;
protected $dataProducerManager;
protected $entityTypeManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin
->createResolverBuilder();
$plugin
->setDataProducerManager($container
->get('plugin.manager.graphql.data_producer'));
$plugin
->setEntityTypeManager($container
->get('entity_type.manager'));
return $plugin;
}
public function registerResolvers(ResolverRegistryInterface $registry) {
$this->registry = $registry;
}
protected function setDataProducerManager(DataProducerPluginManager $pluginManager) {
$this->dataProducerManager = $pluginManager;
}
protected function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
protected function mapBundleToSchemaName(string $bundleName) {
return str_replace('_', '', ucwords($bundleName, '_'));
}
protected function resolveBaseFields(string $type, string $entity_type_id) {
$this
->addFieldResolverIfNotExists($type, 'uuid', $this->builder
->produce('entity_uuid')
->map('entity', $this->builder
->fromParent()));
$this
->addFieldResolverIfNotExists($type, 'id', $this->builder
->produce('entity_id')
->map('entity', $this->builder
->fromParent()));
$this
->addFieldResolverIfNotExists($type, 'entity', $this->builder
->produce('entity_type_id')
->map('entity', $this->builder
->fromParent()));
$this
->addFieldResolverIfNotExists($type, 'name', $this->builder
->produce('entity_label')
->map('entity', $this->builder
->fromParent()));
$this
->addFieldResolverIfNotExists($type, 'language', $this->builder
->fromPath('entity', 'langcode.value'));
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
if ($entity_type
->hasLinkTemplate('canonical')) {
$this
->addFieldResolverIfNotExists($type, 'url', $this->builder
->compose($this->builder
->produce('entity_url')
->map('entity', $this->builder
->fromParent()), $this->builder
->produce('url_path')
->map('url', $this->builder
->fromParent())));
}
else {
$this
->addFieldResolverIfNotExists($type, 'url', $this->builder
->fromValue(NULL));
}
if (method_exists($entity_type
->getClass(), 'getCreatedTime')) {
$this
->addFieldResolverIfNotExists($type, 'created', $this->builder
->produce('entity_created')
->map('entity', $this->builder
->fromParent()));
}
if ($entity_type
->entityClassImplements(EntityChangedInterface::class)) {
$this
->addFieldResolverIfNotExists($type, 'changed', $this->builder
->produce('entity_changed')
->map('entity', $this->builder
->fromParent()));
}
if ($entity_type
->entityClassImplements(EntityPublishedInterface::class)) {
$this
->addFieldResolverIfNotExists($type, 'published', $this->builder
->produce('entity_published')
->map('entity', $this->builder
->fromParent()));
}
if ($entity_type
->entityClassImplements(EntityOwnerInterface::class)) {
$this
->addFieldResolverIfNotExists($type, 'author', $this->builder
->produce('entity_owner')
->map('entity', $this->builder
->fromParent()));
}
$this
->addFieldResolverIfNotExists($type, 'entityLinks', $this->builder
->produce('entity_links')
->map('entity', $this->builder
->fromParent()));
}
protected function resolveMediaInterfaceFields(string $type) {
$this
->resolveBaseFields($type, 'media');
$this
->addFieldResolverIfNotExists($type, 'thumbnail', $this->builder
->produce('thunder_image')
->map('entity', $this->builder
->fromPath('entity', 'thumbnail.entity'))
->map('field', $this->builder
->fromPath('entity', 'thumbnail')));
if ($this->dataProducerManager
->hasDefinition('media_expire_fallback_entity')) {
$this
->addFieldResolverIfNotExists($type, 'fallbackMedia', $this->builder
->produce('media_expire_fallback_entity')
->map('entity', $this->builder
->fromParent()));
}
}
protected function resolveParagraphInterfaceFields(string $type) {
$this
->addFieldResolverIfNotExists($type, 'summary', $this->builder
->produce('paragraph_summary')
->map('paragraph', $this->builder
->fromParent()));
}
protected function resolvePageInterfaceFields(string $type, string $entity_type_id) {
$this
->resolveBaseFields($type, $entity_type_id);
}
protected function resolvePageInterfaceQueryFields(string $page_type, string $entity_type_id) {
$this
->addFieldResolverIfNotExists('Query', $page_type, $this->builder
->produce('entity_load_by_uuid')
->map('type', $this->builder
->fromValue($entity_type_id))
->map('uuid', $this->builder
->fromArgument('uuid')));
}
}