You are here

public function EntityBuffer::resolveBufferArray in GraphQL 8.4

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

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/EntityBuffer.php, line 59

Class

EntityBuffer
Collects entity IDs 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'];
  $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);
}