You are here

trait ResolverHelperTrait in Thunder 6.2.x

Helper functions for field resolvers.

Hierarchy

2 files declare their use of ResolverHelperTrait
ThunderSchema.php in modules/thunder_gqls/src/Plugin/GraphQL/Schema/ThunderSchema.php
ThunderSchemaExtensionPluginBase.php in modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSchemaExtensionPluginBase.php

File

modules/thunder_gqls/src/Traits/ResolverHelperTrait.php, line 11

Namespace

Drupal\thunder_gqls\Traits
View source
trait ResolverHelperTrait {

  /**
   * ResolverBuilder.
   *
   * @var \Drupal\graphql\GraphQL\ResolverBuilder
   */
  protected $builder;

  /**
   * ResolverRegistryInterface.
   *
   * @var \Drupal\graphql\GraphQL\ResolverRegistryInterface
   */
  protected $registry;

  /**
   * Add field resolver to registry, if it does not already exist.
   *
   * @param string $type
   *   The type name.
   * @param string $field
   *   The field name.
   * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $resolver
   *   The field resolver.
   */
  protected function addFieldResolverIfNotExists(string $type, string $field, ResolverInterface $resolver) {
    if (!$this->registry
      ->getFieldResolver($type, $field)) {
      $this->registry
        ->addFieldResolver($type, $field, $resolver);
    }
  }

  /**
   * Create the ResolverBuilder.
   */
  protected function createResolverBuilder() {
    $this->builder = new ResolverBuilder();
  }

  /**
   * Produces an entity_reference field.
   *
   * @param string $field
   *   Name of the filed.
   * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface|null $entity
   *   Entity to get the field property.
   *
   * @return \Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerProxy
   *   The field data producer.
   */
  public function fromEntityReference(string $field, ResolverInterface $entity = NULL) {
    return $this->builder
      ->produce('entity_reference')
      ->map('field', $this->builder
      ->fromValue($field))
      ->map('entity', $entity ?: $this->builder
      ->fromParent());
  }

  /**
   * Produces an entity_reference_revisions field.
   *
   * @param string $field
   *   Name of the filed.
   * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface|null $entity
   *   Entity to get the field property.
   *
   * @return \Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerProxy
   *   The field data producer.
   */
  public function fromEntityReferenceRevisions(string $field, $entity = NULL) {
    return $this->builder
      ->produce('entity_reference_revisions')
      ->map('field', $this->builder
      ->fromValue($field))
      ->map('entity', $entity ?: $this->builder
      ->fromParent())
      ->map('language', $this->builder
      ->fromPath('entity', 'langcode.value', $this->builder
      ->fromParent()));
  }

  /**
   * Define callback field resolver for a type.
   *
   * @param string $type
   *   Type to add fields.
   * @param array $fields
   *   The fields.
   */
  public function addSimpleCallbackFields(string $type, array $fields) {
    foreach ($fields as $field) {
      $this
        ->addFieldResolverIfNotExists($type, $field, $this->builder
        ->callback(function ($arr) use ($field) {
        return $arr[$field];
      }));
    }
  }

  /**
   * Produces an entity from a given path.
   *
   * @param \Drupal\graphql\GraphQL\Resolver\ResolverInterface $path
   *   The path resolver.
   *
   * @return \Drupal\graphql\GraphQL\Resolver\ResolverInterface
   *   The resolved entity.
   */
  public function fromRoute(ResolverInterface $path) {
    return $this->builder
      ->compose($this->builder
      ->produce('route_load')
      ->map('path', $path), $this->builder
      ->produce('route_entity')
      ->map('url', $this->builder
      ->fromParent())
      ->map('language', $this->builder
      ->produce('thunder_entity_sub_request')
      ->map('path', $path)
      ->map('key', $this->builder
      ->fromValue('language'))));
  }

}

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.