diff --git a/src/Appwrite/GraphQL/Resolvers.php b/src/Appwrite/GraphQL/Resolvers.php index 74f7fb7619..27da07e03c 100644 --- a/src/Appwrite/GraphQL/Resolvers.php +++ b/src/Appwrite/GraphQL/Resolvers.php @@ -56,6 +56,30 @@ class Resolvers ); } + /** + * Create a resolver for getting a document in a specified database and collection. + * + * @param App $utopia + * @param Database $dbForProject + * @param string $databaseId + * @param string $collectionId + * @return callable + */ + public static function resolveDocument( + App $utopia, + Database $dbForProject, + string $databaseId, + string $collectionId, + string $methodType, + ): callable { + return [self::class, 'resolveDocument' . \ucfirst($methodType)]( + $utopia, + $dbForProject, + $databaseId, + $collectionId + ); + } + /** * Create a resolver for getting a document in a specified database and collection. * @@ -126,6 +150,42 @@ class Resolvers ); } + /** + * Create a resolver for creating a document in a specified database and collection. + * + * @param App $utopia + * @param Database $dbForProject + * @param string $databaseId + * @param string $collectionId + * @return callable + */ + public static function resolveDocumentCreate( + App $utopia, + Database $dbForProject, + string $databaseId, + string $collectionId, + ): callable { + return self::resolveDocumentMutate($utopia, $dbForProject, $databaseId, $collectionId, 'POST'); + } + + /** + * Create a resolver for updating a document in a specified database and collection. + * + * @param App $utopia + * @param Database $dbForProject + * @param string $databaseId + * @param string $collectionId + * @return callable + */ + public static function resolveDocumentUpdate( + App $utopia, + Database $dbForProject, + string $databaseId, + string $collectionId, + ): callable { + return self::resolveDocumentMutate($utopia, $dbForProject, $databaseId, $collectionId, 'PATCH'); + } + /** * Create a resolver for mutating a document in a specified database and collection. * @@ -136,7 +196,7 @@ class Resolvers * @param string $method * @return callable */ - public static function resolveDocumentMutate( + private static function resolveDocumentMutate( App $utopia, Database $dbForProject, string $databaseId, diff --git a/src/Appwrite/GraphQL/SchemaBuilder.php b/src/Appwrite/GraphQL/SchemaBuilder.php index ec59e4a52d..3985482bd2 100644 --- a/src/Appwrite/GraphQL/SchemaBuilder.php +++ b/src/Appwrite/GraphQL/SchemaBuilder.php @@ -128,7 +128,7 @@ class SchemaBuilder $params = []; foreach ($route->getParams() as $key => $value) { - $argType = TypeMapper::typeFromParameter( + $argType = TypeMapper::fromRouteParameter( $utopia, $value['validator'], !$value['optional'], @@ -199,8 +199,8 @@ class SchemaBuilder !empty($attrs = Authorization::skip(fn() => $dbForProject->find( collection: 'attributes', queries: [ - Query::limit($limit), - Query::offset($offset), + Query::limit($limit), + Query::offset($offset), ] ))) ) { @@ -233,11 +233,12 @@ class SchemaBuilder ]); $attributes = \array_merge( $attributes, - TypeRegistry::defaultArgsFor('mutate') + TypeRegistry::argumentsFor('mutate') ); + $queryFields[$collectionId . 'Get'] = [ 'type' => $objectType, - 'args' => TypeRegistry::defaultArgsFor('id'), + 'args' => TypeRegistry::argumentsFor('id'), 'resolve' => Resolvers::resolveDocumentGet( $utopia, $dbForProject, @@ -247,24 +248,25 @@ class SchemaBuilder ]; $queryFields[$collectionId . 'List'] = [ 'type' => $objectType, - 'args' => TypeRegistry::defaultArgsFor('list'), + 'args' => TypeRegistry::argumentsFor('list'), 'resolve' => Resolvers::resolveDocumentList( $utopia, $dbForProject, $databaseId, $collectionId ), - 'complexity' => fn(int $complexity, array $args) => $complexity * $args['limit'], + 'complexity' => function(int $complexity, array $args) { + return $complexity * $args['limit']; + }, ]; $mutationFields[$collectionId . 'Create'] = [ 'type' => $objectType, 'args' => $attributes, - 'resolve' => Resolvers::resolveDocumentMutate( + 'resolve' => Resolvers::resolveDocumentCreate( $utopia, $dbForProject, $databaseId, $collectionId, - 'POST' ) ]; $mutationFields[$collectionId . 'Update'] = [ @@ -275,12 +277,11 @@ class SchemaBuilder $dbForProject, $databaseId, $collectionId, - 'PATCH' ) ]; $mutationFields[$collectionId . 'Delete'] = [ 'type' => $objectType, - 'args' => TypeRegistry::defaultArgsFor('id'), + 'args' => TypeRegistry::argumentsFor('id'), 'resolve' => Resolvers::resolveDocumentDelete( $utopia, $dbForProject, diff --git a/src/Appwrite/GraphQL/TypeMapper.php b/src/Appwrite/GraphQL/TypeMapper.php index e0ac78e0c2..7d61d42d4b 100644 --- a/src/Appwrite/GraphQL/TypeMapper.php +++ b/src/Appwrite/GraphQL/TypeMapper.php @@ -21,7 +21,7 @@ class TypeMapper * @return Type * @throws Exception */ - public static function typeFromParameter( + public static function fromRouteParameter( App $utopia, Validator|callable $validator, bool $required, @@ -58,7 +58,7 @@ class TypeMapper break; case 'Utopia\Validator\ArrayList': /** @noinspection PhpPossiblePolymorphicInvocationInspection */ - $type = Type::listOf(self::typeFromParameter( + $type = Type::listOf(self::fromRouteParameter( $utopia, $validator->getValidator(), $required, @@ -118,10 +118,10 @@ class TypeMapper * @return Type * @throws Exception */ - public static function typeFromAttribute(string $type, bool $array, bool $required): Type + public static function fromCollectionAttribute(string $type, bool $array, bool $required): Type { if ($array) { - return Type::listOf(self::typeFromAttribute($type, false, $required)); + return Type::listOf(self::fromCollectionAttribute($type, false, $required)); } $type = match ($type) { diff --git a/src/Appwrite/GraphQL/TypeRegistry.php b/src/Appwrite/GraphQL/TypeRegistry.php index 1680c6588c..afdc12e8ff 100644 --- a/src/Appwrite/GraphQL/TypeRegistry.php +++ b/src/Appwrite/GraphQL/TypeRegistry.php @@ -150,7 +150,7 @@ class TypeRegistry * @param string $key * @return array */ - public static function defaultArgsFor(string $key): array + public static function argumentsFor(string $key): array { if (isset(self::$defaultDocumentArgs[$key])) { return self::$defaultDocumentArgs[$key];