-
Notifications
You must be signed in to change notification settings - Fork 60
/
Build.php
85 lines (72 loc) · 2.39 KB
/
Build.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
<?php
namespace SilverStripe\GraphQL\Dev;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\DebugView;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\GraphQL\Schema\SchemaFactory;
use SilverStripe\ORM\DatabaseAdmin;
class Build extends Controller
{
private static $url_handlers = [
'' => 'build'
];
private static $allowed_actions = [
'build'
];
/**
* @param HTTPRequest $request
* @throws SchemaBuilderException
*/
public function build(HTTPRequest $request)
{
$isBrowser = !Director::is_cli();
if ($isBrowser) {
$renderer = DebugView::create();
echo $renderer->renderHeader();
echo $renderer->renderInfo("GraphQL Schema Builder", Director::absoluteBaseURL());
echo "<div class=\"build\">";
}
$clear = (bool) $request->getVar('clear');
$this->buildSchema($request->getVar('schema'), $clear);
if ($isBrowser) {
echo "</div>";
echo $renderer->renderFooter();
}
}
/**
* @param null $key
* @param bool $clear
* @throws SchemaBuilderException
*/
public function buildSchema($key = null, $clear = false): void
{
Schema::setVerbose(true);
$keys = $key ? [$key] : array_keys(Schema::config()->get('schemas'));
$keys = array_filter($keys, function ($key) {
return $key !== Schema::ALL;
});
foreach ($keys as $key) {
Benchmark::start('build-schema-' . $key);
Schema::message(sprintf('--- Building schema "%s" ---', $key));
$schema = SchemaFactory::singleton()->boot($key);
if ($clear) {
$schema->getStore()->clear();
}
$schema->process();
// Allow for "empty" schemas which don't have any types defined.
// This enables baseline configuration of the "default" schema.
if ($schema->exists()) {
} else {
Schema::message('No types defined, skipping');
continue;
}
$schema->save();
Schema::message(
Benchmark::end('build-schema-' . $key, 'Built schema in %sms.')
);
}
}
}