WIP schema extension POC
This commit is contained in:
parent
a50f099ba3
commit
82f368f0d9
|
@ -3,11 +3,12 @@
|
|||
use Appwrite\GraphQL\Builder;
|
||||
use GraphQL\GraphQL;
|
||||
use GraphQL\Type;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Utils\SchemaExtender;
|
||||
use Appwrite\Utopia\Response;
|
||||
use GraphQL\Error\DebugFlag;
|
||||
use Utopia\App;
|
||||
|
||||
|
||||
App::post('/v1/graphql')
|
||||
->desc('GraphQL Endpoint')
|
||||
->label('scope', 'graphql')
|
||||
|
@ -24,6 +25,27 @@ App::post('/v1/graphql')
|
|||
/** @var Utopia\App $utopia */
|
||||
/** @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', '');
|
||||
$variables = $request->getPayload('variables', null);
|
||||
$response->setContentType(Response::CONTENT_TYPE_NULL);
|
||||
|
@ -40,7 +62,7 @@ App::post('/v1/graphql')
|
|||
try {
|
||||
$debug = $isDevelopment ? ( DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE ) : DebugFlag::NONE;
|
||||
$rootValue = [];
|
||||
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables)
|
||||
$result = GraphQL::executeQuery($extendedSchema, $query, $rootValue, null, $variables)
|
||||
->setErrorFormatter(Builder::getErrorFormatter($isDevelopment, $version));
|
||||
$output = $result->toArray($debug);
|
||||
} catch (\Exception $error) {
|
||||
|
|
|
@ -559,7 +559,7 @@ App::setResource('schema', function($utopia, $response, $request, $register) {
|
|||
$schema = $register->get('_schema');
|
||||
} catch (Exception $e) {
|
||||
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){
|
||||
return $schema;
|
||||
});
|
||||
|
|
|
@ -585,11 +585,11 @@ services:
|
|||
|
||||
graphql-explorer:
|
||||
container_name: graphql-explorer
|
||||
image: appwrite/docker-altair
|
||||
image: appwrite/altair:0.1.0
|
||||
restart: unless-stopped
|
||||
networks:
|
||||
- appwrite
|
||||
ports:
|
||||
ports:
|
||||
- 9509:3000
|
||||
environment:
|
||||
- SERVER_URL=http://localhost/v1/graphql
|
||||
|
|
|
@ -187,7 +187,31 @@ class Builder {
|
|||
|
||||
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
|
||||
|
@ -198,7 +222,7 @@ class Builder {
|
|||
* @param $register
|
||||
* @return Schema
|
||||
*/
|
||||
public static function buildSchema($utopia, $response, $register) {
|
||||
public static function buildModelSchema($utopia, $response, $register) {
|
||||
Console::log("[INFO] Building GraphQL Schema...");
|
||||
$start = microtime(true);
|
||||
|
||||
|
|
Loading…
Reference in a new issue