1
0
Fork 0
mirror of synced 2024-07-01 04:30:59 +12:00

Pass-thru graphql args as request parameters

This commit is contained in:
Jake Barnby 2022-04-08 19:05:59 +12:00
parent 47642f63ff
commit af128ede7c
No known key found for this signature in database
GPG key ID: A4674EBC0E404657
2 changed files with 19 additions and 3 deletions

View file

@ -5,11 +5,11 @@ use Appwrite\Utopia\Response;
use GraphQL\Error\DebugFlag;
use GraphQL\GraphQL;
use GraphQL\Type;
use GraphQL\Validator\Rules\DisableIntrospection;
use GraphQL\Validator\Rules\QueryComplexity;
use GraphQL\Validator\Rules\QueryDepth;
use Swoole\Coroutine\WaitGroup;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Validator\JSON;
use Utopia\Validator\Text;
@ -44,12 +44,15 @@ App::post('/v1/graphql')
/** @var Utopia\Registry\Registry $register */
/** @var \Utopia\Database\Database $dbForProject */
$start = microtime(true);
// Should allow accepting entire body as query if content-type is application/graphql
if ($request->getHeader('content-type') === 'application/graphql') {
$query = \implode("\r\n", $request->getParams());
}
$debugFlags = App::isDevelopment()
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS
: DebugFlag::NONE;
$validations = array_merge(
@ -57,7 +60,7 @@ App::post('/v1/graphql')
[
new QueryComplexity(App::getEnv('_APP_GRAPHQL_MAX_QUERY_COMPLEXITY', 200)),
new QueryDepth(App::getEnv('_APP_GRAPHQL_MAX_QUERY_DEPTH', 3)),
new DisableIntrospection(),
//new DisableIntrospection(),
]
);
@ -90,4 +93,7 @@ App::post('/v1/graphql')
}
);
$wg->wait(App::getEnv('_APP_GRAPHQL_REQUEST_TIMEOUT', 30));
$time_elapsed_secs = (microtime(true) - $start) * 1000;
Console::info("[DEBUG] GraphQL Action Time: {$time_elapsed_secs}ms");
});

View file

@ -514,6 +514,16 @@ class Builder
/* Define a resolve function that defines how to fetch data for this type */
$resolve = fn($type, $args, $context, $info) => new CoroutinePromise(
function (callable $resolve, callable $reject) use ($utopia, $request, $response, &$register, $route, $args) {
// Mutate the original request object to include the query variables at the top level
$swooleRq = (fn() => $this->swoole)->bindTo($request, $request)();
$swooleRq->post = $args;
// Drop json content type so post args are used directly
if ($swooleRq->header['content-type'] === 'application/json') {
unset($swooleRq->header['content-type']);
}
$request = new Request($swooleRq);
$utopia
->setRoute($route)
->execute($route, $request);