2020-01-04 10:16:26 +13:00
|
|
|
<?php
|
|
|
|
|
2022-04-07 18:40:28 +12:00
|
|
|
use Appwrite\GraphQL\Builder;
|
2021-02-26 09:43:21 +13:00
|
|
|
use Appwrite\Utopia\Response;
|
2021-03-17 02:34:11 +13:00
|
|
|
use GraphQL\Error\DebugFlag;
|
2022-04-06 01:48:51 +12:00
|
|
|
use GraphQL\GraphQL;
|
|
|
|
use GraphQL\Type;
|
2022-04-07 18:40:28 +12:00
|
|
|
use GraphQL\Validator\Rules\DisableIntrospection;
|
|
|
|
use GraphQL\Validator\Rules\QueryComplexity;
|
|
|
|
use GraphQL\Validator\Rules\QueryDepth;
|
|
|
|
use Swoole\Coroutine\WaitGroup;
|
2020-06-29 05:31:21 +12:00
|
|
|
use Utopia\App;
|
2022-04-07 18:40:28 +12:00
|
|
|
use Utopia\Validator\JSON;
|
|
|
|
use Utopia\Validator\Text;
|
2020-01-04 10:16:26 +13:00
|
|
|
|
2021-02-26 09:43:21 +13:00
|
|
|
App::post('/v1/graphql')
|
|
|
|
->desc('GraphQL Endpoint')
|
2022-04-07 18:39:33 +12:00
|
|
|
->groups(['api', 'grapgql'])
|
2021-03-05 07:40:52 +13:00
|
|
|
->label('scope', 'graphql')
|
2022-04-07 18:39:33 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
|
|
|
->label('sdk.namespace', 'graphql')
|
|
|
|
->label('sdk.method', 'execute')
|
|
|
|
->label('sdk.description', '/docs/references/graphql/execute.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)
|
2022-04-07 18:39:42 +12:00
|
|
|
->label('abuse-time', 60)
|
|
|
|
->param('query', '', new Text(1024), 'The query to execute. Max 1024 chars.')
|
|
|
|
->param('operationName', '', new Text(256), 'Name of the operation to execute', true)
|
|
|
|
->param('variables', [], new JSON(), 'Variables to use in the operation', true)
|
2021-02-26 09:43:21 +13:00
|
|
|
->inject('request')
|
|
|
|
->inject('response')
|
2021-03-11 04:22:19 +13:00
|
|
|
->inject('utopia')
|
|
|
|
->inject('register')
|
2022-04-06 01:48:51 +12:00
|
|
|
->inject('dbForProject')
|
|
|
|
->inject('promiseAdapter')
|
2022-04-07 18:40:28 +12:00
|
|
|
->inject('apiSchema')
|
|
|
|
->action(function ($query, $operationName, $variables, $request, $response, $utopia, $register, $dbForProject, $promiseAdapter, $apiSchema) {
|
2021-03-13 08:00:43 +13:00
|
|
|
/** @var Utopia\Swoole\Request $request */
|
|
|
|
/** @var Appwrite\Utopia\Response $response */
|
|
|
|
/** @var Type\Schema $schema */
|
|
|
|
/** @var Utopia\App $utopia */
|
|
|
|
/** @var Utopia\Registry\Registry $register */
|
2022-04-06 01:48:51 +12:00
|
|
|
/** @var \Utopia\Database\Database $dbForProject */
|
2021-11-25 21:04:39 +13:00
|
|
|
|
2022-04-07 18:40:28 +12:00
|
|
|
if ($request->getHeader('content-type') === 'application/graphql') {
|
|
|
|
$query = \implode("\r\n", $request->getParams());
|
|
|
|
}
|
2021-03-11 02:51:03 +13:00
|
|
|
|
2022-04-07 18:40:28 +12:00
|
|
|
$debugFlags = App::isDevelopment()
|
2022-04-06 01:48:51 +12:00
|
|
|
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
|
|
|
|
: DebugFlag::NONE;
|
|
|
|
|
2022-04-07 18:39:15 +12:00
|
|
|
$validations = array_merge(
|
|
|
|
GraphQL::getStandardValidationRules(),
|
|
|
|
[
|
|
|
|
new QueryComplexity(App::getEnv('_APP_GRAPHQL_MAX_QUERY_COMPLEXITY', 200)),
|
|
|
|
new QueryDepth(App::getEnv('_APP_GRAPHQL_MAX_QUERY_DEPTH', 3)),
|
|
|
|
new DisableIntrospection(),
|
|
|
|
]
|
|
|
|
);
|
2022-04-07 18:40:28 +12:00
|
|
|
|
2022-04-07 18:40:49 +12:00
|
|
|
$schema = Builder::appendProjectSchema(
|
|
|
|
$apiSchema,
|
|
|
|
$register,
|
|
|
|
$dbForProject
|
2022-04-08 02:02:48 +12:00
|
|
|
);
|
2022-04-07 18:40:49 +12:00
|
|
|
|
2022-04-07 18:40:28 +12:00
|
|
|
$promise = GraphQL::promiseToExecute(
|
2022-04-06 01:48:51 +12:00
|
|
|
$promiseAdapter,
|
|
|
|
$schema,
|
|
|
|
$query,
|
2022-04-07 18:40:28 +12:00
|
|
|
variableValues: $variables,
|
|
|
|
operationName: $operationName,
|
2022-04-07 18:39:15 +12:00
|
|
|
validationRules: $validations
|
2022-04-07 18:40:28 +12:00
|
|
|
);
|
|
|
|
|
2022-04-08 02:02:48 +12:00
|
|
|
// Blocking wait while queries resolve asynchronously
|
2022-04-07 18:40:28 +12:00
|
|
|
$wg = new WaitGroup();
|
|
|
|
$wg->add();
|
|
|
|
$promise->then(
|
|
|
|
function ($result) use ($response, $debugFlags, $wg) {
|
2022-04-08 02:02:48 +12:00
|
|
|
$response->json($result->toArray($debugFlags));
|
2022-04-07 18:40:28 +12:00
|
|
|
$wg->done();
|
|
|
|
},
|
|
|
|
function ($error) use ($response, $wg) {
|
2022-04-08 02:02:48 +12:00
|
|
|
$response->text(\json_encode(['errors' => [\json_encode($error)]]));
|
2022-04-07 18:40:28 +12:00
|
|
|
$wg->done();
|
|
|
|
}
|
|
|
|
);
|
2022-04-08 02:02:48 +12:00
|
|
|
$wg->wait(App::getEnv('_APP_GRAPHQL_REQUEST_TIMEOUT', 30));
|
2022-04-06 01:48:51 +12:00
|
|
|
});
|