You are here

public function EntityUuidBuffer::resolveBufferArray in GraphQL 8.4

Resolve the buffer as an array.

Simplifies sub-class implementations by concealing the object storage details of the buffer object.

Parameters

array $buffer: The buffer as an array.

Return value

array The resolved/loaded items.

Overrides BufferBase::resolveBufferArray

File

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

Class

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

Namespace

Drupal\graphql\GraphQL\Buffers

Code

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);
}