feat: added getCurrentModel()
This commit is contained in:
parent
bf57280503
commit
99ade8ec13
|
@ -27,30 +27,15 @@ App::post('/v1/graphql')
|
||||||
|
|
||||||
$myErrorFormatter = function(Error $error) {
|
$myErrorFormatter = function(Error $error) {
|
||||||
$formattedError = FormattedError::createFromException($error);
|
$formattedError = FormattedError::createFromException($error);
|
||||||
// var_dump("***** IN ERROR FORMATTER ******");
|
var_dump("***** IN ERROR FORMATTER ******");
|
||||||
// var_dump("{$error->getMessage()}");
|
$parentError = $error->getPrevious();
|
||||||
// var_dump("{$error->getCode()}");
|
$formattedError['code'] = $parentError->getCode();
|
||||||
// var_dump("{$error->getFile()}");
|
$formattedError['file'] = $parentError->getFile();
|
||||||
// var_dump("{$error->getLine()}");
|
$formattedError['version'] = App::getEnv('_APP_VERSION', 'UNKNOWN');
|
||||||
// var_dump("{$error->getTrace()}");
|
$formattedError['line'] = $parentError->getLine();
|
||||||
|
|
||||||
$formattedError['code'] = $error->getCode();
|
|
||||||
$formattedError['file'] = $error->getFile();
|
|
||||||
$formattedError['line'] = $error->getLine();
|
|
||||||
// $formattedError['trace'] = $error->getTrace();
|
|
||||||
return $formattedError;
|
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', '');
|
$query = $request->getPayload('query', '');
|
||||||
$variables = $request->getPayload('variables', null);
|
$variables = $request->getPayload('variables', null);
|
||||||
$response->setContentType(Response::CONTENT_TYPE_NULL);
|
$response->setContentType(Response::CONTENT_TYPE_NULL);
|
||||||
|
@ -63,8 +48,10 @@ App::post('/v1/graphql')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE;
|
$debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE;
|
||||||
|
// $debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS;
|
||||||
$rootValue = [];
|
$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);
|
$output = $result->toArray($debug);
|
||||||
} catch (\Exception $error) {
|
} catch (\Exception $error) {
|
||||||
$output = [
|
$output = [
|
||||||
|
@ -80,5 +67,5 @@ App::post('/v1/graphql')
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$response->json($output);
|
$response->json($output);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -203,21 +203,6 @@ class Builder {
|
||||||
return $args;
|
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
|
* 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);
|
$type = self::getTypeMapping($responseModel, $response);
|
||||||
$description = $route->getDesc();
|
$description = $route->getDesc();
|
||||||
$args = self::getArgs($route->getParams(), $utopia);
|
$args = self::getArgs($route->getParams(), $utopia);
|
||||||
|
|
||||||
$resolve = function ($type, $args, $context, $info) use (&$register, $route) {
|
$resolve = function ($type, $args, $context, $info) use (&$register, $route) {
|
||||||
$utopia = $register->get('__app');
|
$utopia = $register->get('__app');
|
||||||
$utopia->setRoute($route)
|
$utopia->setRoute($route)->execute($route, $args);
|
||||||
->execute($route, $args);
|
|
||||||
$response = $register->get('__response');
|
$response = $register->get('__response');
|
||||||
$result = $response->getPayload();
|
$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.. *****");
|
var_dump("***** There has been an exception.. *****");
|
||||||
unset($result['trace']);
|
|
||||||
// var_dump($result);
|
|
||||||
throw new Exception($result['message'], $result['code']);
|
throw new Exception($result['message'], $result['code']);
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
};
|
};
|
||||||
|
|
||||||
$field = [
|
$field = [
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'args' => $args,
|
'args' => $args,
|
||||||
'resolve' => $resolve
|
'resolve' => $resolve
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($method == 'GET') {
|
if ($method == 'GET') {
|
||||||
$queryFields[$methodName] = $field;
|
$queryFields[$methodName] = $field;
|
||||||
} else if ($method == 'POST' || $method == 'PUT' || $method == 'PATCH' || $method == 'DELETE') {
|
} else if ($method == 'POST' || $method == 'PUT' || $method == 'PATCH' || $method == 'DELETE') {
|
||||||
|
|
|
@ -6,6 +6,13 @@ use GraphQL\Error\ClientAware;
|
||||||
|
|
||||||
class Exception extends \Exception implements 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()
|
public function isClientSafe()
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
|
|
28
src/Appwrite/GraphQL/ExceptionDev.php
Normal file
28
src/Appwrite/GraphQL/ExceptionDev.php
Normal 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';
|
||||||
|
}
|
||||||
|
}
|
|
@ -133,6 +133,11 @@ class Response extends SwooleResponse
|
||||||
*/
|
*/
|
||||||
protected $payload = [];
|
protected $payload = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $model = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response constructor.
|
* Response constructor.
|
||||||
*
|
*
|
||||||
|
@ -251,6 +256,17 @@ class Response extends SwooleResponse
|
||||||
return $this->models;
|
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
|
* Validate response objects and outputs
|
||||||
* the response according to given format type
|
* the response according to given format type
|
||||||
|
@ -301,6 +317,8 @@ class Response extends SwooleResponse
|
||||||
$model = $this->getModel($model);
|
$model = $this->getModel($model);
|
||||||
$output = [];
|
$output = [];
|
||||||
|
|
||||||
|
$this->model = $model->getType();
|
||||||
|
|
||||||
if ($model->isAny()) {
|
if ($model->isAny()) {
|
||||||
$this->payload = $document->getArrayCopy();
|
$this->payload = $document->getArrayCopy();
|
||||||
return $this->payload;
|
return $this->payload;
|
||||||
|
|
Loading…
Reference in a new issue