You are here

class EntityUuidBuffer in GraphQL 8.4

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

Hierarchy

Expanded class hierarchy of EntityUuidBuffer

1 file declares its use of EntityUuidBuffer
EntityLoadByUuid.php in src/Plugin/GraphQL/DataProducer/Entity/EntityLoadByUuid.php
1 string reference to 'EntityUuidBuffer'
graphql.services.yml in ./graphql.services.yml
graphql.services.yml
1 service uses EntityUuidBuffer
graphql.buffer.entity_uuid in ./graphql.services.yml
Drupal\graphql\GraphQL\Buffers\EntityUuidBuffer

File

src/GraphQL/Buffers/EntityUuidBuffer.php, line 12

Namespace

Drupal\graphql\GraphQL\Buffers
View source
class EntityUuidBuffer 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|string $uuid
   *   The entity uuid(s) to load.
   *
   * @return \Closure
   *   The callback to invoke to load the result for this buffer item.
   */
  public function add($type, $uuid) {
    $item = new \ArrayObject([
      'type' => $type,
      'uuid' => $uuid,
    ]);
    return $this
      ->createBufferResolver($item);
  }

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

  /**
   * {@inheritdoc}
   */
  public function resolveBufferArray(array $buffer) {
    $type = reset($buffer)['type'];
    $entityType = $this->entityTypeManager
      ->getDefinition($type);
    if (!($uuid_key = $entityType
      ->getKey('uuid'))) {
      throw new EntityStorageException("Entity type {$type} does not support UUIDs.");
    }
    $uuids = array_map(function (\ArrayObject $item) {
      return (array) $item['uuid'];
    }, $buffer);
    $uuids = call_user_func_array('array_merge', $uuids);
    $uuids = array_values(array_unique($uuids));
    $entities = $this->entityTypeManager
      ->getStorage($type)
      ->loadByProperties([
      $uuid_key => $uuids,
    ]);
    $entities = array_reduce(array_map(function (EntityInterface $entity) {
      return [
        $entity
          ->uuid() => $entity,
      ];
    }, $entities), 'array_merge', []);
    return array_map(function ($item) use ($entities) {
      if (is_array($item['uuid'])) {
        return array_reduce($item['uuid'], function ($carry, $current) use ($entities) {
          if (!empty($entities[$current])) {
            return $carry + [
              $current => $entities[$current],
            ];
          }
          return $carry;
        }, []);
      }
      return isset($entities[$item['uuid']]) ? $entities[$item['uuid']] : 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.
EntityUuidBuffer::$entityTypeManager protected property The entity type manager service.
EntityUuidBuffer::add public function Add an item to the buffer.
EntityUuidBuffer::getBufferId protected function Returns the bucket name for grouping items together. Overrides BufferBase::getBufferId
EntityUuidBuffer::resolveBufferArray public function Resolve the buffer as an array. Overrides BufferBase::resolveBufferArray
EntityUuidBuffer::__construct public function EntityBuffer constructor.