You are here

class ThunderSchema in Thunder 6.2.x

Tha base schema for Thunder composable schema.

Plugin annotation


@Schema(
  id = "thunder",
  name = "Thunder composable schema"
)

Hierarchy

Expanded class hierarchy of ThunderSchema

File

modules/thunder_gqls/src/Plugin/GraphQL/Schema/ThunderSchema.php, line 23

Namespace

Drupal\thunder_gqls\Plugin\GraphQL\Schema
View source
class ThunderSchema extends ComposableSchema {
  use ResolverHelperTrait;
  const REQUIRED_EXTENSIONS = [
    'thunder_pages',
    'thunder_media',
    'thunder_paragraphs',
  ];

  /**
   * The data producer plugin manager.
   *
   * @var \Drupal\graphql\Plugin\DataProducerPluginManager
   */
  protected $dataProducerManager;

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Set the plugin manager.
   *
   * @param \Drupal\graphql\Plugin\DataProducerPluginManager $pluginManager
   *   The data producer plugin manager.
   */
  protected function setDataProducerManager(DataProducerPluginManager $pluginManager) {
    $this->dataProducerManager = $pluginManager;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  protected function getExtensions() {
    return array_map(function ($id) {
      return $this->extensionManager
        ->createInstance($id);
    }, array_unique(array_filter($this
      ->getConfiguration()['extensions']) + static::REQUIRED_EXTENSIONS));
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  protected function getSchemaDefinition() {
    return SdlSchemaPluginBase::getSchemaDefinition();
  }

  /**
   * Resolve custom types, that are used in multiple places.
   */
  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',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResolverHelperTrait::$builder protected property ResolverBuilder.
ResolverHelperTrait::$registry protected property ResolverRegistryInterface.
ResolverHelperTrait::addFieldResolverIfNotExists protected function Add field resolver to registry, if it does not already exist.
ResolverHelperTrait::addSimpleCallbackFields public function Define callback field resolver for a type.
ResolverHelperTrait::createResolverBuilder protected function Create the ResolverBuilder.
ResolverHelperTrait::fromEntityReference public function Produces an entity_reference field.
ResolverHelperTrait::fromEntityReferenceRevisions public function Produces an entity_reference_revisions field.
ResolverHelperTrait::fromRoute public function Produces an entity from a given path.
ThunderSchema::$dataProducerManager protected property The data producer plugin manager.
ThunderSchema::buildConfigurationForm public function
ThunderSchema::create public static function
ThunderSchema::getExtensions protected function
ThunderSchema::getResolverRegistry public function
ThunderSchema::getSchemaDefinition protected function
ThunderSchema::REQUIRED_EXTENSIONS constant
ThunderSchema::resolveBaseTypes private function Resolve custom types, that are used in multiple places.
ThunderSchema::setDataProducerManager protected function Set the plugin manager.