You are here

public function QueryArticles::resolve in GraphQL 8.4

Parameters

int $offset:

int $limit:

\Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata:

Return value

\Drupal\graphql_examples\Wrappers\QueryConnection

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php, line 88

Class

QueryArticles
Plugin annotation @DataProducer( id = "query_articles", name = @Translation("Load articles"), description = @Translation("Loads a list of articles."), produces = @ContextDefinition("any", label = @Translation("Article connection") ), …

Namespace

Drupal\graphql_examples\Plugin\GraphQL\DataProducer

Code

public function resolve($offset, $limit, RefinableCacheableDependencyInterface $metadata) {
  if ($limit > static::MAX_LIMIT) {
    throw new UserError(sprintf('Exceeded maximum query limit: %s.', static::MAX_LIMIT));
  }
  $storage = $this->entityTypeManager
    ->getStorage('node');
  $entityType = $storage
    ->getEntityType();
  $query = $storage
    ->getQuery()
    ->currentRevision()
    ->accessCheck();
  $query
    ->condition($entityType
    ->getKey('bundle'), 'article');
  $query
    ->range($offset, $limit);
  $metadata
    ->addCacheTags($entityType
    ->getListCacheTags());
  $metadata
    ->addCacheContexts($entityType
    ->getListCacheContexts());
  return new QueryConnection($query);
}