2023-02-01 12:50:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
|
|
|
use GraphQL\GraphQL as GQL;
|
|
|
|
use GraphQL\Server\StandardServer;
|
|
|
|
use GraphQL\Error\DebugFlag;
|
2023-02-04 18:24:34 +00:00
|
|
|
use GraphQL\Type\Schema;
|
2023-02-01 12:50:00 +00:00
|
|
|
use GraphQL\Utils\SchemaPrinter;
|
|
|
|
|
|
|
|
class GraphQL extends Extension
|
|
|
|
{
|
2023-02-04 18:24:34 +00:00
|
|
|
public static function get_schema(): Schema
|
|
|
|
{
|
|
|
|
global $_tracer;
|
|
|
|
$_tracer->begin("Create Schema");
|
2023-02-05 15:56:06 +00:00
|
|
|
$schema = new \GQLA\Schema();
|
2023-02-04 18:24:34 +00:00
|
|
|
$_tracer->end(null);
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:18:21 +00:00
|
|
|
private function cors(): void
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$pat = $config->get_string("graphql_cors_pattern");
|
|
|
|
|
|
|
|
if ($pat && isset($_SERVER['HTTP_ORIGIN'])) {
|
|
|
|
if (preg_match("#$pat#", $_SERVER['HTTP_ORIGIN'])) {
|
|
|
|
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
|
|
|
|
header('Access-Control-Allow-Credentials: true');
|
|
|
|
header('Access-Control-Max-Age: 86400');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
|
|
|
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
|
|
|
|
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
|
|
|
|
}
|
|
|
|
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
|
|
|
|
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_string('graphql_cors_pattern', "");
|
|
|
|
$config->set_default_bool('graphql_debug', false);
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:50:00 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event)
|
|
|
|
{
|
2023-02-07 14:18:21 +00:00
|
|
|
global $config, $page;
|
2023-02-02 16:50:09 +00:00
|
|
|
if ($event->page_matches("graphql")) {
|
2023-02-07 14:18:21 +00:00
|
|
|
$this->cors();
|
2023-02-01 12:50:00 +00:00
|
|
|
$t1 = ftime();
|
|
|
|
$server = new StandardServer([
|
2023-02-04 18:24:34 +00:00
|
|
|
'schema' => $this->get_schema(),
|
2023-02-01 12:50:00 +00:00
|
|
|
]);
|
|
|
|
$t2 = ftime();
|
|
|
|
$resp = $server->executeRequest();
|
2023-02-07 14:18:21 +00:00
|
|
|
if ($config->get_bool("graphql_debug")) {
|
|
|
|
$debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS;
|
|
|
|
$body = $resp->toArray($debug);
|
|
|
|
} else {
|
|
|
|
$body = $resp->toArray();
|
|
|
|
}
|
2023-02-01 12:50:00 +00:00
|
|
|
$t3 = ftime();
|
|
|
|
$body['stats'] = get_debug_info_arr();
|
|
|
|
$body['stats']['graphql_schema_time'] = round($t2 - $t1, 2);
|
|
|
|
$body['stats']['graphql_execute_time'] = round($t3 - $t2, 2);
|
|
|
|
$page->set_mode(PageMode::DATA);
|
|
|
|
$page->set_mime("application/json");
|
|
|
|
$page->set_data(\json_encode($body, JSON_UNESCAPED_UNICODE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onCommand(CommandEvent $event)
|
|
|
|
{
|
|
|
|
if ($event->cmd == "help") {
|
|
|
|
print "\tgraphql <query string>\n";
|
2023-02-04 18:24:34 +00:00
|
|
|
print "\t\teg 'graphql \"{ post(id: 18) { id, hash } }\"'\n\n";
|
2023-02-01 12:50:00 +00:00
|
|
|
print "\tgraphql-schema\n";
|
|
|
|
print "\t\tdump the schema\n\n";
|
|
|
|
}
|
|
|
|
if ($event->cmd == "graphql") {
|
|
|
|
$t1 = ftime();
|
2023-02-04 18:24:34 +00:00
|
|
|
$schema = $this->get_schema();
|
2023-02-01 12:50:00 +00:00
|
|
|
$t2 = ftime();
|
|
|
|
$debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS;
|
|
|
|
$body = GQL::executeQuery($schema, $event->args[0])->toArray($debug);
|
|
|
|
$t3 = ftime();
|
|
|
|
$body['stats'] = get_debug_info_arr();
|
|
|
|
$body['stats']['graphql_schema_time'] = round($t2 - $t1, 2);
|
|
|
|
$body['stats']['graphql_execute_time'] = round($t3 - $t2, 2);
|
|
|
|
echo \json_encode($body, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT);
|
|
|
|
}
|
|
|
|
if ($event->cmd == "graphql-schema") {
|
2023-02-04 18:24:34 +00:00
|
|
|
$schema = $this->get_schema();
|
2023-02-01 12:50:00 +00:00
|
|
|
echo(SchemaPrinter::doPrint($schema));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|