1
0
Fork 0
mirror of synced 2024-07-01 04:30:59 +12:00

feat: added getCurrentModel()

This commit is contained in:
Christy Jacob 2021-03-18 02:15:47 +05:30
parent bf57280503
commit 99ade8ec13
5 changed files with 69 additions and 47 deletions

View file

@ -27,30 +27,15 @@ App::post('/v1/graphql')
$myErrorFormatter = function(Error $error) {
$formattedError = FormattedError::createFromException($error);
// var_dump("***** IN ERROR FORMATTER ******");
// var_dump("{$error->getMessage()}");
// var_dump("{$error->getCode()}");
// var_dump("{$error->getFile()}");
// var_dump("{$error->getLine()}");
// var_dump("{$error->getTrace()}");
$formattedError['code'] = $error->getCode();
$formattedError['file'] = $error->getFile();
$formattedError['line'] = $error->getLine();
// $formattedError['trace'] = $error->getTrace();
var_dump("***** IN ERROR FORMATTER ******");
$parentError = $error->getPrevious();
$formattedError['code'] = $parentError->getCode();
$formattedError['file'] = $parentError->getFile();
$formattedError['version'] = App::getEnv('_APP_VERSION', 'UNKNOWN');
$formattedError['line'] = $parentError->getLine();
return $formattedError;
};
$myErrorHandler = function(array $errors, callable $formatter) {
// $errors = array_map( function ($error) {
// unset($error['trace']);
// },$errors);
// var_dump("**** In My Error Handler *****");
// var_dump($errors);
return array_map($formatter, $errors);
};
$query = $request->getPayload('query', '');
$variables = $request->getPayload('variables', null);
$response->setContentType(Response::CONTENT_TYPE_NULL);
@ -63,8 +48,10 @@ App::post('/v1/graphql')
try {
$debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE;
// $debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS;
$rootValue = [];
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables)->setErrorFormatter($myErrorFormatter)->setErrorsHandler($myErrorHandler);
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables)
->setErrorFormatter($myErrorFormatter);
$output = $result->toArray($debug);
} catch (\Exception $error) {
$output = [
@ -80,5 +67,5 @@ App::post('/v1/graphql')
];
}
$response->json($output);
}
);
}
);

View file

@ -203,21 +203,6 @@ class Builder {
return $args;
}
/**
* Function to check if a model
*
* @param string $a
* @return void
*/
protected static function isModel($response, Model $model): bool {
foreach ($model->getRules() as $key => $rule) {
if (!isset($response[$key])) {
return false;
}
}
return true;
}
/**
* This function goes through all the REST endpoints in the API and builds a
@ -246,29 +231,26 @@ class Builder {
$type = self::getTypeMapping($responseModel, $response);
$description = $route->getDesc();
$args = self::getArgs($route->getParams(), $utopia);
$resolve = function ($type, $args, $context, $info) use (&$register, $route) {
$utopia = $register->get('__app');
$utopia->setRoute($route)
->execute($route, $args);
$utopia->setRoute($route)->execute($route, $args);
$response = $register->get('__response');
$result = $response->getPayload();
if (self::isModel($result, $response->getModel(Response::MODEL_ERROR)) || self::isModel($result, $response->getModel(Response::MODEL_ERROR_DEV))) {
if ( $response->getCurrentModel() == Response::MODEL_ERROR_DEV ) {
var_dump("***** There has been an dev exception.. *****");
throw new ExceptionDev($result['message'], $result['code'], $result['version'], $result['file'], $result['line'], $result['trace']);
} else if ( $response->getCurrentModel() == Response::MODEL_ERROR ) {
var_dump("***** There has been an exception.. *****");
unset($result['trace']);
// var_dump($result);
throw new Exception($result['message'], $result['code']);
}
return $result;
};
$field = [
'type' => $type,
'description' => $description,
'args' => $args,
'resolve' => $resolve
];
if ($method == 'GET') {
$queryFields[$methodName] = $field;
} else if ($method == 'POST' || $method == 'PUT' || $method == 'PATCH' || $method == 'DELETE') {

View file

@ -6,6 +6,13 @@ use GraphQL\Error\ClientAware;
class Exception extends \Exception implements ClientAware
{
function __construct(string $message = '', int $code = 0, string $version = '') {
$this->message = $message;
$this->code = $code;
$this->version = $version;
}
public function isClientSafe()
{
return true;

View file

@ -0,0 +1,28 @@
<?php
namespace Appwrite\GraphQL;
use GraphQL\Error\ClientAware;
class ExceptionDev extends \Exception implements ClientAware
{
function __construct(string $message = '', int $code = 0, string $version = '', string $file = '', int $line = -1, array $trace = []) {
$this->message = $message;
$this->code = $code;
$this->version = $version;
$this->file = $file;
$this->line = $line;
$this->trace = $trace;
}
public function isClientSafe()
{
return true;
}
public function getCategory()
{
return 'Appwrite Server Exception';
}
}

View file

@ -133,6 +133,11 @@ class Response extends SwooleResponse
*/
protected $payload = [];
/**
* @var string
*/
protected $model = '';
/**
* Response constructor.
*
@ -251,6 +256,17 @@ class Response extends SwooleResponse
return $this->models;
}
/**
* Returns the model that was used to
* parse the currrent payload
*
* @return string
*/
public function getCurrentModel(): string
{
return $this->model;
}
/**
* Validate response objects and outputs
* the response according to given format type
@ -301,6 +317,8 @@ class Response extends SwooleResponse
$model = $this->getModel($model);
$output = [];
$this->model = $model->getType();
if ($model->isAny()) {
$this->payload = $document->getArrayCopy();
return $this->payload;