diff --git a/src/Appwrite/GraphQL/Builder.php b/src/Appwrite/GraphQL/Builder.php index 1f3698521a..866033537d 100644 --- a/src/Appwrite/GraphQL/Builder.php +++ b/src/Appwrite/GraphQL/Builder.php @@ -245,26 +245,29 @@ class Builder { $responseModel = $response->getModel($responseModelName); self::createTypeMapping($responseModel, $response); $type = self::$typeMapping[$responseModel->getType()]; + $description = $route->getDesc(); $args = self::getArgs($route->getParams(), $utopia); + + $resolve = function ($type, $args, $context, $info) use (&$register, $route) { + $utopia = $register->get('__app'); + $utopia->setRoute($route) + ->execute($route, $args); + $response = $register->get('__response'); + $result = $response->getPayload(); + if (self::isModel($result, $response->getModel(Response::MODEL_ERROR)) || self::isModel($result, $response->getModel(Response::MODEL_ERROR_DEV))) { + var_dump("***** There has been an exception.. *****"); + unset($result['trace']); + // var_dump($result); + throw new Exception($result['message'], $result['code']); + } + return $result; + }; $field = [ 'type' => $type, - 'description' => $route->getDesc(), + 'description' => $description, 'args' => $args, - 'resolve' => function ($type, $args, $context, $info) use (&$register, $route) { - $utopia = $register->get('__app'); - $utopia->setRoute($route) - ->execute($route, $args); - $response = $register->get('__response'); - $result = $response->getPayload(); - if (self::isModel($result, $response->getModel(Response::MODEL_ERROR)) || self::isModel($result, $response->getModel(Response::MODEL_ERROR_DEV))) { - var_dump("***** There has been an exception.. *****"); - unset($result['trace']); - // var_dump($result); - throw new Exception($result['message'], $result['code']); - } - return $result; - } + 'resolve' => $resolve ]; if ($method == 'GET') { diff --git a/tests/e2e/Services/GraphQL/GraphQLBase.php b/tests/e2e/Services/GraphQL/GraphQLBase.php index 117d318982..8f24e41099 100644 --- a/tests/e2e/Services/GraphQL/GraphQLBase.php +++ b/tests/e2e/Services/GraphQL/GraphQLBase.php @@ -6,52 +6,50 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideClient; +use Tests\E2E\Scopes\SideServer; class GraphQLBase extends Scope { use ProjectCustom; - use SideClient; + use SideServer; - public function testListUsers() - { - - /** - * Test for SUCCESS - */ - - // $projectId = $this->getProject()['$id']; - - $projectId = '60394d47b252a'; - $collectionId = "6048c40b28392"; - + public function createKey(string $name, array $scopes): string { + $projectId = $this->getProject()['$id']; $query = " - query listDocuments(\$collectionId: String!){ - database_listDocuments (collectionId: \$collectionId) { - sum - documents + mutation createKey(\$projectId: String!, \$name: String!, \$scopes: [Json]!){ + projects_createKey (projectId: \$projectId, name: \$name, scopes: \$scopes) { + id + name + scopes + secret } } "; - + $variables = [ - 'collectionId' => $collectionId + "projectId" => $projectId, + "name" => $name, + "scopes" => $scopes ]; $graphQLPayload = [ "query" => $query, "variables" => $variables ]; - - $response = $this->client->call(Client::METHOD_POST, '/graphql', array_merge([ + + $key = $this->client->call(Client::METHOD_POST, '/graphql', [ 'origin' => 'http://localhost', 'content-type' => 'application/json', - 'x-appwrite-project' => $projectId, - ]), $graphQLPayload); + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + 'x-appwrite-project' => 'console' + ], $graphQLPayload); - var_dump($response['headers']); - var_dump($response['body']); - $this->assertEquals($response['headers']['status-code'], 200); - + $this->assertEquals($key['headers']['status-code'], 201); + $this->assertNull($key['body']['errors']); + $this->assertIsArray($key['body']['data']); + $this->assertIsArray($key['body']['data']['projects_createKey']); + + return $key['body']['data']['projects_createKey']['secret']; } } \ No newline at end of file diff --git a/tests/e2e/Services/GraphQL/GraphQLServerTest.php b/tests/e2e/Services/GraphQL/GraphQLServerTest.php index b428dfc090..1ba34e92b6 100644 --- a/tests/e2e/Services/GraphQL/GraphQLServerTest.php +++ b/tests/e2e/Services/GraphQL/GraphQLServerTest.php @@ -7,12 +7,12 @@ use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; -use function PHPSTORM_META\type; class GraphQLServerTest extends Scope { use SideServer; use ProjectCustom; + use GraphQLBase; public function testCreateCollection(): array { $projectId = $this->getProject()['$id']; @@ -388,45 +388,6 @@ class GraphQLServerTest extends Scope $this->assertIsArray($countries['body']['data']); $this->assertNull($countries['body']['data']['locale_getCountries']); - } - public function createKey(string $name, array $scopes): string { - $projectId = $this->getProject()['$id']; - $query = " - mutation createKey(\$projectId: String!, \$name: String!, \$scopes: [Json]!){ - projects_createKey (projectId: \$projectId, name: \$name, scopes: \$scopes) { - id - name - scopes - secret - } - } - "; - - $variables = [ - "projectId" => $projectId, - "name" => $name, - "scopes" => $scopes - ]; - - $graphQLPayload = [ - "query" => $query, - "variables" => $variables - ]; - - $key = $this->client->call(Client::METHOD_POST, '/graphql', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'cookie' => 'a_session_console=' . $this->getRoot()['session'], - 'x-appwrite-project' => 'console' - ], $graphQLPayload); - - $this->assertEquals($key['headers']['status-code'], 201); - $this->assertNull($key['body']['errors']); - $this->assertIsArray($key['body']['data']); - $this->assertIsArray($key['body']['data']['projects_createKey']); - - return $key['body']['data']['projects_createKey']['secret']; - } } \ No newline at end of file