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