Fixing examples

This commit is contained in:
Vladimir Razuvaev 2017-08-20 02:31:11 +07:00
parent 009cdecb94
commit 8098b2b886
2 changed files with 18 additions and 23 deletions

View File

@ -7,7 +7,7 @@ require_once __DIR__ . '/../../vendor/autoload.php';
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Schema;
use GraphQL\Type\Schema;
use GraphQL\GraphQL;
try {
@ -20,7 +20,7 @@ try {
'message' => ['type' => Type::string()],
],
'resolve' => function ($root, $args) {
return $root['prefix'].$args['message'];
return $root['prefix'] . $args['message'];
}
],
],
@ -42,6 +42,8 @@ try {
],
]);
// See docs on schema options:
// http://webonyx.github.io/graphql-php/type-system/schema/#configuration-options
$schema = new Schema([
'query' => $queryType,
'mutation' => $mutationType,
@ -53,14 +55,15 @@ try {
$variableValues = isset($input['variables']) ? $input['variables'] : null;
$rootValue = ['prefix' => 'You said: '];
$result = GraphQL::execute($schema, $query, $rootValue, null, $variableValues);
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
$output = $result->toArray();
} catch (\Exception $e) {
$result = [
$output = [
'error' => [
'message' => $e->getMessage()
]
];
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($result);
echo json_encode($output);

View File

@ -9,16 +9,17 @@ use \GraphQL\Examples\Blog\Data\DataSource;
use \GraphQL\Type\Schema;
use \GraphQL\GraphQL;
use \GraphQL\Error\FormattedError;
use \GraphQL\Error\Debug;
// Disable default PHP error reporting - we have better one for debug mode (see bellow)
ini_set('display_errors', 0);
$debug = false;
if (!empty($_GET['debug'])) {
// Catch custom errors (to report them in query results if debugging is enabled)
$phpErrors = [];
set_error_handler(function($severity, $message, $file, $line) use (&$phpErrors) {
$phpErrors[] = new ErrorException($message, 0, $severity, $file, $line);
throw new ErrorException($message, 0, $severity, $file, $line);
});
$debug = Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE;
}
try {
@ -49,30 +50,21 @@ try {
'query' => Types::query()
]);
$result = GraphQL::execute(
$result = GraphQL::executeQuery(
$schema,
$data['query'],
null,
$appContext,
(array) $data['variables']
);
// Add reported PHP errors to result (if any)
if (!empty($_GET['debug']) && !empty($phpErrors)) {
$result['extensions']['phpErrors'] = array_map(
['GraphQL\Error\FormattedError', 'createFromPHPError'],
$phpErrors
);
}
$output = $result->toArray($debug);
$httpStatus = 200;
} catch (\Exception $error) {
$httpStatus = 500;
if (!empty($_GET['debug'])) {
$result['extensions']['exception'] = FormattedError::createFromException($error);
} else {
$result['errors'] = [FormattedError::create('Unexpected Error')];
}
$output['errors'] = [
FormattedError::createFromException($error, $debug)
];
}
header('Content-Type: application/json', true, $httpStatus);
echo json_encode($result);
echo json_encode($output);