Add GET endpoint as required by spec
This commit is contained in:
parent
d4a77504c0
commit
b4ff4aa123
1 changed files with 103 additions and 57 deletions
|
@ -9,18 +9,17 @@ 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;
|
||||||
|
|
||||||
App::post('/v1/graphql')
|
App::get('/v1/graphql')
|
||||||
->desc('GraphQL Endpoint')
|
->desc('GraphQL Endpoint')
|
||||||
->groups(['api', 'grapgql'])
|
->groups(['api', 'grapgql'])
|
||||||
->label('scope', 'graphql')
|
->label('scope', 'graphql')
|
||||||
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT])
|
||||||
->label('sdk.namespace', 'graphql')
|
->label('sdk.namespace', 'graphql')
|
||||||
->label('sdk.method', 'execute')
|
->label('sdk.method', 'query')
|
||||||
->label('sdk.description', '/docs/references/graphql/execute.md')
|
->label('sdk.description', '/docs/references/graphql/query.md')
|
||||||
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
||||||
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||||
->label('sdk.response.model', Response::MODEL_ANY)
|
->label('sdk.response.model', Response::MODEL_ANY)
|
||||||
|
@ -36,19 +35,61 @@ App::post('/v1/graphql')
|
||||||
->inject('dbForProject')
|
->inject('dbForProject')
|
||||||
->inject('promiseAdapter')
|
->inject('promiseAdapter')
|
||||||
->inject('apiSchema')
|
->inject('apiSchema')
|
||||||
->action(function ($query, $operationName, $variables, $request, $response, $utopia, $register, $dbForProject, $promiseAdapter, $apiSchema) {
|
->action(Closure::fromCallable('graphqlRequest'));
|
||||||
/** @var Utopia\Swoole\Request $request */
|
|
||||||
|
App::post('/v1/graphql')
|
||||||
|
->desc('GraphQL Endpoint')
|
||||||
|
->groups(['api', 'grapgql'])
|
||||||
|
->label('scope', 'graphql')
|
||||||
|
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT])
|
||||||
|
->label('sdk.namespace', 'graphql')
|
||||||
|
->label('sdk.method', 'mutate')
|
||||||
|
->label('sdk.description', '/docs/references/graphql/mutate.md')
|
||||||
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
||||||
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||||
|
->label('sdk.response.model', Response::MODEL_ANY)
|
||||||
|
->label('abuse-limit', 60)
|
||||||
|
->label('abuse-time', 60)
|
||||||
|
->param('query', '', new Text(1024), 'The query to execute. Max 1024 chars.', true)
|
||||||
|
->param('operationName', null, new Text(256), 'Name of the operation to execute', true)
|
||||||
|
->param('variables', [], new JSON(), 'Variables to use in the operation', true)
|
||||||
|
->inject('request')
|
||||||
|
->inject('response')
|
||||||
|
->inject('utopia')
|
||||||
|
->inject('dbForProject')
|
||||||
|
->inject('promiseAdapter')
|
||||||
|
->inject('apiSchema')
|
||||||
|
->action(Closure::fromCallable('graphqlRequest'));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
function graphqlRequest(
|
||||||
|
$query,
|
||||||
|
$operationName,
|
||||||
|
$variables,
|
||||||
|
$request,
|
||||||
|
$response,
|
||||||
|
$utopia,
|
||||||
|
$dbForProject,
|
||||||
|
$promiseAdapter,
|
||||||
|
$apiSchema
|
||||||
|
)
|
||||||
|
{
|
||||||
|
/** @var Appwrite\Utopia\Request $request */
|
||||||
/** @var Appwrite\Utopia\Response $response */
|
/** @var Appwrite\Utopia\Response $response */
|
||||||
/** @var Type\Schema $schema */
|
/** @var Type\Schema $schema */
|
||||||
/** @var Utopia\App $utopia */
|
/** @var Utopia\App $utopia */
|
||||||
/** @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:
|
||||||
|
// https://graphql.org/learn/serving-over-http/#post-request
|
||||||
// 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 = $request->getSwoole()->rawContent();
|
||||||
|
}
|
||||||
|
if (empty($query)) {
|
||||||
|
throw new Exception('Query is empty', Response::STATUS_CODE_BAD_REQUEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
$debugFlags = App::isDevelopment()
|
$debugFlags = App::isDevelopment()
|
||||||
|
@ -66,7 +107,9 @@ App::post('/v1/graphql')
|
||||||
|
|
||||||
$schema = Builder::appendProjectSchema(
|
$schema = Builder::appendProjectSchema(
|
||||||
$apiSchema,
|
$apiSchema,
|
||||||
$register,
|
$utopia,
|
||||||
|
$request,
|
||||||
|
$response,
|
||||||
$dbForProject
|
$dbForProject
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -84,7 +127,13 @@ App::post('/v1/graphql')
|
||||||
$wg->add();
|
$wg->add();
|
||||||
$promise->then(
|
$promise->then(
|
||||||
function ($result) use ($response, $debugFlags, $wg) {
|
function ($result) use ($response, $debugFlags, $wg) {
|
||||||
$response->json(['data' => $result->toArray($debugFlags)]);
|
$result = $result->toArray($debugFlags);
|
||||||
|
if (isset($result['errors'])) {
|
||||||
|
$response->json(['data' => [], ...$result]);
|
||||||
|
$wg->done();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$response->json(['data' => $result]);
|
||||||
$wg->done();
|
$wg->done();
|
||||||
},
|
},
|
||||||
function ($error) use ($response, $wg) {
|
function ($error) use ($response, $wg) {
|
||||||
|
@ -92,8 +141,5 @@ App::post('/v1/graphql')
|
||||||
$wg->done();
|
$wg->done();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
$wg->wait(App::getEnv('_APP_GRAPHQL_REQUEST_TIMEOUT', 30));
|
$wg->wait();
|
||||||
|
}
|
||||||
$time_elapsed_secs = (microtime(true) - $start) * 1000;
|
|
||||||
Console::info("[DEBUG] GraphQL Action Time: {$time_elapsed_secs}ms");
|
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in a new issue