You are here

public function EntityRendered::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/EntityRendered.php, line 85

Class

EntityRendered
Plugin annotation @GraphQLField( id = "entity_rendered", name = "entityRendered", type = "String", secure = true, deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityRenderedDeriver", )

Namespace

Drupal\graphql_core\Plugin\GraphQL\Fields\Entity

Code

public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
  if ($value instanceof ContentEntityInterface) {
    $mode = isset($args['mode']) ? $args['mode'] : 'full';
    $language = $value
      ->language()
      ->getId();
    $builder = $this->entityTypeManager
      ->getViewBuilder($value
      ->getEntityTypeId());
    $view = $builder
      ->view($value, $mode, $language);
    $context = new RenderContext();

    /** @var \GraphQL\Executor\ExecutionResult|\GraphQL\Executor\ExecutionResult[] $result */
    $result = $this->renderer
      ->executeInRenderContext($context, function () use ($view) {
      return $this->renderer
        ->render($view);
    });
    if (!$context
      ->isEmpty()) {
      (yield new CacheableValue((string) $result, [
        $context
          ->pop(),
      ]));
    }
    else {
      (yield (string) $result);
    }
  }
}