You are here

protected function EntityById::resolveValues in GraphQL 8.3

Retrieve the list of field values.

Always returns a list of field values. Even for single value fields. Single/multi field handling is responsibility of the base class.

Parameters

mixed $value: The current object value.

array $args: Field arguments.

$context: The resolve context.

\GraphQL\Type\Definition\ResolveInfo $info: The resolve info object.

Return value

\Generator The value generator.

Overrides FieldPluginBase::resolveValues

File

modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/EntityById.php, line 99

Class

EntityById
Plugin annotation @GraphQLField( id = "entity_by_id", secure = true, arguments = { "id" = "String!" }, contextual_arguments = {"language"}, deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityByIdDeriver" )

Namespace

Drupal\graphql_core\Plugin\GraphQL\Fields\Entity

Code

protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
  $resolver = $this->entityBuffer
    ->add($this
    ->getPluginDefinition()['entity_type'], $args['id']);
  return function ($value, array $args, ResolveContext $context, ResolveInfo $info) use ($resolver) {
    if (!($entity = $resolver())) {

      // If there is no entity with this id, add the list cache tags so that the
      // cache entry is purged whenever a new entity of this type is saved.
      $pluginDefinition = $this
        ->getPluginDefinition();
      $entityType = $this->entityTypeManager
        ->getDefinition($pluginDefinition['entity_type']);
      (yield (new CacheableValue(NULL))
        ->addCacheTags($entityType
        ->getListCacheTags()));
    }
    else {

      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $access = $entity
        ->access('view', NULL, TRUE);
      if ($access
        ->isAllowed()) {
        if (isset($args['language']) && $args['language'] != $entity
          ->language()
          ->getId() && $entity instanceof TranslatableInterface && $entity
          ->isTranslatable()) {
          if ($entity
            ->hasTranslation($args['language'])) {
            $entity = $entity
              ->getTranslation($args['language']);
          }
        }
        (yield $entity
          ->addCacheableDependency($access));
      }
      else {
        (yield new CacheableValue(NULL, [
          $access,
        ]));
      }
    }
  };
}