Skip to content

Commit 5e42995

Browse files
committed
Merge branch '1.0' into 1
2 parents 6bc3873 + 170ec1d commit 5e42995

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/Controller.php

+32-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use SilverStripe\Core\Path;
1212
use SilverStripe\GraphQL\Schema\Schema;
1313
use SilverStripe\Security\SecurityToken;
14+
use SilverStripe\View\Requirements;
1415

1516
class Controller extends BaseController
1617
{
@@ -34,26 +35,51 @@ class Controller extends BaseController
3435
public function index(HTTPRequest $request)
3536
{
3637
$routes = $this->getRoutes();
37-
$json = null;
38+
$endpoint = sizeof($routes ?? []) === 1 ? $routes[0] : null;
39+
$csrf = SecurityToken::inst()->getValue();
40+
$tabs = [];
3841
if (sizeof($routes ?? []) > 1) {
39-
$tabs = [];
4042
foreach ($routes as $route) {
4143
$tabs[] = [
4244
'endpoint' => Director::absoluteURL($route),
4345
'query' => '',
4446
'name' => $route,
4547
'headers' => [
46-
'X-CSRF-TOKEN' => SecurityToken::inst()->getValue(),
48+
'X-CSRF-TOKEN' => $csrf,
4749
]
4850
];
4951
}
52+
}
53+
54+
$data = [
55+
'headers' => [
56+
'X-CSRF-TOKEN' => $csrf,
57+
],
58+
'endpoint' => $endpoint,
59+
'settings' => [
60+
'request.globalHeaders' => [
61+
'X-CSRF-TOKEN' => $csrf,
62+
],
63+
'request.credentials' => 'include',
64+
],
65+
];
5066

51-
$json = json_encode($tabs);
67+
if ($tabs) {
68+
$data['tabs'] = $tabs;
5269
}
5370

71+
$jsonPayload = json_encode($data);
72+
73+
Requirements::customScript(<<<JS
74+
window.addEventListener('load', function (event) {
75+
GraphQLPlayground.init(document.getElementById('root'), $jsonPayload)
76+
});
77+
JS
78+
);
79+
5480
return [
55-
'Endpoint' => sizeof($routes ?? []) === 1 ? $routes[0] : null,
56-
'TabsJSON' => $json,
81+
'Endpoint' => $endpoint,
82+
'TabsJSON' => $tabs ? json_encode($tabs): null,
5783
];
5884
}
5985

templates/DevTools.ss

+1-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<title>GraphQL IDE | Silverstripe CMS</title>
88
<link rel="shortcut icon" href="$resourceURL('silverstripe/graphql-devtools: client/favicon.png')" />
99
<% require javascript('silverstripe/graphql-devtools: client/bundle.js') %>
10-
10+
1111
<style>
1212
html, body {
1313
margin: 0;
@@ -91,22 +91,5 @@
9191
<span class="title">GraphQL Playground</span>
9292
</div>
9393
</div>
94-
<script>window.addEventListener('load', function (event) {
95-
GraphQLPlayground.init(document.getElementById('root'), {
96-
headers: {
97-
'X-CSRF-TOKEN': '$SecurityID',
98-
},
99-
endpoint: '$Endpoint',
100-
settings: {
101-
'request.globalHeaders': {
102-
'X-CSRF-TOKEN': '$SecurityID'
103-
},
104-
'request.credentials': 'include',
105-
},
106-
<% if $TabsJSON %>
107-
tabs: $TabsJSON.RAW
108-
<% end_if %>
109-
})
110-
})</script>
11194
</body>
11295
</html>

0 commit comments

Comments
 (0)