class EntityUuidBuffer in GraphQL 8.4
Collects entity UUIDs per entity type and loads them all at once in the end.
Hierarchy
- class \Drupal\graphql\GraphQL\Buffers\BufferBase
- class \Drupal\graphql\GraphQL\Buffers\EntityUuidBuffer
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'
1 service uses EntityUuidBuffer
File
- src/
GraphQL/ Buffers/ EntityUuidBuffer.php, line 12
Namespace
Drupal\graphql\GraphQL\BuffersView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BufferBase:: |
protected | property | The the array of buffers. | |
BufferBase:: |
protected | property | The array of result sets. | |
BufferBase:: |
public | function | Helper function to create a resolver for a singular buffer. | |
BufferBase:: |
protected | function | Creates a callback to invoke to load the result for this buffer item. | |
BufferBase:: |
protected | function | Resolves the given buffer wholly. | |
BufferBase:: |
protected | function | Returns the result of the given item after processing the buffer if needed. | |
EntityUuidBuffer:: |
protected | property | The entity type manager service. | |
EntityUuidBuffer:: |
public | function | Add an item to the buffer. | |
EntityUuidBuffer:: |
protected | function |
Returns the bucket name for grouping items together. Overrides BufferBase:: |
|
EntityUuidBuffer:: |
public | function |
Resolve the buffer as an array. Overrides BufferBase:: |
|
EntityUuidBuffer:: |
public | function | EntityBuffer constructor. |