2021-03-10 07:42:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\GraphQL;
|
|
|
|
|
|
|
|
|
|
use Appwrite\GraphQL\Types\JsonType;
|
|
|
|
|
use Appwrite\Utopia\Response;
|
|
|
|
|
use Appwrite\Utopia\Response\Model;
|
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
|
use GraphQL\Type\Schema;
|
2021-03-12 19:00:43 +00:00
|
|
|
use Appwrite\GraphQL\Exception;
|
2021-03-18 18:55:43 +00:00
|
|
|
use GraphQL\Error\Error;
|
|
|
|
|
use GraphQL\Error\FormattedError;
|
2021-03-18 19:26:45 +00:00
|
|
|
use Utopia\CLI\Console;
|
2021-03-10 07:42:45 +00:00
|
|
|
|
2021-03-11 15:44:04 +00:00
|
|
|
class Builder {
|
2021-03-10 07:42:45 +00:00
|
|
|
|
2021-03-12 18:00:41 +00:00
|
|
|
/** @var JsonType $jsonParser */
|
|
|
|
|
protected static $jsonParser = null;
|
2021-03-10 07:42:45 +00:00
|
|
|
|
2021-03-12 18:00:41 +00:00
|
|
|
/** @var array $typeMapping */
|
|
|
|
|
protected static $typeMapping = null;
|
2021-03-10 07:42:45 +00:00
|
|
|
|
2021-03-12 18:00:41 +00:00
|
|
|
/**
|
|
|
|
|
* Function to initialise the typeMapping array with the base cases of the recursion
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-03-16 20:05:48 +00:00
|
|
|
public static function init()
|
2021-03-12 18:00:41 +00:00
|
|
|
{
|
2021-03-10 07:42:45 +00:00
|
|
|
self::$typeMapping = [
|
|
|
|
|
Model::TYPE_BOOLEAN => Type::boolean(),
|
|
|
|
|
Model::TYPE_STRING => Type::string(),
|
|
|
|
|
Model::TYPE_INTEGER => Type::int(),
|
|
|
|
|
Model::TYPE_FLOAT => Type::float(),
|
2021-03-12 18:00:41 +00:00
|
|
|
Model::TYPE_JSON => self::json(),
|
2021-03-17 21:17:50 +00:00
|
|
|
Response::MODEL_NONE => self::json(),
|
2021-03-12 18:00:41 +00:00
|
|
|
Response::MODEL_ANY => self::json(),
|
2021-03-10 07:42:45 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 18:00:41 +00:00
|
|
|
/**
|
|
|
|
|
* Function to create a singleton for $jsonParser
|
|
|
|
|
*
|
|
|
|
|
* @return JsonType
|
|
|
|
|
*/
|
2021-03-16 20:05:48 +00:00
|
|
|
public static function json()
|
2021-03-12 18:00:41 +00:00
|
|
|
{
|
2021-03-16 20:05:48 +00:00
|
|
|
if (is_null(self::$jsonParser)) {
|
2021-03-12 18:00:41 +00:00
|
|
|
self::$jsonParser = new JsonType();
|
|
|
|
|
}
|
|
|
|
|
return self::$jsonParser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the map already contains the type, end the recursion and return.
|
|
|
|
|
* Iterate through all the rules in the response model. Each rule is of the form
|
|
|
|
|
* [
|
|
|
|
|
* [KEY 1] => [
|
|
|
|
|
* 'type' => A string from Appwrite/Utopia/Response
|
|
|
|
|
* 'description' => A description of the type
|
|
|
|
|
* 'default' => A default value for this type
|
|
|
|
|
* 'example' => An example of this type
|
|
|
|
|
* 'require' => a boolean representing whether this field is required
|
|
|
|
|
* 'array' => a boolean representing whether this field is an array
|
|
|
|
|
* ],
|
|
|
|
|
* [KEY 2] => [
|
|
|
|
|
* ],
|
|
|
|
|
* [KEY 3] => [
|
|
|
|
|
* ] .....
|
|
|
|
|
* ]
|
|
|
|
|
* If there are any field names containing characters other than a-z, A-Z, 0-9, _ ,
|
|
|
|
|
* we need to remove all those characters. Currently Appwrite's Response model has only the
|
2021-03-18 19:41:38 +00:00
|
|
|
* $ sign which is prohibited by the GraphQL spec. So we're only replacing that. We need to replace this with a regex
|
2021-03-12 18:00:41 +00:00
|
|
|
* based approach.
|
|
|
|
|
*
|
|
|
|
|
* @param Model $model
|
|
|
|
|
* @param Response $response
|
2021-03-16 20:05:48 +00:00
|
|
|
* @return Type
|
2021-03-12 18:00:41 +00:00
|
|
|
*/
|
2021-03-16 20:05:48 +00:00
|
|
|
static function getTypeMapping(Model $model, Response $response): Type
|
2021-03-12 18:00:41 +00:00
|
|
|
{
|
2021-03-16 20:05:48 +00:00
|
|
|
if (isset(self::$typeMapping[$model->getType()])) {
|
|
|
|
|
return self::$typeMapping[$model->getType()];
|
|
|
|
|
}
|
2021-03-10 07:42:45 +00:00
|
|
|
|
|
|
|
|
$rules = $model->getRules();
|
|
|
|
|
$name = $model->getType();
|
|
|
|
|
$fields = [];
|
|
|
|
|
$type = null;
|
|
|
|
|
foreach ($rules as $key => $props) {
|
|
|
|
|
$keyWithoutSpecialChars = str_replace('$', '', $key);
|
|
|
|
|
if (isset(self::$typeMapping[$props['type']])) {
|
|
|
|
|
$type = self::$typeMapping[$props['type']];
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
$complexModel = $response->getModel($props['type']);
|
2021-03-16 20:05:48 +00:00
|
|
|
$type = self::getTypeMapping($complexModel, $response);
|
2021-03-10 07:42:45 +00:00
|
|
|
} catch (Exception $e) {
|
2021-03-18 19:41:38 +00:00
|
|
|
Console::error("Could Not find model for : {$props['type']}");
|
2021-03-10 07:42:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($props['array']) {
|
|
|
|
|
$type = Type::listOf($type);
|
|
|
|
|
}
|
|
|
|
|
$fields[$keyWithoutSpecialChars] = [
|
|
|
|
|
'type' => $type,
|
|
|
|
|
'description' => $props['description'],
|
2021-03-11 15:44:04 +00:00
|
|
|
'resolve' => function ($object, $args, $context, $info) use ($key) {
|
2021-03-10 07:42:45 +00:00
|
|
|
return $object[$key];
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
$objectType = [
|
|
|
|
|
'name' => $name,
|
|
|
|
|
'fields' => $fields
|
|
|
|
|
];
|
2021-03-16 20:05:48 +00:00
|
|
|
self::$typeMapping[$name] = new ObjectType($objectType);
|
|
|
|
|
return self::$typeMapping[$name];
|
2021-03-10 07:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 19:41:38 +00:00
|
|
|
/**
|
|
|
|
|
* Function to map a Utopia\Validator to a valid GraphQL Type
|
2021-03-12 18:00:41 +00:00
|
|
|
*
|
|
|
|
|
* @param $validator
|
|
|
|
|
* @param bool $required
|
|
|
|
|
* @param $utopia
|
2021-03-18 19:41:38 +00:00
|
|
|
* @param $injections
|
|
|
|
|
* @return GraphQL\Type\Definition\Type
|
2021-03-12 18:00:41 +00:00
|
|
|
*/
|
2021-03-18 19:41:38 +00:00
|
|
|
protected static function getArgType($validator, bool $required, $utopia, $injections): Type
|
|
|
|
|
{
|
2021-03-10 07:42:45 +00:00
|
|
|
$validator = (\is_callable($validator)) ? call_user_func_array($validator, $utopia->getResources($injections)) : $validator;
|
|
|
|
|
$type = [];
|
|
|
|
|
switch ((!empty($validator)) ? \get_class($validator) : '') {
|
|
|
|
|
case 'Utopia\Validator\Text':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\Boolean':
|
|
|
|
|
$type = Type::boolean();
|
|
|
|
|
break;
|
|
|
|
|
case 'Appwrite\Database\Validator\UID':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\Email':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\URL':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\JSON':
|
|
|
|
|
case 'Utopia\Validator\Mock':
|
|
|
|
|
case 'Utopia\Validator\Assoc':
|
2021-03-12 18:00:41 +00:00
|
|
|
$type = self::json();
|
2021-03-10 07:42:45 +00:00
|
|
|
break;
|
|
|
|
|
case 'Appwrite\Storage\Validator\File':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
case 'Utopia\Validator\ArrayList':
|
2021-03-12 18:00:41 +00:00
|
|
|
$type = Type::listOf(self::json());
|
2021-03-10 07:42:45 +00:00
|
|
|
break;
|
|
|
|
|
case 'Appwrite\Auth\Validator\Password':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\Range': /* @var $validator \Utopia\Validator\Range */
|
|
|
|
|
$type = Type::int();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\Numeric':
|
|
|
|
|
$type = Type::int();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\Length':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\Host':
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
case 'Utopia\Validator\WhiteList': /* @var $validator \Utopia\Validator\WhiteList */
|
|
|
|
|
$type = Type::string();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2021-03-16 13:34:11 +00:00
|
|
|
$type = self::json();
|
2021-03-10 07:42:45 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($required) {
|
|
|
|
|
$type = Type::nonNull($type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $type;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 19:48:19 +00:00
|
|
|
|
2021-03-12 18:00:41 +00:00
|
|
|
/**
|
2021-03-12 19:00:43 +00:00
|
|
|
* 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
|
2021-03-12 18:00:41 +00:00
|
|
|
*
|
2021-03-12 19:00:43 +00:00
|
|
|
* @param $utopia
|
|
|
|
|
* @param $response
|
|
|
|
|
* @param $register
|
|
|
|
|
* @return Schema
|
2021-03-12 18:00:41 +00:00
|
|
|
*/
|
2021-03-10 15:22:19 +00:00
|
|
|
public static function buildSchema($utopia, $response, $register) {
|
2021-03-18 19:26:45 +00:00
|
|
|
Console::info("[INFO] Building GraphQL Schema...");
|
2021-03-10 07:42:45 +00:00
|
|
|
$start = microtime(true);
|
|
|
|
|
|
2021-03-12 18:09:25 +00:00
|
|
|
self::init();
|
2021-03-10 07:42:45 +00:00
|
|
|
$queryFields = [];
|
|
|
|
|
$mutationFields = [];
|
2021-03-12 18:09:25 +00:00
|
|
|
|
2021-03-10 07:42:45 +00:00
|
|
|
foreach($utopia->getRoutes() as $method => $routes ){
|
|
|
|
|
foreach($routes as $route) {
|
2021-03-18 19:26:45 +00:00
|
|
|
|
2021-03-10 07:42:45 +00:00
|
|
|
$namespace = $route->getLabel('sdk.namespace', '');
|
2021-03-12 18:09:25 +00:00
|
|
|
$methodName = $namespace.'_'.$route->getLabel('sdk.method', '');
|
|
|
|
|
$responseModelName = $route->getLabel('sdk.response.model', "");
|
2021-03-18 19:26:45 +00:00
|
|
|
|
2021-03-17 21:17:50 +00:00
|
|
|
if ( $responseModelName !== "" ) {
|
2021-03-12 18:09:25 +00:00
|
|
|
$responseModel = $response->getModel($responseModelName);
|
2021-03-18 19:26:45 +00:00
|
|
|
|
2021-03-18 19:48:19 +00:00
|
|
|
/* Create a GraphQL type for the current response model */
|
2021-03-16 20:05:48 +00:00
|
|
|
$type = self::getTypeMapping($responseModel, $response);
|
2021-03-18 19:48:19 +00:00
|
|
|
/* Get a description for this type */
|
2021-03-16 14:34:43 +00:00
|
|
|
$description = $route->getDesc();
|
2021-03-18 19:48:19 +00:00
|
|
|
/* Create the args required for this type */
|
|
|
|
|
$args = [];
|
|
|
|
|
foreach ($route->getParams() as $key => $value) {
|
|
|
|
|
$args[$key] = [
|
|
|
|
|
'type' => self::getArgType($value['validator'],!$value['optional'], $utopia, $value['injections']),
|
|
|
|
|
'description' => $value['description'],
|
|
|
|
|
'defaultValue' => $value['default']
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
/* Define a resolve function that defines how to fetch data for this type */
|
2021-03-16 14:34:43 +00:00
|
|
|
$resolve = function ($type, $args, $context, $info) use (&$register, $route) {
|
|
|
|
|
$utopia = $register->get('__app');
|
2021-03-17 20:45:47 +00:00
|
|
|
$utopia->setRoute($route)->execute($route, $args);
|
2021-03-16 14:34:43 +00:00
|
|
|
$response = $register->get('__response');
|
|
|
|
|
$result = $response->getPayload();
|
2021-03-17 20:45:47 +00:00
|
|
|
if ( $response->getCurrentModel() == Response::MODEL_ERROR_DEV ) {
|
|
|
|
|
throw new ExceptionDev($result['message'], $result['code'], $result['version'], $result['file'], $result['line'], $result['trace']);
|
|
|
|
|
} else if ( $response->getCurrentModel() == Response::MODEL_ERROR ) {
|
2021-03-16 14:34:43 +00:00
|
|
|
throw new Exception($result['message'], $result['code']);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
};
|
2021-03-18 19:26:45 +00:00
|
|
|
|
2021-03-12 18:09:25 +00:00
|
|
|
$field = [
|
|
|
|
|
'type' => $type,
|
2021-03-16 14:34:43 +00:00
|
|
|
'description' => $description,
|
2021-03-12 18:09:25 +00:00
|
|
|
'args' => $args,
|
2021-03-16 14:34:43 +00:00
|
|
|
'resolve' => $resolve
|
2021-03-12 18:09:25 +00:00
|
|
|
];
|
2021-03-18 19:26:45 +00:00
|
|
|
|
2021-03-12 18:09:25 +00:00
|
|
|
if ($method == 'GET') {
|
|
|
|
|
$queryFields[$methodName] = $field;
|
|
|
|
|
} else if ($method == 'POST' || $method == 'PUT' || $method == 'PATCH' || $method == 'DELETE') {
|
|
|
|
|
$mutationFields[$methodName] = $field;
|
2021-03-10 07:42:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-12 18:09:25 +00:00
|
|
|
|
2021-03-10 07:42:45 +00:00
|
|
|
ksort($queryFields);
|
|
|
|
|
ksort($mutationFields);
|
2021-03-18 19:26:45 +00:00
|
|
|
|
2021-03-10 07:42:45 +00:00
|
|
|
$queryType = new ObjectType([
|
|
|
|
|
'name' => 'Query',
|
|
|
|
|
'description' => 'The root of all your queries',
|
|
|
|
|
'fields' => $queryFields
|
|
|
|
|
]);
|
|
|
|
|
$mutationType = new ObjectType([
|
|
|
|
|
'name' => 'Mutation',
|
|
|
|
|
'description' => 'The root of all your mutations',
|
|
|
|
|
'fields' => $mutationFields
|
|
|
|
|
]);
|
|
|
|
|
$schema = new Schema([
|
|
|
|
|
'query' => $queryType,
|
|
|
|
|
'mutation' => $mutationType
|
|
|
|
|
]);
|
2021-03-12 18:09:25 +00:00
|
|
|
|
2021-03-10 07:42:45 +00:00
|
|
|
$time_elapsed_secs = microtime(true) - $start;
|
2021-03-18 19:26:45 +00:00
|
|
|
Console::info("[INFO] Time Taken To Build Schema : ${time_elapsed_secs}s");
|
2021-03-12 19:00:43 +00:00
|
|
|
|
2021-03-10 07:42:45 +00:00
|
|
|
return $schema;
|
|
|
|
|
}
|
2021-03-18 18:55:43 +00:00
|
|
|
|
2021-03-18 19:26:45 +00:00
|
|
|
public static function getErrorFormatter(bool $isDevelopment, string $version): callable
|
|
|
|
|
{
|
|
|
|
|
$errorFormatter = function(Error $error) use ($isDevelopment, $version) {
|
|
|
|
|
$formattedError = FormattedError::createFromException($error);
|
|
|
|
|
$parentError = $error->getPrevious();
|
|
|
|
|
$formattedError['code'] = $parentError->getCode();
|
|
|
|
|
$formattedError['version'] = $version;
|
|
|
|
|
if($isDevelopment) {
|
|
|
|
|
$formattedError['file'] = $parentError->getFile();
|
|
|
|
|
$formattedError['line'] = $parentError->getLine();
|
|
|
|
|
}
|
|
|
|
|
return $formattedError;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return $errorFormatter;
|
2021-03-18 18:55:43 +00:00
|
|
|
}
|
2021-03-10 07:42:45 +00:00
|
|
|
}
|