1+ <?php
2+
3+ namespace Drupal \graphql_examples \Plugin \GraphQL \DataProducer ;
4+
5+ use Drupal \Core \Cache \RefinableCacheableDependencyInterface ;
6+ use Drupal \Core \Entity \EntityTypeManagerInterface ;
7+ use Drupal \Core \Plugin \ContainerFactoryPluginInterface ;
8+ use Drupal \graphql \Plugin \GraphQL \DataProducer \DataProducerPluginBase ;
9+ use Drupal \graphql_examples \Wrappers \QueryConnection ;
10+ use GraphQL \Error \UserError ;
11+ use Symfony \Component \DependencyInjection \ContainerInterface ;
12+
13+ /**
14+ * @DataProducer(
15+ * id = "query_articles",
16+ * name = @Translation("Load articles"),
17+ * description = @Translation("Loads a list of articles."),
18+ * produces = @ContextDefinition("any",
19+ * label = @Translation("Article connection")
20+ * ),
21+ * consumes = {
22+ * "offset" = @ContextDefinition("integer",
23+ * label = @Translation("Offset"),
24+ * required = FALSE
25+ * ),
26+ * "limit" = @ContextDefinition("integer",
27+ * label = @Translation("Limit"),
28+ * required = FALSE
29+ * )
30+ * }
31+ * )
32+ */
33+ class QueryArticles extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
34+
35+ const MAX_LIMIT = 100 ;
36+
37+ /**
38+ * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39+ */
40+ protected $ entityManager ;
41+
42+ /**
43+ * {@inheritdoc}
44+ *
45+ * @codeCoverageIgnore
46+ */
47+ public static function create (ContainerInterface $ container , array $ configuration , $ plugin_id , $ plugin_definition ) {
48+ return new static (
49+ $ configuration ,
50+ $ plugin_id ,
51+ $ plugin_definition ,
52+ $ container ->get ('entity.manager ' )
53+ );
54+ }
55+
56+ /**
57+ * Articles constructor.
58+ *
59+ * @param array $configuration
60+ * The plugin configuration.
61+ * @param string $pluginId
62+ * The plugin id.
63+ * @param mixed $pluginDefinition
64+ * The plugin definition.
65+ *
66+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityManager
67+ *
68+ * @codeCoverageIgnore
69+ */
70+ public function __construct (
71+ array $ configuration ,
72+ $ pluginId ,
73+ $ pluginDefinition ,
74+ EntityTypeManagerInterface $ entityManager
75+ ) {
76+ parent ::__construct ($ configuration , $ pluginId , $ pluginDefinition );
77+ $ this ->entityManager = $ entityManager ;
78+ }
79+
80+ /**
81+ * @param $offset
82+ * @param $limit
83+ * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
84+ *
85+ * @return \Drupal\graphql_examples\Wrappers\QueryConnection
86+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
87+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
88+ */
89+ public function resolve ($ offset , $ limit , RefinableCacheableDependencyInterface $ metadata ) {
90+ if (!$ limit > static ::MAX_LIMIT ) {
91+ throw new UserError (sprintf ('Exceeded maximum query limit: %s. ' , static ::MAX_LIMIT ));
92+ }
93+
94+ $ storage = $ this ->entityManager ->getStorage ('node ' );
95+ $ type = $ storage ->getEntityType ();
96+ $ query = $ storage ->getQuery ()
97+ ->currentRevision ()
98+ ->accessCheck ();
99+
100+ $ query ->condition ($ type ->getKey ('bundle ' ), 'article ' );
101+ $ query ->range ($ offset , $ limit );
102+
103+ $ metadata ->addCacheTags ($ type ->getListCacheTags ());
104+ $ metadata ->addCacheContexts ($ type ->getListCacheContexts ());
105+
106+ return new QueryConnection ($ query );
107+ }
108+ }
0 commit comments