1
0
Fork 0
mirror of synced 2024-06-28 19:20:25 +12:00

Update promise result output

This commit is contained in:
Jake Barnby 2022-05-02 20:21:40 +12:00
parent e3824170b1
commit 2ee5762bc7
No known key found for this signature in database
GPG key ID: A4674EBC0E404657

View file

@ -3,6 +3,7 @@
use Appwrite\Extend\Exception;
use Appwrite\Utopia\Response;
use GraphQL\Error\DebugFlag;
use GraphQL\Executor\ExecutionResult;
use GraphQL\GraphQL;
use GraphQL\Type;
use GraphQL\Validator\Rules\DisableIntrospection;
@ -62,6 +63,7 @@ App::post('/v1/graphql')
/**
* @throws Exception
* @throws \Exception
*/
function graphqlRequest(
$query,
@ -85,17 +87,14 @@ function graphqlRequest(
$query = $request->getSwoole()->rawContent();
}
if (empty($query)) {
throw new Exception('No query supplied.', 400, Exception::GRAPHQL_NO_QUERY);
throw new Exception('No query supplied.', 400, Exception::GRAPHQL_NO_QUERY);
}
$debugFlags = App::isDevelopment()
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS
: DebugFlag::NONE;
// Roughly equivalent to 200 REST requests of work per GraphQL request
$maxComplexity = App::getEnv('_APP_GRAPHQL_MAX_QUERY_COMPLEXITY', 200);
// Maximum nested query depth. Limited to 3 as we don't have more than 3 levels of data relationships
$maxDepth = App::getEnv('_APP_GRAPHQL_MAX_QUERY_DEPTH', 3);
$validations = GraphQL::getStandardValidationRules();
@ -112,25 +111,20 @@ function graphqlRequest(
validationRules: $validations
);
// Blocking wait while queries resolve
$output = [];
$wg = new WaitGroup();
$wg->add();
$promise->then(
function ($result) use ($response, $debugFlags, $wg) {
$result = $result->toArray($debugFlags);
\var_dump("Result:" . $result);
if (isset($result['errors'])) {
$response->json(['data' => [], ...$result]);
$wg->done();
return;
}
$response->json(['data' => $result]);
function ($result) use ($response, $debugFlags, &$output, $wg) {
$output = $result->toArray($debugFlags);
$wg->done();
},
function ($error) use ($response, $wg) {
$response->text(\json_encode(['errors' => [\json_encode($error)]]));
function ($error) use ($response, &$output, $wg) {
$output = ['errors' => [$error]];
$wg->done();
}
);
$wg->wait();
$response->json($output);
}