WIP schema extension POC
This commit is contained in:
parent
a50f099ba3
commit
82f368f0d9
4 changed files with 53 additions and 7 deletions
|
@ -3,11 +3,12 @@
|
||||||
use Appwrite\GraphQL\Builder;
|
use Appwrite\GraphQL\Builder;
|
||||||
use GraphQL\GraphQL;
|
use GraphQL\GraphQL;
|
||||||
use GraphQL\Type;
|
use GraphQL\Type;
|
||||||
|
use GraphQL\Type\Definition\ObjectType;
|
||||||
|
use GraphQL\Utils\SchemaExtender;
|
||||||
use Appwrite\Utopia\Response;
|
use Appwrite\Utopia\Response;
|
||||||
use GraphQL\Error\DebugFlag;
|
use GraphQL\Error\DebugFlag;
|
||||||
use Utopia\App;
|
use Utopia\App;
|
||||||
|
|
||||||
|
|
||||||
App::post('/v1/graphql')
|
App::post('/v1/graphql')
|
||||||
->desc('GraphQL Endpoint')
|
->desc('GraphQL Endpoint')
|
||||||
->label('scope', 'graphql')
|
->label('scope', 'graphql')
|
||||||
|
@ -24,6 +25,27 @@ App::post('/v1/graphql')
|
||||||
/** @var Utopia\App $utopia */
|
/** @var Utopia\App $utopia */
|
||||||
/** @var Utopia\Registry\Registry $register */
|
/** @var Utopia\Registry\Registry $register */
|
||||||
|
|
||||||
|
$queryType = new ObjectType([
|
||||||
|
'name' => 'Query',
|
||||||
|
'description' => 'The root of all your queries',
|
||||||
|
'fields' => [
|
||||||
|
'accountGet' => [
|
||||||
|
'type' => Type\Definition\Type::string(),
|
||||||
|
'description' => 'Extension description',
|
||||||
|
'args' => [],
|
||||||
|
'resolve' => fn() => "Replacing account get response"
|
||||||
|
],
|
||||||
|
'testQuery' => [
|
||||||
|
'type' => Type\Definition\Type::string(),
|
||||||
|
'description' => 'Extension description 2',
|
||||||
|
'args' => [],
|
||||||
|
'resolve' => fn() => "Test query response"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
$extendedSchema = SchemaExtender::extend($schema, $queryType->astNode);
|
||||||
|
|
||||||
$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);
|
||||||
|
@ -40,7 +62,7 @@ App::post('/v1/graphql')
|
||||||
try {
|
try {
|
||||||
$debug = $isDevelopment ? ( DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE ) : DebugFlag::NONE;
|
$debug = $isDevelopment ? ( DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE ) : DebugFlag::NONE;
|
||||||
$rootValue = [];
|
$rootValue = [];
|
||||||
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables)
|
$result = GraphQL::executeQuery($extendedSchema, $query, $rootValue, null, $variables)
|
||||||
->setErrorFormatter(Builder::getErrorFormatter($isDevelopment, $version));
|
->setErrorFormatter(Builder::getErrorFormatter($isDevelopment, $version));
|
||||||
$output = $result->toArray($debug);
|
$output = $result->toArray($debug);
|
||||||
} catch (\Exception $error) {
|
} catch (\Exception $error) {
|
||||||
|
|
|
@ -559,7 +559,7 @@ App::setResource('schema', function($utopia, $response, $request, $register) {
|
||||||
$schema = $register->get('_schema');
|
$schema = $register->get('_schema');
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Console::error('Schema not present. Generating Schema...');
|
Console::error('Schema not present. Generating Schema...');
|
||||||
$schema = Builder::buildSchema($utopia, $response, $register);
|
$schema = Builder::buildModelSchema($utopia, $response, $register);
|
||||||
$register->set('_schema', function () use ($schema){
|
$register->set('_schema', function () use ($schema){
|
||||||
return $schema;
|
return $schema;
|
||||||
});
|
});
|
||||||
|
|
|
@ -585,7 +585,7 @@ services:
|
||||||
|
|
||||||
graphql-explorer:
|
graphql-explorer:
|
||||||
container_name: graphql-explorer
|
container_name: graphql-explorer
|
||||||
image: appwrite/docker-altair
|
image: appwrite/altair:0.1.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
- appwrite
|
- appwrite
|
||||||
|
|
|
@ -188,6 +188,30 @@ class Builder {
|
||||||
return $type;
|
return $type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 $utopia
|
||||||
|
* @param $response
|
||||||
|
* @param $register
|
||||||
|
* @return Schema
|
||||||
|
*/
|
||||||
|
public static function buildDatabaseSchema($utopia, $response, $register)
|
||||||
|
{
|
||||||
|
/** @var Model\Collection[] $collections */
|
||||||
|
|
||||||
|
Console::log("[INFO] Building GraphQL Database Schema...");
|
||||||
|
$start = microtime(true);
|
||||||
|
$collections = [];
|
||||||
|
|
||||||
|
foreach($collections as $collection) {
|
||||||
|
foreach ($collection->getRules() as $rule) {
|
||||||
|
/** @var Model\Rule $rule */
|
||||||
|
$modelName = $rule->getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
|
@ -198,7 +222,7 @@ class Builder {
|
||||||
* @param $register
|
* @param $register
|
||||||
* @return Schema
|
* @return Schema
|
||||||
*/
|
*/
|
||||||
public static function buildSchema($utopia, $response, $register) {
|
public static function buildModelSchema($utopia, $response, $register) {
|
||||||
Console::log("[INFO] Building GraphQL Schema...");
|
Console::log("[INFO] Building GraphQL Schema...");
|
||||||
$start = microtime(true);
|
$start = microtime(true);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue