1
0
Fork 0
mirror of synced 2024-06-28 19:20:25 +12:00
appwrite/app/controllers/api/graphql.php

67 lines
2.1 KiB
PHP
Raw Normal View History

2020-01-04 10:16:26 +13:00
<?php
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\Executor\ExecutionResult;
use GraphQL\GraphQL;
use GraphQL\Type;
2020-06-29 05:31:21 +12:00
use Utopia\App;
2020-01-04 10:16:26 +13:00
App::post('/v1/graphql')
->desc('GraphQL Endpoint')
2021-03-05 07:40:52 +13:00
->label('scope', 'graphql')
->inject('request')
->inject('response')
2021-03-10 20:42:45 +13:00
->inject('schema')
2021-03-11 04:22:19 +13:00
->inject('utopia')
->inject('register')
2022-04-06 01:48:51 +12:00
->inject('dbForProject')
->inject('promiseAdapter')
->middleware(true)
->action(function ($request, $response, $schema, $utopia, $register, $dbForProject, $promiseAdapter) {
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
2021-03-11 02:51:03 +13:00
$query = $request->getPayload('query', '');
2022-04-06 01:48:51 +12:00
$variables = $request->getPayload('variables');
2021-03-11 02:51:03 +13:00
$response->setContentType(Response::CONTENT_TYPE_NULL);
$register->set('__app', function () use ($utopia) {
return $utopia;
});
$register->set('__response', function () use ($response) {
return $response;
});
2021-03-19 08:26:45 +13:00
$isDevelopment = App::isDevelopment();
2022-04-06 01:48:51 +12:00
$debugFlags = $isDevelopment
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
: DebugFlag::NONE;
$rootValue = [];
GraphQL::promiseToExecute(
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-06 01:48:51 +12:00
$promiseAdapter,
$schema,
$query,
$rootValue,
null,
$variables
)->then(function (ExecutionResult $result) use ($response, $debugFlags) {
$response->json($result->toArray($debugFlags));
});
2022-04-07 18:39:15 +12:00
validationRules: $validations
2022-04-06 01:48:51 +12:00
});