Refactor methods for clarity

This commit is contained in:
Jake Barnby 2022-09-22 20:59:47 +12:00
parent 5bd420a823
commit 5f0f136e6e
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
4 changed files with 78 additions and 17 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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) {

View file

@ -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];