-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphql.php
57 lines (49 loc) · 1.49 KB
/
graphql.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
<?php
declare(strict_types=1);
require_once __DIR__ . "/vendor/autoload.php";
use GraphQL\GraphQL;
use GraphQL\Utils\BuildSchema;
$resolveEcho = require "echo.php";
$resolveSum = require "sum.php";
try {
$schema = file_get_contents("schema.graphql");
$schema = BuildSchema::build($schema);
$rootValue = [
"echo" => $resolveEcho,
"sum" => $resolveSum,
"prefix" => "Welcome, Dear ",
];
if(isset($_POST["submit"])) {
$query = $_POST["query"] ?? null;
$variableValues = $_POST["variables"] ?? null;
if($variableValues === "") $variableValues = null;
} else {
$rawInput = file_get_contents("php://input");
$input = json_decode($rawInput, true);
$query = $input["query"] ?? null;
$variableValues = $input["variables"] ?? null;
if($input === null) {
print "<form action=\"\" method=\"POST\">";
print "<b>Query:</b><br>";
print "<textarea name=\"query\"></textarea>";
print "<br>";
print "<b>Variables:</b><br>";
print "<textarea name=\"variables\"></textarea>";
print "<br>";
print "<button name=\"submit\">Submit</button>";
print "</form>";
exit();
}
}
header("Content-Type: application/json; charset=UTF-8");
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
} catch (Throwable $e) {
$result = [
"status"=>0,
"debug" => [
"message" => $e->getMessage(),
],
"data"=>[],
];
}
echo json_encode($result);