graphql-php/examples/02-shorthand/graphql.php
Simon Podlipsky 68bfde953d
PHPBench
2018-09-26 11:18:14 +02:00

32 lines
1004 B
PHP

<?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;
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
} catch (\Exception $e) {
$result = [
'error' => [
'message' => $e->getMessage()
]
];
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($result);