1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
7
|
|
|
use Drupal\Core\Entity\Query\QueryInterface; |
8
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
9
|
|
|
use Drupal\graphql\GraphQL\Buffers\EntityBuffer; |
10
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
11
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Retrieve the entity result set of an entity query. |
17
|
|
|
* |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "entity_query_entities", |
20
|
|
|
* secure = true, |
21
|
|
|
* name = "entities", |
22
|
|
|
* type = "[Entity]", |
23
|
|
|
* parents = {"EntityQueryResult"} |
24
|
|
|
* ) |
25
|
|
|
*/ |
26
|
|
|
class EntityQueryEntities extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
27
|
|
|
use DependencySerializationTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The entity buffer service. |
31
|
|
|
* |
32
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer |
33
|
|
|
*/ |
34
|
|
|
protected $entityBuffer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The entity type manager service. |
38
|
|
|
* |
39
|
|
|
* @var \Drupal\Core\Entity\EntityTypeManagerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $entityTypeManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* EntityQueryEntities constructor. |
45
|
|
|
* |
46
|
|
|
* @param array $configuration |
47
|
|
|
* The plugin configuration array. |
48
|
|
|
* @param string $pluginId |
49
|
|
|
* The plugin id. |
50
|
|
|
* @param mixed $pluginDefinition |
51
|
|
|
* The plugin definition array. |
52
|
|
|
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager |
53
|
|
|
* The entity type manager service. |
54
|
|
|
* @param \Drupal\graphql\GraphQL\Buffers\EntityBuffer $entityBuffer |
55
|
|
|
* The entity buffer service. |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
array $configuration, |
59
|
|
|
$pluginId, |
60
|
|
|
$pluginDefinition, |
61
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
62
|
|
|
EntityBuffer $entityBuffer |
63
|
|
|
) { |
64
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
65
|
|
|
$this->entityBuffer = $entityBuffer; |
66
|
|
|
$this->entityTypeManager = $entityTypeManager; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
73
|
|
|
return new static( |
74
|
|
|
$configuration, |
75
|
|
|
$pluginId, |
76
|
|
|
$pluginDefinition, |
77
|
|
|
$container->get('entity_type.manager'), |
78
|
|
|
$container->get('graphql.buffer.entity') |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
86
|
|
|
if ($value instanceof QueryInterface) { |
|
|
|
|
87
|
|
|
$type = $value->getEntityTypeId(); |
88
|
|
|
$result = $value->execute(); |
89
|
|
|
|
90
|
|
|
if ($value->hasTag('revisions')) { |
91
|
|
|
return $this->resolveFromRevisionIds($type, array_keys($result)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// If this is a revision query, the version ids are the array keys. |
95
|
|
|
return $this->resolveFromEntityIds($type, array_values($result)); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Resolves entities lazily through the entity buffer. |
101
|
|
|
* |
102
|
|
|
* @param string $type |
103
|
|
|
* The entity type. |
104
|
|
|
* @param array $ids |
105
|
|
|
* The entity ids to load. |
106
|
|
|
* |
107
|
|
|
* @return \Closure |
108
|
|
|
* The deferred resolver. |
109
|
|
|
*/ |
110
|
|
|
protected function resolveFromEntityIds($type, $ids) { |
111
|
|
|
$resolve = $this->entityBuffer->add($type, $ids); |
112
|
|
|
return function($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
113
|
|
|
return $this->resolveEntities($resolve()); |
114
|
|
|
}; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Resolves entity revisions. |
119
|
|
|
* |
120
|
|
|
* @param string $type |
121
|
|
|
* The entity type. |
122
|
|
|
* @param array $ids |
123
|
|
|
* The entity revision ids to load. |
124
|
|
|
* |
125
|
|
|
* @return \Generator |
126
|
|
|
* The resolved revisions. |
127
|
|
|
*/ |
128
|
|
|
protected function resolveFromRevisionIds($type, $ids) { |
129
|
|
|
$storage = $this->entityTypeManager->getStorage($type); |
130
|
|
|
$entities = array_map(function ($id) use ($storage) { |
131
|
|
|
return $storage->loadRevision($id); |
132
|
|
|
}, $ids); |
133
|
|
|
|
134
|
|
|
return $this->resolveEntities($entities); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Resolves entity objects and checks view permissions. |
139
|
|
|
* |
140
|
|
|
* @param array $entities |
141
|
|
|
* The entities to resolve. |
142
|
|
|
* |
143
|
|
|
* @return \Generator |
144
|
|
|
* The resolved entities. |
145
|
|
|
*/ |
146
|
|
|
protected function resolveEntities(array $entities) { |
147
|
|
|
/** @var \Drupal\Core\Entity\EntityInterface $entity */ |
148
|
|
|
foreach ($entities as $entity) { |
149
|
|
|
$access = $entity->access('view', NULL, TRUE); |
150
|
|
|
|
151
|
|
View Code Duplication |
if ($access->isAllowed()) { |
|
|
|
|
152
|
|
|
yield $entity->addCacheableDependency($access); |
153
|
|
|
} |
154
|
|
|
else { |
155
|
|
|
yield new CacheableValue(NULL, [$access]); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|