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 { |
|
|
|
|
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
|
|
|
|