2017-07-07 22:26:01 +03:00
|
|
|
<?php
|
|
|
|
// Test this using following command
|
|
|
|
// php -S localhost:8080 ./graphql.php &
|
|
|
|
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
|
|
|
|
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
|
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
use GraphQL\GraphQL;
|
|
|
|
use GraphQL\Utils\BuildSchema;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
$schema = BuildSchema::build(file_get_contents(__DIR__ . '/schema.graphqls'));
|
|
|
|
$rootValue = include __DIR__ . '/rootvalue.php';
|
|
|
|
|
|
|
|
$rawInput = file_get_contents('php://input');
|
|
|
|
$input = json_decode($rawInput, true);
|
|
|
|
$query = $input['query'];
|
|
|
|
$variableValues = isset($input['variables']) ? $input['variables'] : null;
|
|
|
|
|
2018-09-26 12:18:02 +03:00
|
|
|
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
|
2017-07-07 22:26:01 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$result = [
|
|
|
|
'error' => [
|
|
|
|
'message' => $e->getMessage()
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
echo json_encode($result);
|
|
|
|
|