QueryArticles.php in GraphQL 8.4
File
examples/graphql_example/src/Plugin/GraphQL/DataProducer/QueryArticles.php
View source
<?php
namespace Drupal\graphql_examples\Plugin\GraphQL\DataProducer;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\graphql_examples\Wrappers\QueryConnection;
use GraphQL\Error\UserError;
use Symfony\Component\DependencyInjection\ContainerInterface;
class QueryArticles extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
const MAX_LIMIT = 100;
protected $entityTypeManager;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->entityTypeManager = $entityTypeManager;
}
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);
}
}
Classes
Name |
Description |
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")
),
… |