You are here

public function ImageUrl::resolve in GraphQL 8.4

Resolver.

Parameters

\Drupal\file\FileInterface $entity:

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata:

Return value

string|null

File

src/Plugin/GraphQL/DataProducer/Entity/Fields/Image/ImageUrl.php, line 85

Class

ImageUrl
Returns the file URL of a file entity.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\Fields\Image

Code

public function resolve(FileInterface $entity, RefinableCacheableDependencyInterface $metadata) {
  $access = $entity
    ->access('view', NULL, TRUE);
  $metadata
    ->addCacheableDependency($access);
  if ($access
    ->isAllowed()) {

    // The underlying URL generator that will be invoked will leak cache
    // metadata, resulting in an exception. By wrapping within a new render
    // context, we can capture the leaked metadata and make sure it gets
    // incorporated into the response.
    $context = new RenderContext();
    $url = $this->renderer
      ->executeInRenderContext($context, function () use ($entity) {
      return file_create_url($entity
        ->getFileUri());
    });
    if (!$context
      ->isEmpty()) {
      $metadata
        ->addCacheableDependency($context
        ->pop());
    }
    return $url;
  }
  return NULL;
}