1
0
Fork 0
mirror of synced 2024-10-05 12:43:13 +13:00

Abstract setting params to init

This commit is contained in:
Jake Barnby 2022-10-17 13:27:15 +13:00
parent fb68ed377e
commit fb5d2fc25c
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
3 changed files with 91 additions and 63 deletions

View file

@ -1082,20 +1082,57 @@ App::setResource('schema', function ($utopia, $dbForProject) {
}; };
$urls = [ $urls = [
'list' => function (string $collectionId, array $args) { 'list' => function (string $databaseId, string $collectionId, array $args) {
return "/v1/database/collections/{$collectionId}/documents"; return "/v1/databases/$databaseId/collections/$collectionId/documents";
}, },
'create' => function (string $collectionId, array $args) { 'create' => function (string $databaseId, string $collectionId, array $args) {
return "/v1/database/collections/{$collectionId}/documents"; return "/v1/databases/$databaseId/collections/$collectionId/documents";
}, },
'read' => function (string $collectionId, array $args) { 'read' => function (string $databaseId, string $collectionId, array $args) {
return "/v1/database/collections/{$collectionId}/documents/{$args['documentId']}"; return "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['documentId']}";
}, },
'update' => function (string $collectionId, array $args) { 'update' => function (string $databaseId, string $collectionId, array $args) {
return "/v1/database/collections/{$collectionId}/documents/{$args['documentId']}"; return "/v1/databases/$databaseId/collections/$collectionId/documents/{$args['documentId']}";
}, },
'delete' => function (string $collectionId, array $args) { 'delete' => function (string $databaseId, string $collectionId, array $args) {
return "/v1/database/collections/{$collectionId}/documents/{$args['documentId']}"; 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, $utopia,
$complexity, $complexity,
$attributes, $attributes,
$urls $urls,
$params,
); );
}, ['utopia', 'dbForProject']); }, ['utopia', 'dbForProject']);

View file

