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
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
array $configuration, |
48
|
|
|
$pluginId, |
49
|
|
|
$pluginDefinition, |
50
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
51
|
|
|
EntityBuffer $entityBuffer |
52
|
|
|
) { |
53
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
54
|
|
|
$this->entityBuffer = $entityBuffer; |
55
|
|
|
$this->entityTypeManager = $entityTypeManager; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
62
|
|
|
return new static( |
63
|
|
|
$configuration, |
64
|
|
|
$pluginId, |
65
|
|
|
$pluginDefinition, |
66
|
|
|
$container->get('entity_type.manager'), |
67
|
|
|
$container->get('graphql.buffer.entity') |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
75
|
|
|
if ($value instanceof QueryInterface) { |
|
|
|
|
76
|
|
|
$type = $value->getEntityTypeId(); |
77
|
|
|
$result = $value->execute(); |
78
|
|
|
|
79
|
|
|
if ($value->hasTag('revisions')) { |
80
|
|
|
return $this->resolveFromRevisionIds($type, array_keys($result)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// If this is a revision query, the version ids are the array keys. |
84
|
|
|
return $this->resolveFromEntityIds($type, array_values($result)); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function resolveFromEntityIds($type, $ids) { |
89
|
|
|
$resolve = $this->entityBuffer->add($type, $ids); |
90
|
|
|
return function($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
91
|
|
|
return $this->resolveEntities($resolve()); |
92
|
|
|
}; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function resolveFromRevisionIds($type, $ids) { |
96
|
|
|
$storage = $this->entityTypeManager->getStorage($type); |
97
|
|
|
$entities = array_map(function ($id) use ($storage) { |
98
|
|
|
return $storage->loadRevision($id); |
99
|
|
|
}, $ids); |
100
|
|
|
|
101
|
|
|
return $this->resolveEntities($entities); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function resolveEntities(array $entities) { |
105
|
|
|
/** @var \Drupal\Core\Entity\EntityInterface $entity */ |
106
|
|
|
foreach ($entities as $entity) { |
107
|
|
|
$access = $entity->access('view', NULL, TRUE); |
108
|
|
|
|
109
|
|
View Code Duplication |
if ($access->isAllowed()) { |
|
|
|
|
110
|
|
|
yield $entity->addCacheableDependency($access); |
111
|
|
|
} |
112
|
|
|
else { |
113
|
|
|
yield new CacheableValue(NULL, [$access]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|