You are here

public function EntityLoadMultiple::resolve in GraphQL 8.4

Resolver.

Parameters

string $type:

array $ids:

string|null $language:

array|null $bundles:

bool $access:

\Drupal\Core\Session\AccountInterface|null $accessUser:

string|null $accessOperation:

\Drupal\graphql\GraphQL\Execution\FieldContext $context:

Return value

\GraphQL\Deferred

File

src/Plugin/GraphQL/DataProducer/Entity/EntityLoadMultiple.php, line 146

Class

EntityLoadMultiple
Load multiple entities by IDs.

Namespace

Drupal\graphql\Plugin\GraphQL\DataProducer\Entity

Code

public function resolve($type, array $ids, ?string $language, ?array $bundles, bool $access, ?AccountInterface $accessUser, ?string $accessOperation, FieldContext $context) {
  $resolver = $this->entityBuffer
    ->add($type, $ids);
  return new Deferred(function () use ($type, $language, $bundles, $resolver, $context, $access, $accessUser, $accessOperation) {

    /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
    $entities = $resolver();
    if (!$entities) {

      // 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.
      $type = $this->entityTypeManager
        ->getDefinition($type);

      /** @var \Drupal\Core\Entity\EntityTypeInterface $type */
      $tags = $type
        ->getListCacheTags();
      $context
        ->addCacheTags($tags);
      return [];
    }
    foreach ($entities as $id => $entity) {
      $context
        ->addCacheableDependency($entities[$id]);
      if (isset($bundles) && !in_array($entities[$id]
        ->bundle(), $bundles)) {

        // If the entity is not among the allowed bundles, don't return it.
        unset($entities[$id]);
        continue;
      }
      if (isset($language) && $language !== $entities[$id]
        ->language()
        ->getId() && $entities[$id] instanceof TranslatableInterface) {
        $entities[$id] = $entities[$id]
          ->getTranslation($language);
        $entities[$id]
          ->addCacheContexts([
          "static:language:{$language}",
        ]);
      }
      if ($access) {

        /** @var \Drupal\Core\Access\AccessResultInterface $accessResult */
        $accessResult = $entities[$id]
          ->access($accessOperation, $accessUser, TRUE);
        $context
          ->addCacheableDependency($accessResult);

        // We need to call isAllowed() because isForbidden() returns FALSE
        // for neutral access results, which is dangerous. Only an explicitly
        // allowed result means that the user has access.
        if (!$accessResult
          ->isAllowed()) {
          unset($entities[$id]);
          continue;
        }
      }
    }
    return $entities;
  });
}