Pass-thru graphql args as request parameters
This commit is contained in:
parent
47642f63ff
commit
af128ede7c
2 changed files with 19 additions and 3 deletions
|
@ -5,11 +5,11 @@ use Appwrite\Utopia\Response;
|
||||||
use GraphQL\Error\DebugFlag;
|
use GraphQL\Error\DebugFlag;
|
||||||
use GraphQL\GraphQL;
|
use GraphQL\GraphQL;
|
||||||
use GraphQL\Type;
|
use GraphQL\Type;
|
||||||
use GraphQL\Validator\Rules\DisableIntrospection;
|
|
||||||
use GraphQL\Validator\Rules\QueryComplexity;
|
use GraphQL\Validator\Rules\QueryComplexity;
|
||||||
use GraphQL\Validator\Rules\QueryDepth;
|
use GraphQL\Validator\Rules\QueryDepth;
|
||||||
use Swoole\Coroutine\WaitGroup;
|
use Swoole\Coroutine\WaitGroup;
|
||||||
use Utopia\App;
|
use Utopia\App;
|
||||||
|
use Utopia\CLI\Console;
|
||||||
use Utopia\Validator\JSON;
|
use Utopia\Validator\JSON;
|
||||||
use Utopia\Validator\Text;
|
use Utopia\Validator\Text;
|
||||||
|
|
||||||
|
@ -44,12 +44,15 @@ App::post('/v1/graphql')
|
||||||
/** @var Utopia\Registry\Registry $register */
|
/** @var Utopia\Registry\Registry $register */
|
||||||
/** @var \Utopia\Database\Database $dbForProject */
|
/** @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') {
|
if ($request->getHeader('content-type') === 'application/graphql') {
|
||||||
$query = \implode("\r\n", $request->getParams());
|
$query = \implode("\r\n", $request->getParams());
|
||||||
}
|
}
|
||||||
|
|
||||||
$debugFlags = App::isDevelopment()
|
$debugFlags = App::isDevelopment()
|
||||||
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
|
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS
|
||||||
: DebugFlag::NONE;
|
: DebugFlag::NONE;
|
||||||
|
|
||||||
$validations = array_merge(
|
$validations = array_merge(
|
||||||
|
@ -57,7 +60,7 @@ App::post('/v1/graphql')
|
||||||
[
|
[
|
||||||
new QueryComplexity(App::getEnv('_APP_GRAPHQL_MAX_QUERY_COMPLEXITY', 200)),
|
new QueryComplexity(App::getEnv('_APP_GRAPHQL_MAX_QUERY_COMPLEXITY', 200)),
|
||||||
new QueryDepth(App::getEnv('_APP_GRAPHQL_MAX_QUERY_DEPTH', 3)),
|
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));
|
$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");
|
||||||
});
|
});
|
||||||
|
|
|
@ -514,6 +514,16 @@ class Builder
|
||||||
/* Define a resolve function that defines how to fetch data for this type */
|
/* Define a resolve function that defines how to fetch data for this type */
|
||||||
$resolve = fn($type, $args, $context, $info) => new CoroutinePromise(
|
$resolve = fn($type, $args, $context, $info) => new CoroutinePromise(
|
||||||
function (callable $resolve, callable $reject) use ($utopia, $request, $response, &$register, $route, $args) {
|
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
|
$utopia
|
||||||
->setRoute($route)
|
->setRoute($route)
|
||||||
->execute($route, $request);
|
->execute($route, $request);
|
||||||
|
|
Loading…
Reference in a new issue