You are here

public function SubRequestBuffer::resolveBufferArray in GraphQL 8.3

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/SubRequestBuffer.php, line 117

Class

SubRequestBuffer

Namespace

Drupal\graphql\GraphQL\Buffers

Code

public function resolveBufferArray(array $buffer) {

  /** @var \Drupal\Core\GeneratedUrl $url */
  $url = reset($buffer)['url']
    ->toString(TRUE);
  $current = $this->requestStack
    ->getCurrentRequest();
  $target = $url
    ->getGeneratedUrl();
  $request = $this
    ->createRequest($current, $buffer, $target);

  /** @var \Drupal\graphql\GraphQL\Buffers\SubRequestResponse $response */
  $response = $this->httpKernel
    ->handle($request, HttpKernelInterface::SUB_REQUEST);
  while ($response instanceof LocalRedirectResponse) {
    $target = $response
      ->getTargetUrl();
    $request = $this
      ->createRequest($current, $buffer, $target);
    $response = $this->httpKernel
      ->handle($request, HttpKernelInterface::SUB_REQUEST);
  }
  if (!$response instanceof SubRequestResponse) {
    return array_fill_keys(array_keys($buffer), NULL);
  }

  // TODO:
  // Remove the request stack manipulation once the core issue described at
  // https://www.drupal.org/node/2613044 is resolved.
  while ($this->requestStack
    ->getCurrentRequest() !== $current) {
    $this->requestStack
      ->pop();
  }
  if ($url instanceof CacheableDependencyInterface) {
    $response
      ->addCacheableDependency($url);
  }
  return array_map(function ($value) use ($response) {
    return new CacheableValue($value, [
      $response,
    ]);
  }, $response
    ->getResult());
}