feat: code cleanup

This commit is contained in:
Christy Jacob
2021-03-13 00:30:43 +05:30
parent b1c58e2fc4
commit 96bb4613e4
5 changed files with 35 additions and 57 deletions
+6 -43
View File
@@ -1,53 +1,12 @@
<?php
use GraphQL\GraphQL;
use GraphQL\Type;
use Appwrite\Utopia\Response;
use GraphQL\Error\Error;
use GraphQL\Error\ClientAware;
use GraphQL\Error\FormattedError;
use Utopia\App;
use Utopia\Exception;
/**
* TODO:
* 1. Map all objects, object-params, object-fields
* 2. Parse GraphQL request payload (use: https://github.com/webonyx/graphql-php)
* 3. Route request to relevant controllers (of REST API?) / resolvers and aggergate data
* 4. Handle scope authentication
* 5. Handle errors
* 6. Return response
* 7. Write tests!
*
* Demo
* curl -H "Content-Type: application/json" http://localhost/v1/graphql -d '{"query": "query { echo(message: \"Hello World\") }" }'
*
* Explorers:
* - https://shopify.dev/tools/graphiql-admin-api
* - https://developer.github.com/v4/explorer/
* - http://localhost:4000
*
* Docs
* - Overview
* - Clients
*
* - Queries
* - Mutations
*
* - Objects
*/
class MySafeException extends Exception implements ClientAware
{
public function isClientSafe()
{
return true;
}
public function getCategory()
{
return 'Appwrite Server Error';
}
}
App::post('/v1/graphql')
->desc('GraphQL Endpoint')
@@ -59,6 +18,11 @@ App::post('/v1/graphql')
->inject('register')
->middleware(true)
->action(function ($request, $response, $schema, $utopia, $register) {
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Type\Schema $schema */
/** @var Utopia\App $utopia */
/** @var Utopia\Registry\Registry $register */
$myErrorFormatter = function(Error $error) {
$formattedError = FormattedError::createFromException($error);
@@ -86,7 +50,6 @@ App::post('/v1/graphql')
return array_map($formatter, $errors);
};
$query = $request->getPayload('query', '');
$variables = $request->getPayload('variables', null);
$response->setContentType(Response::CONTENT_TYPE_NULL);
-1
View File
@@ -189,7 +189,6 @@ App::get('/v1/locale/continents')
->action(function ($response, $locale) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Locale\Locale $locale */
$list = $locale->getText('continents'); /* @var $list array */
\asort($list);
-2
View File
@@ -98,8 +98,6 @@ App::get('/v1/users')
/** @var Appwrite\Utopia\Response $response */
/** @var Appwrite\Database\Database $projectDB */
var_dump("Running execute method for list users");
$results = $projectDB->getCollection([
'limit' => $limit,
'offset' => $offset,
+11 -11
View File
@@ -5,13 +5,10 @@ namespace Appwrite\GraphQL;
use Appwrite\GraphQL\Types\JsonType;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use MySafeException;
use Exception;
use Utopia\Validator;
use Appwrite\GraphQL\Exception;
class Builder {
@@ -223,10 +220,13 @@ class Builder {
}
/**
* Function to initialise the typeMapping array with the base cases of the recursion
* This function goes through all the REST endpoints in the API and builds a
* GraphQL schema for all those routes whose response model is neither empty nor NONE
*
* @param string $a
* @return void
* @param $utopia
* @param $response
* @param $register
* @return Schema
*/
public static function buildSchema($utopia, $response, $register) {
var_dump("[INFO] Building GraphQL Schema...");
@@ -253,17 +253,16 @@ class Builder {
'args' => $args,
'resolve' => function ($type, $args, $context, $info) use (&$register, $route) {
$utopia = $register->get('__app');
$utopia->setRoute($route)
->execute($route, $args);
$response = $register->get('__response');
$utopia->setRoute($route);
$utopia->execute($route, $args);
$result = $response->getPayload();
if (self::isModel($result, $response->getModel(Response::MODEL_ERROR)) || self::isModel($result, $response->getModel(Response::MODEL_ERROR_DEV))) {
var_dump("***** There has been an exception.. *****");
unset($result['trace']);
// var_dump($result);
throw new MySafeException($result['message'], $result['code']);
throw new Exception($result['message'], $result['code']);
}
return $result;
}
];
@@ -296,6 +295,7 @@ class Builder {
$time_elapsed_secs = microtime(true) - $start;
var_dump("[INFO] Time Taken To Build Schema : ${time_elapsed_secs}s");
return $schema;
}
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Appwrite\GraphQL;
use GraphQL\Error\ClientAware;
class Exception extends \Exception implements ClientAware
{
public function isClientSafe()
{
return true;
}
public function getCategory()
{
return 'Appwrite Server Exception';
}
}