diff --git a/app/controllers/api/graphql.php b/app/controllers/api/graphql.php index ba801a86e..7ccb462e6 100644 --- a/app/controllers/api/graphql.php +++ b/app/controllers/api/graphql.php @@ -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) { diff --git a/app/init.php b/app/init.php index 9b6a54c8a..659d128e1 100644 --- a/app/init.php +++ b/app/init.php @@ -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; }); diff --git a/docker-compose.yml b/docker-compose.yml index e73ef3826..5c55a6682 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/src/Appwrite/GraphQL/Builder.php b/src/Appwrite/GraphQL/Builder.php index 4c1008a8f..54b5c5128 100644 --- a/src/Appwrite/GraphQL/Builder.php +++ b/src/Appwrite/GraphQL/Builder.php @@ -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);