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