From 8098b2b88638768f79130fa3b297b37bfcf4929a Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Sun, 20 Aug 2017 02:31:11 +0700 Subject: [PATCH] Fixing examples --- examples/00-hello-world/graphql.php | 13 ++++++++----- examples/01-blog/graphql.php | 28 ++++++++++------------------ 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/examples/00-hello-world/graphql.php b/examples/00-hello-world/graphql.php index 3f6afaf..30b1f9c 100644 --- a/examples/00-hello-world/graphql.php +++ b/examples/00-hello-world/graphql.php @@ -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); diff --git a/examples/01-blog/graphql.php b/examples/01-blog/graphql.php index a471dde..58dbe98 100644 --- a/examples/01-blog/graphql.php +++ b/examples/01-blog/graphql.php @@ -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);