2016-10-21 14:43:11 +03:00
|
|
|
<?php
|
|
|
|
// Test this using following command
|
2017-01-06 14:50:21 +03:00
|
|
|
// php -S localhost:8080 ./graphql.php
|
2017-01-06 14:46:37 +03:00
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
2016-10-21 14:43:11 +03:00
|
|
|
|
2016-11-06 17:33:13 +03:00
|
|
|
use \GraphQL\Examples\Blog\Types;
|
2016-10-21 14:43:11 +03:00
|
|
|
use \GraphQL\Examples\Blog\AppContext;
|
|
|
|
use \GraphQL\Examples\Blog\Data\DataSource;
|
2017-08-13 20:08:13 +03:00
|
|
|
use \GraphQL\Type\Schema;
|
2016-10-21 14:43:11 +03:00
|
|
|
use \GraphQL\GraphQL;
|
2016-10-23 01:13:55 +03:00
|
|
|
use \GraphQL\Error\FormattedError;
|
2017-08-19 22:31:11 +03:00
|
|
|
use \GraphQL\Error\Debug;
|
2016-10-21 14:43:11 +03:00
|
|
|
|
|
|
|
// Disable default PHP error reporting - we have better one for debug mode (see bellow)
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
2017-08-19 22:31:11 +03:00
|
|
|
$debug = false;
|
2016-10-21 14:43:11 +03:00
|
|
|
if (!empty($_GET['debug'])) {
|
|
|
|
set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
|
2017-08-19 22:31:11 +03:00
|
|
|
throw new ErrorException($message, 0, $severity, $file, $line);
|
2016-10-21 14:43:11 +03:00
|
|
|
});
|
2017-08-19 22:31:11 +03:00
|
|
|
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE;
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-11-06 17:33:13 +03:00
|
|
|
// Initialize our fake data source
|
|
|
|
DataSource::init();
|
2016-10-21 14:43:11 +03:00
|
|
|
|
|
|
|
// Prepare context that will be available in all field resolvers (as 3rd argument):
|
|
|
|
$appContext = new AppContext();
|
2016-11-06 17:33:13 +03:00
|
|
|
$appContext->viewer = DataSource::findUser('1'); // simulated "currently logged-in user"
|
2016-10-21 14:43:11 +03:00
|
|
|
$appContext->rootUrl = 'http://localhost:8080';
|
|
|
|
$appContext->request = $_REQUEST;
|
|
|
|
|
|
|
|
// Parse incoming query and variables
|
|
|
|
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];
|
|
|
|
|
2016-10-23 01:13:55 +03:00
|
|
|
if (null === $data['query']) {
|
2016-11-08 16:02:10 +03:00
|
|
|
$data['query'] = '{hello}';
|
2016-10-23 01:13:55 +03:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:43:11 +03:00
|
|
|
// GraphQL schema to be passed to query executor:
|
|
|
|
$schema = new Schema([
|
2016-11-06 17:33:13 +03:00
|
|
|
'query' => Types::query()
|
2016-10-21 14:43:11 +03:00
|
|
|
]);
|
|
|
|
|
2017-08-19 22:31:11 +03:00
|
|
|
$result = GraphQL::executeQuery(
|
2016-10-21 14:43:11 +03:00
|
|
|
$schema,
|
|
|
|
$data['query'],
|
|
|
|
null,
|
|
|
|
$appContext,
|
|
|
|
(array) $data['variables']
|
|
|
|
);
|
2017-08-19 22:31:11 +03:00
|
|
|
$output = $result->toArray($debug);
|
2016-10-21 14:43:11 +03:00
|
|
|
$httpStatus = 200;
|
|
|
|
} catch (\Exception $error) {
|
|
|
|
$httpStatus = 500;
|
2017-08-19 22:31:11 +03:00
|
|
|
$output['errors'] = [
|
|
|
|
FormattedError::createFromException($error, $debug)
|
|
|
|
];
|
2016-10-21 14:43:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
header('Content-Type: application/json', true, $httpStatus);
|
2017-08-19 22:31:11 +03:00
|
|
|
echo json_encode($output);
|