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\ObjectType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Schema; use GraphQL\Type\Schema;
use GraphQL\GraphQL; use GraphQL\GraphQL;
try { try {
@ -20,7 +20,7 @@ try {
'message' => ['type' => Type::string()], 'message' => ['type' => Type::string()],
], ],
'resolve' => function ($root, $args) { '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([ $schema = new Schema([
'query' => $queryType, 'query' => $queryType,
'mutation' => $mutationType, 'mutation' => $mutationType,
@ -53,14 +55,15 @@ try {
$variableValues = isset($input['variables']) ? $input['variables'] : null; $variableValues = isset($input['variables']) ? $input['variables'] : null;
$rootValue = ['prefix' => 'You said: ']; $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) { } catch (\Exception $e) {
$result = [ $output = [
'error' => [ 'error' => [
'message' => $e->getMessage() 'message' => $e->getMessage()
] ]
]; ];
} }
header('Content-Type: application/json; charset=UTF-8'); 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\Type\Schema;
use \GraphQL\GraphQL; use \GraphQL\GraphQL;
use \GraphQL\Error\FormattedError; use \GraphQL\Error\FormattedError;
use \GraphQL\Error\Debug;
// Disable default PHP error reporting - we have better one for debug mode (see bellow) // Disable default PHP error reporting - we have better one for debug mode (see bellow)
ini_set('display_errors', 0); ini_set('display_errors', 0);
$debug = false;
if (!empty($_GET['debug'])) { 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) { 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 { try {
@ -49,30 +50,21 @@ try {
'query' => Types::query() 'query' => Types::query()
]); ]);
$result = GraphQL::execute( $result = GraphQL::executeQuery(
$schema, $schema,
$data['query'], $data['query'],
null, null,
$appContext, $appContext,
(array) $data['variables'] (array) $data['variables']
); );
$output = $result->toArray($debug);
// Add reported PHP errors to result (if any)
if (!empty($_GET['debug']) && !empty($phpErrors)) {
$result['extensions']['phpErrors'] = array_map(
['GraphQL\Error\FormattedError', 'createFromPHPError'],
$phpErrors
);
}
$httpStatus = 200; $httpStatus = 200;
} catch (\Exception $error) { } catch (\Exception $error) {
$httpStatus = 500; $httpStatus = 500;
if (!empty($_GET['debug'])) { $output['errors'] = [
$result['extensions']['exception'] = FormattedError::createFromException($error); FormattedError::createFromException($error, $debug)
} else { ];
$result['errors'] = [FormattedError::create('Unexpected Error')];
}
} }
header('Content-Type: application/json', true, $httpStatus); header('Content-Type: application/json', true, $httpStatus);
echo json_encode($result); echo json_encode($output);