-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Custom `GraphQlView` that converts any exceptions hiding the original exception message and adding some useful details like `_statusCode` and `_referenceCode`. Note: The underscores indicate that those properties are not part of the official GraphQL specification (see http://facebook.github.io/graphql/#sec-Errors)
- Loading branch information
1 parent
67f5db7
commit 2c17334
Showing
2 changed files
with
79 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
namespace Wwwision\GraphQL\View; | ||
|
||
use GraphQL\Error; | ||
use GraphQL\Executor\ExecutionResult; | ||
use TYPO3\Flow\Annotations as Flow; | ||
use TYPO3\Flow\Http\Response as HttpResponse; | ||
use TYPO3\Flow\Mvc\View\AbstractView; | ||
use TYPO3\Flow\Exception as FlowException; | ||
|
||
class GraphQlView extends AbstractView | ||
{ | ||
|
||
/** | ||
* @return string The rendered view | ||
* @throws FlowException | ||
*/ | ||
public function render() | ||
{ | ||
if (!isset($this->variables['result'])) { | ||
throw new FlowException(sprintf('The GraphQlView expects a variable "result" of type "%s", non given!', ExecutionResult::class), 1469545196); | ||
} | ||
$result = $this->variables['result']; | ||
if (!$result instanceof ExecutionResult) { | ||
throw new FlowException(sprintf('The GraphQlView expects a variable "result" of type "%s", "%s" given!', ExecutionResult::class, is_object($result) ? get_class($result) : gettype($result)), 1469545198); | ||
} | ||
|
||
/** @var HttpResponse $response */ | ||
$response = $this->controllerContext->getResponse(); | ||
$response->setHeader('Content-Type', 'application/json'); | ||
|
||
return json_encode($this->formatResult($result)); | ||
} | ||
|
||
/** | ||
* Formats the result of the GraphQL execution, converting Flow exceptions by hiding the original exception message | ||
* and adding status- and referenceCode. | ||
* | ||
* @param ExecutionResult $executionResult | ||
* @return array | ||
*/ | ||
private function formatResult(ExecutionResult $executionResult) | ||
{ | ||
$convertedResult = [ | ||
'data' => $executionResult->data, | ||
]; | ||
if (!empty($executionResult->errors)) { | ||
$convertedResult['errors'] = array_map(function(Error $error) { | ||
$errorResult = [ | ||
'message' => $error->message, | ||
'locations' => $error->getLocations() | ||
]; | ||
$exception = $error->getPrevious(); | ||
if ($exception instanceof FlowException) { | ||
$errorResult['message'] = HttpResponse::getStatusMessageByCode($exception->getStatusCode()); | ||
$errorResult['_statusCode'] = $exception->getStatusCode(); | ||
$errorResult['_referenceCode'] = $exception->getReferenceCode(); | ||
} | ||
return $errorResult; | ||
}, $executionResult->errors); | ||
} | ||
if (!empty($executionResult->extensions)) { | ||
$convertedResult['extensions'] = (array)$executionResult->extensions; | ||
} | ||
return $convertedResult; | ||
} | ||
} |