View source
<?php
namespace Drupal\thunder_gqls\Plugin\GraphQL\Schema;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Url;
use Drupal\graphql\GraphQL\ResolverRegistry;
use Drupal\graphql\Plugin\DataProducerPluginManager;
use Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema;
use Drupal\graphql\Plugin\GraphQL\Schema\SdlSchemaPluginBase;
use Drupal\thunder_gqls\Traits\ResolverHelperTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ThunderSchema extends ComposableSchema {
use ResolverHelperTrait;
const REQUIRED_EXTENSIONS = [
'thunder_pages',
'thunder_media',
'thunder_paragraphs',
];
protected $dataProducerManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$schema = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$schema
->setDataProducerManager($container
->get('plugin.manager.graphql.data_producer'));
return $schema;
}
protected function setDataProducerManager(DataProducerPluginManager $pluginManager) {
$this->dataProducerManager = $pluginManager;
}
public function getResolverRegistry() {
$this->registry = new ResolverRegistry();
$this
->createResolverBuilder();
$this
->resolveBaseTypes();
$this
->addFieldResolverIfNotExists('Query', 'redirect', $this->builder
->produce('thunder_redirect')
->map('path', $this->builder
->fromArgument('path')));
if ($this->dataProducerManager
->hasDefinition('access_unpublished_token_set')) {
$this
->addFieldResolverIfNotExists('Query', 'accessUnpublishedToken', $this->builder
->produce('access_unpublished_token_set')
->map('token', $this->builder
->fromArgument('auHash')));
}
return $this->registry;
}
protected function getExtensions() {
return array_map(function ($id) {
return $this->extensionManager
->createInstance($id);
}, array_unique(array_filter($this
->getConfiguration()['extensions']) + static::REQUIRED_EXTENSIONS));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
foreach (Element::children($form['extensions']) as $extension) {
if (in_array($extension, static::REQUIRED_EXTENSIONS)) {
$form['extensions'][$extension]['#access'] = FALSE;
unset($form['extensions']['#options'][$extension]);
}
}
return $form;
}
protected function getSchemaDefinition() {
return SdlSchemaPluginBase::getSchemaDefinition();
}
private function resolveBaseTypes() {
$this
->addFieldResolverIfNotExists('Link', 'url', $this->builder
->callback(function ($parent) {
if (!empty($parent) && isset($parent['uri'])) {
$urlObject = Url::fromUri($parent['uri']);
$url = $urlObject
->toString(TRUE)
->getGeneratedUrl();
}
return $url ?? '';
}));
$this
->addSimpleCallbackFields('Link', [
'title',
]);
$this
->addSimpleCallbackFields('FocalPoint', [
'x',
'y',
]);
$this
->addSimpleCallbackFields('Redirect', [
'url',
'status',
]);
$this
->addSimpleCallbackFields('EntityLinks', [
'canonical',
'deleteForm',
'deleteMultipleForm',
'editForm',
'versionHistory',
'revision',
'create',
'latestVersion',
]);
$this
->addSimpleCallbackFields('Thumbnail', [
'src',
'width',
'height',
'alt',
'title',
]);
$this
->addSimpleCallbackFields('ImageDerivative', [
'src',
'width',
'height',
]);
$this
->addSimpleCallbackFields('Schema', [
'query',
]);
}
}