-
Notifications
You must be signed in to change notification settings - Fork 59
/
Controller.php
111 lines (95 loc) · 3.06 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
<?php
namespace SilverStripe\GraphQL;
use SilverStripe\Control\Controller as BaseController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Director;
use SilverStripe\GraphQL\Auth\Handler;
use SilverStripe\ORM\Versioning\Versioned;
use Exception;
/**
* @todo CSRF protection (or token-based auth)
*/
class Controller extends BaseController
{
/**
* @var Manager
*/
protected $manager;
/**
* {@inheritDoc}
*/
public function index(HTTPRequest $request)
{
$stage = $request->param('Stage');
if ($stage && in_array($stage, [Versioned::DRAFT, Versioned::LIVE])) {
Versioned::set_stage($stage);
}
$contentType = $request->getHeader('Content-Type') ?: $request->getHeader('content-type');
$isJson = preg_match('#^application/json\b#', $contentType);
if ($isJson) {
$rawBody = $request->getBody();
$data = json_decode($rawBody ?: '', true);
} else {
$data = $request->requestVars();
}
$query = isset($data['query']) ? $data['query'] : null;
$variables = isset($data['variables']) ? $data['variables'] : null;
// Some clients (e.g. GraphiQL) double encode as string
if (is_string($variables)) {
$variables = json_decode($variables, true);
}
$this->setManager($manager = $this->getManager());
try {
$member = $this->getAuthHandler()->requireAuthentication($request);
if ($member) {
$manager->setMember($member);
}
$result = $manager->query($query, $variables);
} catch (Exception $exception) {
$error = ['message' => $exception->getMessage()];
if (Director::isDev()) {
$error['code'] = $exception->getCode();
$error['file'] = $exception->getFile();
$error['line'] = $exception->getLine();
$error['trace'] = $exception->getTrace();
}
$result = [
'errors' => [$error]
];
}
return (new HTTPResponse(json_encode($result)))
->addHeader('Content-Type', 'application/json');
}
/**
* @return Manager
*/
public function getManager()
{
if ($this->manager) {
return $this->manager;
}
// Get a service rather than an instance (to allow procedural configuration)
$config = Config::inst()->get('SilverStripe\GraphQL', 'schema');
$manager = Manager::createFromConfig($config);
return $manager;
}
/**
* @param Manager $manager
*/
public function setManager($manager)
{
$this->manager = $manager;
}
/**
* Get an instance of the authorization Handler to manage any authentication requirements
*
* @return Handler
*/
public function getAuthHandler()
{
return new Handler;
}
}