-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_bootstrap.php
75 lines (58 loc) · 1.8 KB
/
graph_bootstrap.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
<?php
use \GraphApp\AppContext;
use \GraphApp\AppObjectType;
use \GraphApp\Routes;
use \GraphQL\Type\Schema;
use \GraphQL\GraphQL;
use \GraphQL\Error\FormattedError;
use \GraphQL\Error\Debug;
$whoops = new \Whoops\Run;
$whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler);
$whoops->register();
require APP_DIRECTORY.'/helpers.php';
$dotenv = Dotenv\Dotenv::create(BASE_PATH);
$dotenv->load();
\GraphApp\Models\Model::$config = config('database');
\GraphApp\Models\DB::enableQueryLog();
$debug = false;
if (!empty($_GET['debug'])) {
set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
throw new ErrorException($message, 0, $severity, $file, $line);
});
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE;
}
try {
$appContext = new AppContext();
$appContext->rootUrl = url();
$appContext->request = $_REQUEST;
if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) {
$raw = file_get_contents('php://input') ?: '';
$data = json_decode($raw, true) ?: [];
} else {
$data = $_REQUEST;
}
$data += ['query' => null, 'variables' => null];
$schema = new Schema([
'query' => AppObjectType::query(Routes::queries()),
'mutation' => AppObjectType::mutation(Routes::mutations()),
]);
$result = GraphQL::executeQuery(
$schema,
$data['query'],
null,
$appContext,
(array) $data['variables']
);
$output = $result->toArray($debug);
$httpStatus = 200;
} catch (\Exception $error) {
$httpStatus = 500;
$output['errors'] = [
FormattedError::createFromException($error, $debug)
];
}
if ($debug) {
$output['queries'] = \GraphApp\Models\DB::getQueryLog();
}
header('Content-Type: application/json', true, $httpStatus);
echo json_encode($output, JSON_UNESCAPED_UNICODE);