@ -85,13 +85,14 @@ class Resolvers
* @param App $utopia * @param App $utopia
* @param string $databaseId * @param string $databaseId
* @param string $collectionId * @param string $collectionId
* @param callable $url
* @return callable * @return callable
*/ */
public static function documentGet( public static function documentGet(
App $utopia, App $utopia,
string $databaseId, string $databaseId,
string $collectionId, string $collectionId,
callable $url callable $url,
): callable { ): callable {
return static fn($type, $args, $context, $info) => new Swoole( 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, $type, $args) {
@ -100,7 +101,7 @@ class Resolvers
$response = $utopia->getResource('response', true); $response = $utopia->getResource('response', true);
$request->setMethod('GET'); $request->setMethod('GET');
$request->setURI($url($collectionId, $args)); $request->setURI($url($databaseId, $collectionId, $args));
self::resolve($utopia, $request, $response, $resolve, $reject); self::resolve($utopia, $request, $response, $resolve, $reject);
} }
@ -113,25 +114,25 @@ class Resolvers
* @param App $utopia * @param App $utopia
* @param string $databaseId * @param string $databaseId
* @param string $collectionId * @param string $collectionId
* @param callable $url
* @return callable * @return callable
*/ */
public static function documentList( public static function documentList(
App $utopia, App $utopia,
string $databaseId, string $databaseId,
string $collectionId, string $collectionId,
callable $url callable $url,
callable $params,
): callable { ): callable {
return static fn($type, $args, $context, $info) => new Swoole( 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); $utopia = $utopia->getResource('utopia:graphql', true);
$request = $utopia->getResource('request', true); $request = $utopia->getResource('request', true);
$response = $utopia->getResource('response', true); $response = $utopia->getResource('response', true);
$request->setMethod('GET'); $request->setMethod('GET');
$request->setURI($url($collectionId, $args)); $request->setURI($url($databaseId, $collectionId, $args));
$request->setGet([ $request->setGet($params($databaseId, $collectionId, $args));
'queries' => $args['queries'],
]);
$beforeResolve = function ($payload) { $beforeResolve = function ($payload) {
return $payload['documents']; return $payload['documents'];
@ -148,37 +149,25 @@ class Resolvers
* @param App $utopia * @param App $utopia
* @param string $databaseId * @param string $databaseId
* @param string $collectionId * @param string $collectionId
* @param callable $url
* @return callable * @return callable
*/ */
public static function documentCreate( public static function documentCreate(
App $utopia, App $utopia,
string $databaseId, string $databaseId,
string $collectionId, string $collectionId,
callable $url callable $url,
callable $params,
): callable { ): callable {
return static fn($type, $args, $context, $info) => new Swoole( 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); $utopia = $utopia->getResource('utopia:graphql', true);
$request = $utopia->getResource('request', true); $request = $utopia->getResource('request', true);
$response = $utopia->getResource('response', true); $response = $utopia->getResource('response', true);
$id = $args['id'] ?? 'unique()';
$permissions = $args['permissions'] ?? null;
unset($args['id']);
unset($args['permissions']);
$request->setMethod('POST'); $request->setMethod('POST');
$request->setURI($url($collectionId, $args)); $request->setURI($url($databaseId, $collectionId, $args));
$request->setPost($params($databaseId, $collectionId, $args));
// Order must be the same as the route params
$request->setPost([
'databaseId' => $databaseId,
'documentId' => $id,
'collectionId' => $collectionId,
'data' => $args,
'permissions' => $permissions,
]);
self::resolve($utopia, $request, $response, $resolve, $reject); self::resolve($utopia, $request, $response, $resolve, $reject);
} }
@ -191,37 +180,25 @@ class Resolvers
* @param App $utopia * @param App $utopia
* @param string $databaseId * @param string $databaseId
* @param string $collectionId * @param string $collectionId
* @param callable $url
* @return callable * @return callable
*/ */
public static function documentUpdate( public static function documentUpdate(
App $utopia, App $utopia,
string $databaseId, string $databaseId,
string $collectionId, string $collectionId,
callable $url callable $url,
callable $params,
): callable { ): callable {
return static fn($type, $args, $context, $info) => new Swoole( 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); $utopia = $utopia->getResource('utopia:graphql', true);
$request = $utopia->getResource('request', true); $request = $utopia->getResource('request', true);
$response = $utopia->getResource('response', true); $response = $utopia->getResource('response', true);
$documentId = $args['id'];
$permissions = $args['permissions'] ?? null;
unset($args['id']);
unset($args['permissions']);
$request->setMethod('PATCH'); $request->setMethod('PATCH');
$request->setURI($url($collectionId, $args)); $request->setURI($url($databaseId, $collectionId, $args));
$request->setPost($params($databaseId, $collectionId, $args));
// Order must be the same as the route params
$request->setPost([
'databaseId' => $databaseId,
'collectionId' => $collectionId,
'documentId' => $documentId,
'data' => $args,
'permissions' => $permissions,
]);
self::resolve($utopia, $request, $response, $resolve, $reject); self::resolve($utopia, $request, $response, $resolve, $reject);
} }
@ -234,13 +211,14 @@ class Resolvers
* @param App $utopia * @param App $utopia
* @param string $databaseId * @param string $databaseId
* @param string $collectionId * @param string $collectionId
* @param callable $url
* @return callable * @return callable
*/ */
public static function documentDelete( public static function documentDelete(
App $utopia, App $utopia,
string $databaseId, string $databaseId,
string $collectionId, string $collectionId,
callable $url callable $url,
): callable { ): callable {
return static fn($type, $args, $context, $info) => new Swoole( 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, $type, $args) {
@ -248,10 +226,8 @@ class Resolvers
$request = $utopia->getResource('request', true); $request = $utopia->getResource('request', true);
$response = $utopia->getResource('response', true); $response = $utopia->getResource('response', true);
$documentId = $args['id'];
$request->setMethod('DELETE'); $request->setMethod('DELETE');
$request->setURI($url($collectionId, $args)); $request->setURI($url($databaseId, $collectionId, $args));
self::resolve($utopia, $request, $response, $resolve, $reject); self::resolve($utopia, $request, $response, $resolve, $reject);
} }

View file

@ -7,6 +7,7 @@ use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type; use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema as GQLSchema; use GraphQL\Type\Schema as GQLSchema;
use Utopia\App; use Utopia\App;
use Utopia\Exception;
use Utopia\Route; use Utopia\Route;
class Schema class Schema
@ -15,13 +16,21 @@ class Schema
protected static array $dirty = []; 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( public static function build(
App $utopia, App $utopia,
callable $complexity, callable $complexity,
callable $attributes, callable $attributes,
array $urls array $urls,
array $params,
): GQLSchema { ): GQLSchema {
App::setResource('utopia:graphql', static function () use ($utopia) { App::setResource('utopia:graphql', static function () use ($utopia) {
return $utopia; return $utopia;
@ -39,7 +48,8 @@ class Schema
// $utopia, // $utopia,
// $complexity, // $complexity,
// $attributes, // $attributes,
// $urls // $urls,
// $params,
//); //);
$queries = \array_merge_recursive( $queries = \array_merge_recursive(
@ -132,7 +142,8 @@ class Schema
App $utopia, App $utopia,
callable $complexity, callable $complexity,
callable $attributes, callable $attributes,
array $urls array $urls,
array $params,
): array { ): array {
$collections = []; $collections = [];
$queryFields = []; $queryFields = [];
@ -194,6 +205,7 @@ class Schema
$databaseId, $databaseId,
$collectionId, $collectionId,
$urls['list'], $urls['list'],
$params['list'],
), ),
'complexity' => $complexity, 'complexity' => $complexity,
]; ];
@ -206,6 +218,7 @@ class Schema
$databaseId, $databaseId,
$collectionId, $collectionId,
$urls['create'], $urls['create'],
$params['create'],
) )
]; ];
$mutationFields[$collectionId . 'Update'] = [ $mutationFields[$collectionId . 'Update'] = [
@ -222,6 +235,7 @@ class Schema
$databaseId, $databaseId,
$collectionId, $collectionId,
$urls['update'], $urls['update'],
$params['update'],
) )
]; ];
$mutationFields[$collectionId . 'Delete'] = [ $mutationFields[$collectionId . 'Delete'] = [