/
BulkLoaderSet.php
127 lines (112 loc) · 3.43 KB
/
BulkLoaderSet.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace SilverStripe\GraphQL\Schema\BulkLoader;
use InvalidArgumentException;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\ClassInfo;
use ReflectionException;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Interfaces\ConfigurationApplier;
use SilverStripe\GraphQL\Schema\Logger;
use SilverStripe\GraphQL\Schema\Schema;
/**
* Composed with a list of bulk loaders to be executed in serial and return the aggregate result
* of all their collect() calls
*/
class BulkLoaderSet implements ConfigurationApplier
{
use Injectable;
/**
* @var AbstractBulkLoader[]
*/
private array $loaders;
private Collection $initialCollection;
/**
* BulkLoaderSet constructor.
* @param AbstractBulkLoader[] $loaders
* @param Collection|null $initialCollection
* @throws ReflectionException
*/
public function __construct(array $loaders = [], ?Collection $initialCollection = null)
{
$this->setLoaders($loaders);
if ($initialCollection) {
$this->initialCollection = $initialCollection;
} else {
$this->initialCollection = Collection::createFromClassList(ClassInfo::allClasses());
}
}
/**
* @param array $config
* @return $this
* @throws SchemaBuilderException
*/
public function applyConfig(array $config): self
{
$registry = Registry::inst();
foreach ($config as $loaderID => $loaderConfig) {
/* @var AbstractBulkLoader $loader */
$loader = $registry->getByID($loaderID);
Schema::invariant($loader, 'Loader "%s" does not exist', $loaderID);
$loader->applyConfig($loaderConfig);
$this->addLoader($loader);
}
return $this;
}
/**
* @param AbstractBulkLoader $loader
* @return $this
*/
public function addLoader(AbstractBulkLoader $loader): self
{
$this->loaders[] = $loader;
return $this;
}
/**
* @return Collection
*/
public function process(): Collection
{
$logger = Injector::inst()->get(LoggerInterface::class . '.graphql-build');
$collection = $this->initialCollection;
$logger->debug(sprintf(
'Bulk loader initial collection size: %s',
count($collection->getClasses() ?? [])
));
foreach ($this->loaders as $loader) {
$collection = $loader->collect($collection);
$logger->debug(sprintf(
'Loader %s reduced bulk load to %s',
$loader->getIdentifier(),
count($collection->getClasses() ?? [])
));
}
return $collection;
}
/**
* @param array $loaders
* @return $this
*/
public function setLoaders(array $loaders): self
{
foreach ($loaders as $loader) {
if (!$loader instanceof AbstractBulkLoader) {
throw new InvalidArgumentException(sprintf(
'%s only accepts instances of %s',
static::class,
AbstractBulkLoader::class
));
}
}
$this->loaders = $loaders;
return $this;
}
/**
* @return AbstractBulkLoader[]
*/
public function getLoaders(): array
{
return $this->loaders;
}
}