From fb5d2fc25cc5f995183663d4e3d0310444685e80 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 17 Oct 2022 13:27:15 +1300 Subject: [PATCH] Abstract setting params to init --- app/init.php | 60 ++++++++++++++++++++----- src/Appwrite/GraphQL/Resolvers.php | 72 ++++++++++-------------------- src/Appwrite/GraphQL/Schema.php | 22 +++++++-- 3 files changed, 91 insertions(+), 63 deletions(-) diff --git a/app/init.php b/app/init.php index d8caa5f7b9..b2ff765526 100644 --- a/app/init.php +++ b/app/init.php @@ -1082,20 +1082,57 @@ App::setResource('schema', function ($utopia, $dbForProject) { }; $urls = [ - 'list' => function (string $collectionId, array $args) { - return "/v1/database/collections/{$collectionId}/documents"; + 'list' => function (string $databaseId, string $collectionId, array $args) { + return "/v1/databases/$databaseId/collections/$collectionId/documents"; }, - 'create' => function (string $collectionId, array $args) { - return "/v1/database/collections/{$collectionId}/documents"; + 'create' => function (string $databaseId, string $collectionId, array $args) { + return "/v1/databases/$databaseId/collections/$collectionId/documents"; }, - 'read' => function (string $collectionId, array $args) { - return "/v1/database/collections/{$collectionId}/documents/{$args['documentId']}"; + 'read' => function (string $databaseId, string $collectionId, array $args) { + return "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['documentId']}"; }, - 'update' => function (string $collectionId, array $args) { - return "/v1/database/collections/{$collectionId}/documents/{$args['documentId']}"; + 'update' => function (string $databaseId, string $collectionId, array $args) { + return "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['documentId']}"; }, - 'delete' => function (string $collectionId, array $args) { - return "/v1/database/collections/{$collectionId}/documents/{$args['documentId']}"; + 'delete' => function (string $databaseId, string $collectionId, array $args) { + return "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['documentId']}"; + }, + ]; + $params = [ + 'list' => function (string $databaseId, string $collectionId, array $args) { + return [ 'queries' => $args['queries']]; + }, + 'create' => function (string $databaseId, string $collectionId, array $args) { + $id = $args['id'] ?? 'unique()'; + $permissions = $args['permissions'] ?? null; + + unset($args['id']); + unset($args['permissions']); + + // Order must be the same as the route params + return [ + 'databaseId' => $databaseId, + 'documentId' => $id, + 'collectionId' => $collectionId, + 'data' => $args, + 'permissions' => $permissions, + ]; + }, + 'update' => function (string $databaseId, string $collectionId, array $args) { + $documentId = $args['id']; + $permissions = $args['permissions'] ?? null; + + unset($args['id']); + unset($args['permissions']); + + // Order must be the same as the route params + return [ + 'databaseId' => $databaseId, + 'collectionId' => $collectionId, + 'documentId' => $documentId, + 'data' => $args, + 'permissions' => $permissions, + ]; }, ]; @@ -1103,6 +1140,7 @@ App::setResource('schema', function ($utopia, $dbForProject) { $utopia, $complexity, $attributes, - $urls + $urls, + $params, ); }, ['utopia', 'dbForProject']); diff --git a/src/Appwrite/GraphQL/Resolvers.php b/src/Appwrite/GraphQL/Resolvers.php index 42d4e52738..b3461dd6a7 100644 --- a/src/Appwrite/GraphQL/Resolvers.php +++ b/src/Appwrite/GraphQL/Resolvers.php @@ -85,13 +85,14 @@ class Resolvers * @param App $utopia * @param string $databaseId * @param string $collectionId + * @param callable $url * @return callable */ public static function documentGet( App $utopia, string $databaseId, string $collectionId, - callable $url + callable $url, ): callable { return static fn($type, $args, $context, $info) => new Swoole( function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $type, $args) { @@ -100,7 +101,7 @@ class Resolvers $response = $utopia->getResource('response', true); $request->setMethod('GET'); - $request->setURI($url($collectionId, $args)); + $request->setURI($url($databaseId, $collectionId, $args)); self::resolve($utopia, $request, $response, $resolve, $reject); } @@ -113,25 +114,25 @@ class Resolvers * @param App $utopia * @param string $databaseId * @param string $collectionId + * @param callable $url * @return callable */ public static function documentList( App $utopia, string $databaseId, string $collectionId, - callable $url + callable $url, + callable $params, ): callable { return static fn($type, $args, $context, $info) => new Swoole( - function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $params, $type, $args) { $utopia = $utopia->getResource('utopia:graphql', true); $request = $utopia->getResource('request', true); $response = $utopia->getResource('response', true); $request->setMethod('GET'); - $request->setURI($url($collectionId, $args)); - $request->setGet([ - 'queries' => $args['queries'], - ]); + $request->setURI($url($databaseId, $collectionId, $args)); + $request->setGet($params($databaseId, $collectionId, $args)); $beforeResolve = function ($payload) { return $payload['documents']; @@ -148,37 +149,25 @@ class Resolvers * @param App $utopia * @param string $databaseId * @param string $collectionId + * @param callable $url * @return callable */ public static function documentCreate( App $utopia, string $databaseId, string $collectionId, - callable $url + callable $url, + callable $params, ): callable { return static fn($type, $args, $context, $info) => new Swoole( - function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $params, $type, $args) { $utopia = $utopia->getResource('utopia:graphql', true); $request = $utopia->getResource('request', true); $response = $utopia->getResource('response', true); - $id = $args['id'] ?? 'unique()'; - $permissions = $args['permissions'] ?? null; - - unset($args['id']); - unset($args['permissions']); - $request->setMethod('POST'); - $request->setURI($url($collectionId, $args)); - - // Order must be the same as the route params - $request->setPost([ - 'databaseId' => $databaseId, - 'documentId' => $id, - 'collectionId' => $collectionId, - 'data' => $args, - 'permissions' => $permissions, - ]); + $request->setURI($url($databaseId, $collectionId, $args)); + $request->setPost($params($databaseId, $collectionId, $args)); self::resolve($utopia, $request, $response, $resolve, $reject); } @@ -191,37 +180,25 @@ class Resolvers * @param App $utopia * @param string $databaseId * @param string $collectionId + * @param callable $url * @return callable */ public static function documentUpdate( App $utopia, string $databaseId, string $collectionId, - callable $url + callable $url, + callable $params, ): callable { return static fn($type, $args, $context, $info) => new Swoole( - function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $type, $args) { + function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $params, $type, $args) { $utopia = $utopia->getResource('utopia:graphql', true); $request = $utopia->getResource('request', true); $response = $utopia->getResource('response', true); - $documentId = $args['id']; - $permissions = $args['permissions'] ?? null; - - unset($args['id']); - unset($args['permissions']); - $request->setMethod('PATCH'); - $request->setURI($url($collectionId, $args)); - - // Order must be the same as the route params - $request->setPost([ - 'databaseId' => $databaseId, - 'collectionId' => $collectionId, - 'documentId' => $documentId, - 'data' => $args, - 'permissions' => $permissions, - ]); + $request->setURI($url($databaseId, $collectionId, $args)); + $request->setPost($params($databaseId, $collectionId, $args)); self::resolve($utopia, $request, $response, $resolve, $reject); } @@ -234,13 +211,14 @@ class Resolvers * @param App $utopia * @param string $databaseId * @param string $collectionId + * @param callable $url * @return callable */ public static function documentDelete( App $utopia, string $databaseId, string $collectionId, - callable $url + callable $url, ): callable { return static fn($type, $args, $context, $info) => new Swoole( function (callable $resolve, callable $reject) use ($utopia, $databaseId, $collectionId, $url, $type, $args) { @@ -248,10 +226,8 @@ class Resolvers $request = $utopia->getResource('request', true); $response = $utopia->getResource('response', true); - $documentId = $args['id']; - $request->setMethod('DELETE'); - $request->setURI($url($collectionId, $args)); + $request->setURI($url($databaseId, $collectionId, $args)); self::resolve($utopia, $request, $response, $resolve, $reject); } diff --git a/src/Appwrite/GraphQL/Schema.php b/src/Appwrite/GraphQL/Schema.php index 4fa8b0450d..41f95fe1d8 100644 --- a/src/Appwrite/GraphQL/Schema.php +++ b/src/Appwrite/GraphQL/Schema.php @@ -7,6 +7,7 @@ use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema as GQLSchema; use Utopia\App; +use Utopia\Exception; use Utopia\Route; class Schema @@ -15,13 +16,21 @@ class Schema protected static array $dirty = []; /** - * @throws \Exception + * + * @param App $utopia + * @param callable $complexity Function to calculate complexity + * @param callable $attributes Function to get attributes + * @param array $urls Array of functions to get urls for specific method types + * @param array $params Array of functions to build parameters for specific method types + * @return GQLSchema + * @throws Exception */ public static function build( App $utopia, callable $complexity, callable $attributes, - array $urls + array $urls, + array $params, ): GQLSchema { App::setResource('utopia:graphql', static function () use ($utopia) { return $utopia; @@ -39,7 +48,8 @@ class Schema // $utopia, // $complexity, // $attributes, - // $urls + // $urls, + // $params, //); $queries = \array_merge_recursive( @@ -132,7 +142,8 @@ class Schema App $utopia, callable $complexity, callable $attributes, - array $urls + array $urls, + array $params, ): array { $collections = []; $queryFields = []; @@ -194,6 +205,7 @@ class Schema $databaseId, $collectionId, $urls['list'], + $params['list'], ), 'complexity' => $complexity, ]; @@ -206,6 +218,7 @@ class Schema $databaseId, $collectionId, $urls['create'], + $params['create'], ) ]; $mutationFields[$collectionId . 'Update'] = [ @@ -222,6 +235,7 @@ class Schema $databaseId, $collectionId, $urls['update'], + $params['update'], ) ]; $mutationFields[$collectionId . 'Delete'] = [