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

Fix custom entity update

This commit is contained in:
Jake Barnby 2022-09-23 13:49:36 +12:00
parent 6e4bad5969
commit eb818d15e0
No known key found for this signature in database
GPG key ID: C437A8CC85B96E9C
3 changed files with 72 additions and 63 deletions

View file

@ -165,7 +165,34 @@ class Resolvers
string $databaseId,
string $collectionId,
): callable {
return self::resolveDocumentMutate($utopia, $dbForProject, $databaseId, $collectionId, 'POST');
return static fn($type, $args, $context, $info) => new CoroutinePromise(
function (callable $resolve, callable $reject) use ($utopia, $dbForProject, $databaseId, $collectionId, $type, $args) {
$utopia = $utopia->getResource('current', true);
$request = $utopia->getResource('request', true);
$response = $utopia->getResource('response', true);
$swoole = $request->getSwoole();
$id = $args['id'] ?? ID::unique();
$permissions = $args['permissions'] ?? null;
unset($args['id']);
unset($args['permissions']);
// Order must be the same as the route params
$swoole->post = [
'databaseId' => $databaseId,
'documentId' => $id,
'collectionId' => $collectionId,
'data' => $args,
'permissions' => $permissions,
];
$swoole->server['request_method'] = 'POST';
$swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents";
$swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents";
self::resolve($utopia, $request, $response, $resolve, $reject);
}
);
}
/**
@ -182,36 +209,16 @@ class Resolvers
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.
*
* @param App $utopia
* @param Database $dbForProject
* @param string $databaseId
* @param string $collectionId
* @param string $method
* @return callable
*/
private static function resolveDocumentMutate(
App $utopia,
Database $dbForProject,
string $databaseId,
string $collectionId,
string $method,
): callable {
return static fn($type, $args, $context, $info) => new CoroutinePromise(
function (callable $resolve, callable $reject) use ($utopia, $dbForProject, $databaseId, $collectionId, $method, $type, $args) {
function (callable $resolve, callable $reject) use ($utopia, $dbForProject, $databaseId, $collectionId, $type, $args) {
$utopia = $utopia->getResource('current', true);
$request = $utopia->getResource('request', true);
$response = $utopia->getResource('response', true);
$swoole = $request->getSwoole();
$id = $args['id'] ?? ID::unique();
$permissions = $args['permissions'];
$documentId = $args['id'];
$permissions = $args['permissions'] ?? null;
unset($args['id']);
unset($args['permissions']);
@ -219,14 +226,14 @@ class Resolvers
// Order must be the same as the route params
$swoole->post = [
'databaseId' => $databaseId,
'documentId' => $id,
'collectionId' => $collectionId,
'documentId' => $documentId,
'data' => $args,
'permissions' => $permissions,
];
$swoole->server['request_method'] = $method;
$swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents";
$swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents";
$swoole->server['request_method'] = 'PATCH';
$swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents/$documentId";
$swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents/$documentId";
self::resolve($utopia, $request, $response, $resolve, $reject);
}
@ -256,7 +263,7 @@ class Resolvers
$swoole = $request->getSwoole();
$documentId = $args['id'];
$swoole->server['request_method'] = 'DELETE';
$swoole->server['request_uri'] = "/v1/databases/$databaseId/collections/$collectionId/documents/$documentId";
$swoole->server['path_info'] = "/v1/databases/$databaseId/collections/$collectionId/documents/$documentId";

View file

@ -279,8 +279,14 @@ class SchemaBuilder
];
$mutationFields[$collectionId . 'Update'] = [
'type' => $objectType,
'args' => $attributes,
'resolve' => Resolvers::resolveDocumentMutate(
'args' => \array_merge(
TypeRegistry::argumentsFor('id'),
\array_map(
fn($attr) => $attr['type'] = Type::getNullableType($attr['type']),
$attributes
)
),
'resolve' => Resolvers::resolveDocumentUpdate(
$utopia,
$dbForProject,
$databaseId,

View file

@ -372,7 +372,7 @@ class DatabaseServerTest extends Scope
public function testCreateIndex($data): array
{
// Wait for attributes to be available
sleep(3);
sleep(2);
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$CREATE_INDEX);
@ -885,36 +885,33 @@ class DatabaseServerTest extends Scope
$this->assertStringContainsString('New Document Name', $document['data']);
}
// /**
// * @depends testCreateCustomEntity
// * @throws Exception
// */
// public function testUpdateCustomEntity(array $data) {
// $projectId = $this->getProject()['$id'];
// $query = $this->getQuery(self::$UPDATE_CUSTOM_ENTITY);
// $gqlPayload = [
// 'query' => $query,
// 'variables' => [
// 'id' => $data['_id'],
// 'data' => [
// 'name' => 'New Custom Entity Name',
// ],
// ]
// ];
//
// $entity = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $projectId,
// ], $this->getHeaders()), $gqlPayload);
//
// \var_dump($entity);
//
// $this->assertArrayNotHasKey('errors', $entity['body']);
// $this->assertIsArray($entity['body']['data']);
// $entity = $entity['body']['data']['actorsUpdate'];
// $this->assertIsArray($entity);
// $this->assertStringContainsString('New Custom Entity Name', $entity['data']);
// }
/**
* @depends testCreateCustomEntity
* @throws Exception
*/
public function testUpdateCustomEntity(array $data)
{
$projectId = $this->getProject()['$id'];
$query = $this->getQuery(self::$UPDATE_CUSTOM_ENTITY);
$gqlPayload = [
'query' => $query,
'variables' => [
'id' => $data['_id'],
'name' => 'New Custom Entity Name',
]
];
$entity = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), $gqlPayload);
$this->assertArrayNotHasKey('errors', $entity['body']);
$this->assertIsArray($entity['body']['data']);
$entity = $entity['body']['data']['actorsUpdate'];
$this->assertIsArray($entity);
$this->assertStringContainsString('New Custom Entity Name', $entity['name']);
}
/**
* @depends testCreateDocument
@ -1040,5 +1037,4 @@ class DatabaseServerTest extends Scope
$this->assertIsNotArray($database['body']);
$this->assertEquals(204, $database['headers']['status-code']);
}
}