You are here

class EntityBuffer in GraphQL 8.4

Same name and namespace in other branches
  1. 8.3 src/GraphQL/Buffers/EntityBuffer.php \Drupal\graphql\GraphQL\Buffers\EntityBuffer

Collects entity IDs per entity type and loads them all at once in the end.

Hierarchy

Expanded class hierarchy of EntityBuffer

5 files declare their use of EntityBuffer
EntityLoad.php in src/Plugin/GraphQL/DataProducer/Entity/EntityLoad.php
EntityLoadMultiple.php in src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php
EntityReference.php in src/Plugin/GraphQL/DataProducer/Field/EntityReference.php
RouteEntity.php in src/Plugin/GraphQL/DataProducer/Routing/RouteEntity.php
TaxonomyLoadTree.php in src/Plugin/GraphQL/DataProducer/Taxonomy/TaxonomyLoadTree.php
1 string reference to 'EntityBuffer'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses EntityBuffer
graphql.buffer.entity in ./graphql.services.yml
Drupal\graphql\GraphQL\Buffers\EntityBuffer

File

src/GraphQL/Buffers/EntityBuffer.php, line 10

Namespace

Drupal\graphql\GraphQL\Buffers
View source
class EntityBuffer extends BufferBase {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * EntityBuffer constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Add an item to the buffer.
   *
   * @param string $type
   *   The entity type of the given entity ids.
   * @param array|int|string $id
   *   The entity id(s) to load.
   *
   * @return \Closure
   *   The callback to invoke to load the result for this buffer item.
   */
  public function add($type, $id) {
    $item = new \ArrayObject([
      'type' => $type,
      'id' => $id,
    ]);
    return $this
      ->createBufferResolver($item);
  }

  /**
   * {@inheritdoc}
   */
  protected function getBufferId($item) {
    return $item['type'];
  }

  /**
   * {@inheritdoc}
   */
  public function resolveBufferArray(array $buffer) {
    $type = reset($buffer)['type'];
    $ids = array_map(function (\ArrayObject $item) {
      return (array) $item['id'];
    }, $buffer);
    $ids = call_user_func_array('array_merge', $ids);
    $ids = array_values(array_unique($ids));

    // Load the buffered entities.
    $entities = $this->entityTypeManager
      ->getStorage($type)
      ->loadMultiple($ids);
    return array_map(function ($item) use ($entities) {
      if (is_array($item['id'])) {
        return array_reduce($item['id'], function ($carry, $current) use ($entities) {
          if (!empty($entities[$current])) {
            array_push($carry, $entities[$current]);
            return $carry;
          }
          return $carry;
        }, []);
      }
      return isset($entities[$item['id']]) ? $entities[$item['id']] : NULL;
    }, $buffer);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BufferBase::$buffers protected property The the array of buffers.
BufferBase::$results protected property The array of result sets.
BufferBase::createBufferResolver public function Helper function to create a resolver for a singular buffer.
BufferBase::createResolver protected function Creates a callback to invoke to load the result for this buffer item.
BufferBase::resolveBuffer protected function Resolves the given buffer wholly.
BufferBase::resolveItem protected function Returns the result of the given item after processing the buffer if needed.
EntityBuffer::$entityTypeManager protected property The entity type manager service.
EntityBuffer::add public function Add an item to the buffer.
EntityBuffer::getBufferId protected function Returns the bucket name for grouping items together. Overrides BufferBase::getBufferId
EntityBuffer::resolveBufferArray public function Resolve the buffer as an array. Overrides BufferBase::resolveBufferArray
EntityBuffer::__construct public function EntityBuffer constructor.