You are here

public function EntityBuffer::resolveBufferArray in GraphQL 8.3

Same name and namespace in other branches
  1. 8.4 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 results, keyed by their corresponding buffer item array key.

Overrides BufferBase::resolveBufferArray

File

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

Class

EntityBuffer

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])) {
          return $carry + [
            $current => $entities[$current],
          ];
        }
        return $carry;
      }, []);
    }
    return isset($entities[$item['id']]) ? $entities[$item['id']] : NULL;
  }, $buffer);
}