|
16 | 16 | use GraphQL\Error\DebugFlag; |
17 | 17 |
|
18 | 18 | // Disable default PHP error reporting - we have better one for debug mode (see bellow) |
19 | | -ini_set('display_errors', 0); |
| 19 | +ini_set('display_errors', '0'); |
20 | 20 |
|
21 | | -$debug = false; |
| 21 | +$debug = DebugFlag::NONE; |
22 | 22 | if (!empty($_GET['debug'])) { |
23 | | - set_error_handler(function ($severity, $message, $file, $line) use (&$phpErrors) { |
| 23 | + set_error_handler(function ($severity, $message, $file, $line) { |
24 | 24 | throw new ErrorException($message, 0, $severity, $file, $line); |
25 | 25 | }); |
26 | 26 | $debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE; |
|
33 | 33 | // Prepare context that will be available in all field resolvers (as 3rd argument): |
34 | 34 | $appContext = new AppContext(); |
35 | 35 | $appContext->viewer = ''; |
36 | | - $appContext->rootUrl = $_SERVER['REQUEST_URI']; |
| 36 | + $appContext->rootUrl = $_SERVER['REQUEST_URI']; // @phpstan-ignore assign.propertyType |
37 | 37 | $appContext->request = $_REQUEST; |
38 | 38 |
|
39 | 39 | // Parse incoming query and variables |
40 | | - if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) { |
| 40 | + if ( |
| 41 | + isset($_SERVER['CONTENT_TYPE']) |
| 42 | + && is_string($_SERVER['CONTENT_TYPE']) |
| 43 | + && strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false |
| 44 | + ) { |
41 | 45 | $raw = file_get_contents('php://input') ?: ''; |
42 | | - $data = json_decode($raw, true); |
43 | | - } |
44 | | - |
45 | | - if (is_null($data)) { |
| 46 | + $data = (array)json_decode($raw, true); |
| 47 | + } else { |
46 | 48 | $data = $appContext->request; |
47 | 49 | } |
48 | 50 |
|
|
53 | 55 |
|
54 | 56 | $result = GraphQL::executeQuery( |
55 | 57 | $schema, |
56 | | - $data['query'], |
| 58 | + $data['query'], // @phpstan-ignore argument.type |
57 | 59 | null, |
58 | 60 | $appContext, |
59 | | - array_key_exists('variables', $data) ? $data['variables'] : [] |
| 61 | + array_key_exists('variables', $data) ? $data['variables'] : [] // @phpstan-ignore argument.type |
60 | 62 | ); |
61 | 63 | $output = $result->toArray($debug); |
62 | 64 | $httpStatus = 200; |
63 | 65 | } catch (\Exception $error) { |
64 | 66 | $httpStatus = 500; |
65 | | - $output['errors'] = [ |
| 67 | + $errors = [ |
66 | 68 | FormattedError::createFromException($error, $debug) |
67 | 69 | ]; |
| 70 | + |
| 71 | + if (isset($output) && is_array($output)) { |
| 72 | + $output['errors'] = $errors; |
| 73 | + } else { |
| 74 | + $output = [ |
| 75 | + 'errors' => $errors |
| 76 | + ]; |
| 77 | + } |
68 | 78 | } |
69 | 79 |
|
70 | 80 | header('Content-Type: application/json', true, $httpStatus); |
|
0 commit comments