mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-03-11 23:36:14 +03:00
Server: Extracted method for emitting response
This commit is contained in:
parent
8098b2b886
commit
71343f2f62
@ -401,8 +401,17 @@ class Helper
|
||||
private function doSendResponse($result, $exitWhenDone)
|
||||
{
|
||||
$httpStatus = $this->resolveHttpStatus($result);
|
||||
$body = json_encode($result);
|
||||
$this->emitResponse($result, $httpStatus, $exitWhenDone);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|\JsonSerializable $jsonSerializable
|
||||
* @param int $httpStatus
|
||||
* @param bool $exitWhenDone
|
||||
*/
|
||||
public function emitResponse($jsonSerializable, $httpStatus, $exitWhenDone)
|
||||
{
|
||||
$body = json_encode($jsonSerializable);
|
||||
header('Content-Type: application/json', true, $httpStatus);
|
||||
echo $body;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace GraphQL\Server;
|
||||
|
||||
use GraphQL\Error\FormattedError;
|
||||
use GraphQL\Error\InvariantViolation;
|
||||
use GraphQL\Executor\ExecutionResult;
|
||||
use GraphQL\Executor\Promise\Promise;
|
||||
@ -43,6 +44,27 @@ class StandardServer
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
/**
|
||||
* Converts and exception to error and sends spec-compliant HTTP 500 error.
|
||||
* Useful when an exception is thrown somewhere outside of server execution context
|
||||
* (e.g. during schema instantiation).
|
||||
*
|
||||
* @api
|
||||
* @param \Throwable $error
|
||||
* @param bool $debug
|
||||
* @param bool $exitWhenDone
|
||||
*/
|
||||
public static function send500Error($error, $debug = false, $exitWhenDone = false)
|
||||
{
|
||||
$response = [
|
||||
'errors' => [
|
||||
FormattedError::createFromException($error, $debug)
|
||||
]
|
||||
];
|
||||
$helper = new Helper();
|
||||
$helper->emitResponse($response, 500, $exitWhenDone);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new instance of a standard GraphQL HTTP server
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user