From 96bb4613e4b0e8d5c992e2cacd78100f2be654f3 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sat, 13 Mar 2021 00:30:43 +0530 Subject: [PATCH] feat: code cleanup --- app/controllers/api/graphql.php | 49 ++++-------------------------- app/controllers/api/locale.php | 1 - app/controllers/api/users.php | 2 -- src/Appwrite/GraphQL/Builder.php | 22 +++++++------- src/Appwrite/GraphQL/Exception.php | 18 +++++++++++ 5 files changed, 35 insertions(+), 57 deletions(-) create mode 100644 src/Appwrite/GraphQL/Exception.php diff --git a/app/controllers/api/graphql.php b/app/controllers/api/graphql.php index c31a61bd0f..335546e0be 100644 --- a/app/controllers/api/graphql.php +++ b/app/controllers/api/graphql.php @@ -1,53 +1,12 @@ 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); diff --git a/app/controllers/api/locale.php b/app/controllers/api/locale.php index c6f720bdc0..c145f81a85 100644 --- a/app/controllers/api/locale.php +++ b/app/controllers/api/locale.php @@ -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); diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 7354b1bb2c..eb11899ede 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -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, diff --git a/src/Appwrite/GraphQL/Builder.php b/src/Appwrite/GraphQL/Builder.php index a35aa8f74f..404def78a3 100644 --- a/src/Appwrite/GraphQL/Builder.php +++ b/src/Appwrite/GraphQL/Builder.php @@ -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; } } diff --git a/src/Appwrite/GraphQL/Exception.php b/src/Appwrite/GraphQL/Exception.php new file mode 100644 index 0000000000..75312d5512 --- /dev/null +++ b/src/Appwrite/GraphQL/Exception.php @@ -0,0 +1,18 @@ +