Completed
Push — 8.x-3.x ( 039426...053d3a )
by Sebastian
05:38 queued 02:12
created

EntityQuery::getQuery()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 2
nop 3
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery;
4
5
use Drupal\Core\Cache\CacheableMetadata;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Entity\EntityTypeManagerInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Youshido\GraphQL\Execution\ResolveInfo;
12
13
/**
14
 * Retrieve a list of entities through an entity query.
15
 *
16
 * @GraphQLField(
17
 *   id = "entity_query",
18
 *   secure = true,
19
 *   name = "entityQuery",
20
 *   type = "EntityQueryResult!",
21
 *   weight = -1,
22
 *   arguments = {
23
 *     "offset" = {
24
 *       "type" = "Int",
25
 *       "default" = 0
26
 *     },
27
 *     "limit" = {
28
 *       "type" = "Int",
29
 *       "default" = 10
30
 *     }
31
 *   },
32
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityQueryDeriver"
33
 * )
34
 */
35
class EntityQuery extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
36
  use DependencySerializationTrait;
37
38
  /**
39
   * The entity type manager.
40
   *
41
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
42
   */
43
  protected $entityTypeManager;
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
49
    $this->entityTypeManager = $entityTypeManager;
50
    parent::__construct($configuration, $pluginId, $pluginDefinition);
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
57
    return new static(
58
      $configuration,
59
      $pluginId,
60
      $pluginDefinition,
61
      $container->get('entity_type.manager')
62
    );
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  protected function getCacheDependencies(array $result, $value, array $args, ResolveInfo $info) {
69
    $entityTypeId = $this->pluginDefinition['entity_type'];
70
    $type = $this->entityTypeManager->getDefinition($entityTypeId);
71
72
    $metadata = new CacheableMetadata();
73
    $metadata->addCacheTags($type->getListCacheTags());
74
    $metadata->addCacheContexts($type->getListCacheContexts());
75
76
    return [$metadata];
77
  }
78
79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function resolveValues($value, array $args, ResolveInfo $info) {
83
    yield $this->getQuery($value, $args, $info);
84
  }
85
86
  /**
87
   * Create an entity query for the plugin's entity type.
88
   *
89
   * @param mixed $value
90
   *   The parent entity type.
91
   * @param array $args
92
   *   The field arguments array.
93
   * @param \Youshido\GraphQL\Execution\ResolveInfo $info
94
   *   The resolve info object.
95
   *
96
   * @return \Drupal\Core\Entity\Query\QueryInterface
97
   *   The entity query object.
98
   */
99
  protected function getQuery($value, array $args, ResolveInfo $info) {
100
    $entityTypeId = $this->pluginDefinition['entity_type'];
101
    $entityStorage = $this->entityTypeManager->getStorage($entityTypeId);
102
    $entityType = $this->entityTypeManager->getDefinition($entityTypeId);
103
104
    $query = $entityStorage->getQuery();
105
    $query->range($args['offset'], $args['limit']);
106
    $query->sort($entityType->getKey('id'));
107
    $query->accessCheck(TRUE);
108
109
    if (!empty($args['filter'])) {
110
      /** @var \Youshido\GraphQL\Type\Object\AbstractObjectType $filter */
111
      $filter = $info->getField()->getArgument('filter')->getType();
112
      /** @var \Drupal\graphql\GraphQL\Type\InputObjectType $filterType */
113
      $filterType = $filter->getNamedType();
114
      $filterFields = $filterType->getPlugin()->getPluginDefinition()['fields'];
115
116
      foreach ($args['filter'] as $key => $arg) {
117
        $query->condition($filterFields[$key]['field_name'], $arg);
118
      }
119
    }
120
121
    return $query;
122
  }
123
124
}
125