From 4ca6750309758e6fab036614d84f01fe1f27e001 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 13 Jul 2022 23:21:02 +1200 Subject: [PATCH] Cleanup controller --- app/controllers/api/functions.php | 4 +- app/controllers/api/graphql.php | 104 ++++++++++++------ src/Appwrite/GraphQL/TypeRegistry.php | 2 +- .../GraphQL/GraphQLFunctionsServerTest.php | 4 +- 4 files changed, 75 insertions(+), 39 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index fa7ffdbcb..0137ab92a 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -439,7 +439,7 @@ App::delete('/v1/functions/:functionId') $events->setParam('functionId', $function->getId()); - $response->dynamic(new Document(), Response::MODEL_NONE); + $response->noContent(); }); App::post('/v1/functions/:functionId/deployments') @@ -781,7 +781,7 @@ App::delete('/v1/functions/:functionId/deployments/:deploymentId') ->setType(DELETE_TYPE_DOCUMENT) ->setDocument($deployment); - $response->dynamic(new Document(), Response::MODEL_NONE); + $response->noContent(); }); App::post('/v1/functions/:functionId/executions') diff --git a/app/controllers/api/graphql.php b/app/controllers/api/graphql.php index 484e8fa3c..5666f2dd3 100644 --- a/app/controllers/api/graphql.php +++ b/app/controllers/api/graphql.php @@ -2,6 +2,7 @@ use Appwrite\Extend\Exception; use Appwrite\GraphQL\Promises\CoroutinePromiseAdapter; +use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use GraphQL\Error\DebugFlag; use GraphQL\GraphQL; @@ -70,32 +71,14 @@ function graphqlRequest( $contentType = $request->getHeader('content-type'); if ($contentType === 'application/graphql') { - $query = [ 'query' => $request->getSwoole()->rawContent() ]; + $query = parseGraphqlRequest($request); + } + if (\str_starts_with($contentType, 'multipart/form-data')) { + $query = parseMultipartRequest($query, $request); } - if (!isset($query[0])) { $query = [$query]; } - - if (\str_starts_with($contentType, 'multipart/form-data')) { - $operations = \json_decode($query[0]['operations'], true); - $map = \json_decode($query[0]['map'], true); - foreach ($map as $fileKey => $locations) { - foreach ($locations as $location) { - $items = &$operations; - foreach (\explode('.', $location) as $key) { - if (!isset($items[$key]) || !\is_array($items[$key])) { - $items[$key] = []; - } - $items = &$items[$key]; - } - $items = $request->getFiles($fileKey); - } - } - $query[0]['query'] = $operations['query']; - $query[0]['variables'] = $operations['variables']; - } - if (empty($query)) { throw new Exception('No query supplied.', 400, Exception::GRAPHQL_NO_QUERY); } @@ -109,9 +92,9 @@ function graphqlRequest( if (App::isProduction()) { $validations[] = new DisableIntrospection(); - $debugFlags = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE; - } else { $debugFlags = DebugFlag::NONE; + } else { + $debugFlags = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE; } $promises = []; @@ -130,18 +113,11 @@ function graphqlRequest( $wg = new WaitGroup(); $wg->add(); $promiseAdapter->all($promises)->then( - function ($result) use ($response, &$output, $wg, $debugFlags) { - foreach ($result as $queryResponse) { - $output[] = $queryResponse->toArray($debugFlags); - } - if (isset($output[1])) { - $output = \array_merge_recursive(...$output); - } else { - $output = $output[0]; - } + function (array $results) use (&$output, &$wg, $debugFlags) { + processResult($results, $output, $debugFlags); $wg->done(); }, - function ($error) use ($response, &$output, $wg) { + function ($error) use (&$output, $wg) { $output = ['errors' => [$error]]; $wg->done(); } @@ -150,3 +126,63 @@ function graphqlRequest( $response->json($output); } + +/** + * Parse an application/graphql request + * + * @param Request $request + * @return array + */ +function parseGraphqlRequest(Request $request): array +{ + return [ 'query' => $request->getSwoole()->rawContent() ]; +} + +/** + * Parse a multipart/form-data request + * + * @param array $query + * @param Request $request + * @return array + */ +function parseMultipartRequest(array $query, Request $request): array +{ + $operations = \json_decode($query['operations'], true); + $map = \json_decode($query['map'], true); + foreach ($map as $fileKey => $locations) { + foreach ($locations as $location) { + $items = &$operations; + foreach (\explode('.', $location) as $key) { + if (!isset($items[$key]) || !\is_array($items[$key])) { + $items[$key] = []; + } + $items = &$items[$key]; + } + $items = $request->getFiles($fileKey); + } + } + $query['query'] = $operations['query']; + $query['variables'] = $operations['variables']; + + return $query; +} + +/** + * Process an array of results for output + * + * @param $result + * @param $output + * @param $debugFlags + * @return void + */ +function processResult($result, &$output, $debugFlags): void +{ + if (!isset($result[1])) { + $output = $result[0]->toArray($debugFlags); + } else { + $output = \array_merge_recursive(...\array_map( + fn ($item) => $item->toArray($debugFlags), + $result + )); + } +} diff --git a/src/Appwrite/GraphQL/TypeRegistry.php b/src/Appwrite/GraphQL/TypeRegistry.php index 848220d2a..4ad97265b 100644 --- a/src/Appwrite/GraphQL/TypeRegistry.php +++ b/src/Appwrite/GraphQL/TypeRegistry.php @@ -126,7 +126,7 @@ class TypeRegistry } else { try { $complexModel = self::$models[$type]; - $type = self::get($complexModel); + $type = self::get($complexModel->getType()); } catch (\Exception) { Console::error('Could not find model for ' . $type); } diff --git a/tests/e2e/Services/GraphQL/GraphQLFunctionsServerTest.php b/tests/e2e/Services/GraphQL/GraphQLFunctionsServerTest.php index 5dd83fa95..ecfb5f27e 100644 --- a/tests/e2e/Services/GraphQL/GraphQLFunctionsServerTest.php +++ b/tests/e2e/Services/GraphQL/GraphQLFunctionsServerTest.php @@ -400,7 +400,7 @@ class GraphQLFunctionsServerTest extends Scope 'x-appwrite-project' => $projectId, ], $this->getHeaders()), $gqlPayload); - $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(204, $response['headers']['status-code']); } /** @@ -425,6 +425,6 @@ class GraphQLFunctionsServerTest extends Scope 'x-appwrite-project' => $projectId, ], $this->getHeaders()), $gqlPayload); - $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(204, $response['headers']['status-code']); } }