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