-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathQueryConnection.php
52 lines (43 loc) · 1.02 KB
/
QueryConnection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Drupal\graphql_examples\Wrappers;
use Drupal\Core\Entity\Query\QueryInterface;
use GraphQL\Deferred;
/**
* Helper class that wraps entity queries.
*/
class QueryConnection {
/**
* @var \Drupal\Core\Entity\Query\QueryInterface
*/
protected $query;
/**
* QueryConnection constructor.
*
* @param \Drupal\Core\Entity\Query\QueryInterface $query
*/
public function __construct(QueryInterface $query) {
$this->query = $query;
}
/**
* @return int
*/
public function total() {
$query = clone $this->query;
$query->range(NULL, NULL)->count();
return $query->execute();
}
/**
* @return array|\GraphQL\Deferred
*/
public function items() {
$result = $this->query->execute();
if (empty($result)) {
return [];
}
$buffer = \Drupal::service('graphql.buffer.entity');
$callback = $buffer->add($this->query->getEntityTypeId(), array_values($result));
return new Deferred(function () use ($callback) {
return $callback();
});
}
}