1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\EntityQuery; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Entity\Query\QueryInterface; |
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
8
|
|
|
use Drupal\graphql\GraphQL\Buffers\EntityBuffer; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Retrieve the entity result set of an entity query. |
15
|
|
|
* |
16
|
|
|
* @GraphQLField( |
17
|
|
|
* id = "entity_query_entities", |
18
|
|
|
* secure = true, |
19
|
|
|
* name = "entities", |
20
|
|
|
* type = "[Entity]", |
21
|
|
|
* parents = {"EntityQueryResult"} |
22
|
|
|
* ) |
23
|
|
|
*/ |
24
|
|
|
class EntityQueryEntities extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
25
|
|
|
use DependencySerializationTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The entity buffer service. |
29
|
|
|
* |
30
|
|
|
* @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer |
31
|
|
|
*/ |
32
|
|
|
protected $entityBuffer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function __construct( |
38
|
|
|
array $configuration, |
39
|
|
|
$pluginId, |
40
|
|
|
$pluginDefinition, |
41
|
|
|
EntityBuffer $entityBuffer |
42
|
|
|
) { |
43
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
44
|
|
|
$this->entityBuffer = $entityBuffer; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
51
|
|
|
return new static( |
52
|
|
|
$configuration, |
53
|
|
|
$pluginId, |
54
|
|
|
$pluginDefinition, |
55
|
|
|
$container->get('graphql.buffer.entity') |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function resolveValues($value, array $args, ResolveInfo $info) { |
63
|
|
|
if ($value instanceof QueryInterface) { |
|
|
|
|
64
|
|
|
$resolve = $this->entityBuffer->add($value->getEntityTypeId(), $value->execute()); |
65
|
|
|
return function($value, array $args, ResolveInfo $info) use ($resolve) { |
|
|
|
|
66
|
|
|
$entities = $resolve(); |
67
|
|
|
|
68
|
|
|
/** @var \Drupal\Core\Entity\EntityInterface $entity */ |
69
|
|
|
foreach ($entities as $entity) { |
70
|
|
|
$access = $entity->access('view', NULL, TRUE); |
71
|
|
|
$this->commitCacheableDependency($info, $access); |
72
|
|
|
yield $access->isAllowed() ? $entity : NULL; |
73
|
|
|
} |
74
|
|
|
}; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|