-
Notifications
You must be signed in to change notification settings - Fork 13
/
Controller.php
149 lines (135 loc) · 4.68 KB
/
Controller.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace SilverStripe\GraphQLDevTools;
use SilverStripe\Control\Controller as BaseController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\GraphQL\Controller as GraphQLController;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Injector\InjectorNotFoundException;
use SilverStripe\Core\Path;
use SilverStripe\GraphQL\Schema\Schema;
use SilverStripe\Security\SecurityToken;
use SilverStripe\View\Requirements;
class Controller extends BaseController
{
/**
* @var string
* @config
*/
private static $default_schema = 'default';
/**
* @var array
* @config
*/
private static $schemas = [];
/**
* @var string
*/
protected $template = 'DevTools';
public function index(HTTPRequest $request)
{
$routes = $this->getRoutes();
$endpoint = sizeof($routes ?? []) === 1 ? $routes[0] : null;
$csrf = SecurityToken::inst()->getValue();
$tabs = [];
if (sizeof($routes ?? []) > 1) {
foreach ($routes as $route) {
$tabs[] = [
'endpoint' => Director::absoluteURL($route),
'query' => '',
'name' => $route,
'headers' => [
'X-CSRF-TOKEN' => $csrf,
]
];
}
}
$data = [
'headers' => [
'X-CSRF-TOKEN' => $csrf,
],
'endpoint' => $endpoint,
'settings' => [
'request.globalHeaders' => [
'X-CSRF-TOKEN' => $csrf,
],
'request.credentials' => 'include',
],
];
if ($tabs) {
$data['tabs'] = $tabs;
}
$jsonPayload = json_encode($data);
Requirements::customScript(<<<JS
window.addEventListener('load', function (event) {
GraphQLPlayground.init(document.getElementById('root'), $jsonPayload)
});
JS
);
return [
'Endpoint' => $endpoint,
'TabsJSON' => $tabs ? json_encode($tabs): null,
];
}
private function getRoutes(): array
{
$schemaOverride = $this->getRequest()->getVar('schema');
if ($schemaOverride) {
$schemas = [$schemaOverride];
} else {
$schemas = $this->config()->get('schemas');
}
$routes = $this->findAvailableRoutes($schemas);
$defaultSchema = $this->config()->get('default_schema');
$defaultRoute = $routes[$defaultSchema] ?? null;
$allRoutes = array_values($routes ?? []);
if (!$defaultRoute) {
if (sizeof($allRoutes ?? []) === 1) {
$defaultRoute = $allRoutes[0];
} else {
throw new \RuntimeException(
"Could not find your default schema '$defaultSchema'. You will need to add one
to the SilverStripe\Control\Director.rules config setting."
);
}
}
array_unshift($allRoutes, $defaultRoute);
$uniqueRoutes = array_unique($allRoutes ?? []);
return array_map(function ($route) {
return Path::join(Director::baseURL(), $route);
}, $uniqueRoutes ?? []);
}
/**
* Find all available graphql routes
* @param array|string $schemas
* @return array
*/
protected function findAvailableRoutes($schemas = []): array
{
$routes = [];
$rules = Director::config()->get('rules');
foreach ($rules as $pattern => $controllerInfo) {
$routeClass = (is_string($controllerInfo)) ? $controllerInfo : $controllerInfo['Controller'];
$explicitSchema = $controllerInfo['Schema'] ?? null;
if ($explicitSchema) {
if ($schemas === '*' || in_array($explicitSchema, $schemas ?? [])) {
$routes[$explicitSchema] = Path::normalise($pattern, true);
}
continue;
}
try {
$routeController = Injector::inst()->get($routeClass);
if ($routeController instanceof GraphQLController) {
$schemaKey = class_exists(Schema::class)
? $routeController->getSchemaKey()
: $routeController->getManager()->getSchemaKey();
if ($schemas === '*' || in_array($schemaKey, $schemas ?? [])) {
$routes[$schemaKey] = Path::normalise($pattern, true);
}
}
} catch (InjectorNotFoundException $ex) {
}
}
return $routes;
}
}