Completed
Push — 8.x-3.x ( d7a068...174067 )
by Sebastian
04:46
created

EntityQuery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 72
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 1
A getCacheDependencies() 0 10 1
B resolveValues() 0 23 4
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
 *   nullable = false,
22
 *   weight = -1,
23
 *   arguments = {
24
 *     "offset" = {
25
 *       "type" = "Int",
26
 *       "nullable" = true,
27
 *       "default" = 0
28
 *     },
29
 *     "limit" = {
30
 *       "type" = "Int",
31
 *       "nullable" = true,
32
 *       "default" = 10
33
 *     }
34
 *   },
35
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityQueryDeriver"
36
 * )
37
 */
38
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...
39
  use DependencySerializationTrait;
40
41
  /**
42
   * The entity type manager.
43
   *
44
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
45
   */
46
  protected $entityTypeManager;
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
52
    $this->entityTypeManager = $entityTypeManager;
53
    parent::__construct($configuration, $pluginId, $pluginDefinition);
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
60
    return new static(
61
      $configuration,
62
      $pluginId,
63
      $pluginDefinition,
64
      $container->get('entity_type.manager')
65
    );
66
  }
67
68
  /**
69
   * {@inheritdoc}
70
   */
71
  protected function getCacheDependencies($result, $value, array $args) {
72
    $entityTypeId = $this->pluginDefinition['entity_type'];
73
    $type = $this->entityTypeManager->getDefinition($entityTypeId);
74
75
    $metadata = new CacheableMetadata();
76
    $metadata->addCacheTags($type->getListCacheTags());
77
    $metadata->addCacheContexts($type->getListCacheContexts());
78
79
    return [$metadata];
80
  }
81
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function resolveValues($value, array $args, ResolveInfo $info) {
86
    $entityTypeId = $this->pluginDefinition['entity_type'];
87
    $storage = $this->entityTypeManager->getStorage($entityTypeId);
88
    $type = $this->entityTypeManager->getDefinition($entityTypeId);
89
90
    $query = $storage->getQuery();
91
    $query->range($args['offset'], $args['limit']);
92
    $query->sort($type->getKey('id'));
93
94
    if (array_key_exists('filter', $args) && $args['filter']) {
95
      /** @var \Youshido\GraphQL\Type\Object\AbstractObjectType $filter */
96
      $filter = $info->getField()->getArgument('filter')->getType();
97
      /** @var \Drupal\graphql\GraphQL\Type\InputObjectType $filterType */
98
      $filterType = $filter->getNamedType();
99
      $filterFields = $filterType->getPlugin()->getPluginDefinition()['fields'];
100
101
      foreach ($args['filter'] as $key => $arg) {
102
        $query->condition($filterFields[$key]['field_name'], $arg);
103
      }
104
    }
105
106
    yield $query;
107
  }
108
109
}
110