Remove redundant output redirection
This commit is contained in:
parent
4d60904cff
commit
8a9a5bec1e
1 changed files with 10 additions and 41 deletions
|
@ -708,25 +708,15 @@ class Builder
|
||||||
callable $resolve,
|
callable $resolve,
|
||||||
callable $reject,
|
callable $reject,
|
||||||
): void {
|
): void {
|
||||||
$request = $utopia->getResource('request');
|
|
||||||
$response = $utopia->getResource('response');
|
|
||||||
$swoole = $request->getSwoole();
|
|
||||||
|
|
||||||
// Drop json content type so post args are used directly
|
// Drop json content type so post args are used directly
|
||||||
if (
|
if ($request->getHeader('content-type') === 'application/json') {
|
||||||
\array_key_exists('content-type', $swoole->header)
|
unset($request->getSwoole()->header['content-type']);
|
||||||
&& $swoole->header['content-type'] === 'application/json'
|
|
||||||
) {
|
|
||||||
unset($swoole->header['content-type']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$gqlResponse = $response;
|
$request = new Request($request->getSwoole());
|
||||||
$request = new Request($swoole);
|
|
||||||
$apiResponse = new Response($response->getSwoole());
|
|
||||||
$apiResponse->setContentType(Response::CONTENT_TYPE_NULL);
|
|
||||||
|
|
||||||
$utopia->setResource('request', fn() => $request);
|
$utopia->setResource('request', fn() => $request);
|
||||||
$utopia->setResource('response', fn() => $apiResponse);
|
|
||||||
|
$response->setContentType(Response::CONTENT_TYPE_NULL);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Set route to null so match doesn't early return the GraphQL route
|
// Set route to null so match doesn't early return the GraphQL route
|
||||||
|
@ -735,45 +725,24 @@ class Builder
|
||||||
|
|
||||||
$utopia->execute($route, $request);
|
$utopia->execute($route, $request);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
self::reassign($gqlResponse, $apiResponse);
|
|
||||||
$reject($e);
|
$reject($e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self::reassign($gqlResponse, $apiResponse);
|
$payload = $response->getPayload();
|
||||||
|
|
||||||
$payload = $apiResponse->getPayload();
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 400) {
|
||||||
|
$reject(new GQLException($payload['message'], $response->getStatusCode()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (\array_key_exists('$id', $payload)) {
|
if (\array_key_exists('$id', $payload)) {
|
||||||
$payload['_id'] = $payload['$id'];
|
$payload['_id'] = $payload['$id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($apiResponse->getStatusCode() < 200 || $apiResponse->getStatusCode() >= 400) {
|
|
||||||
$reject(new GQLException($payload['message'], $apiResponse->getStatusCode()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$resolve($payload);
|
$resolve($payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Response $gqlResponse
|
|
||||||
* @param Response $apiResponse
|
|
||||||
* @return void
|
|
||||||
* @throws \Utopia\Exception
|
|
||||||
*/
|
|
||||||
private static function reassign(Response $gqlResponse, Response $apiResponse): void
|
|
||||||
{
|
|
||||||
$gqlResponse->setContentType($apiResponse->getContentType());
|
|
||||||
$gqlResponse->setStatusCode($apiResponse->getStatusCode());
|
|
||||||
foreach ($apiResponse->getHeaders() as $key => $value) {
|
|
||||||
$gqlResponse->addHeader($key, $value);
|
|
||||||
}
|
|
||||||
foreach ($apiResponse->getCookies() as $name => $cookie) {
|
|
||||||
$gqlResponse->addCookie($name, $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue