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

153 lines
5 KiB
PHP
Raw Normal View History

2020-01-04 10:16:26 +13:00
<?php
2022-04-26 19:49:36 +12:00
use Appwrite\Extend\Exception;
2022-07-13 21:34:56 +12:00
use Appwrite\GraphQL\Promises\CoroutinePromiseAdapter;
use Appwrite\Utopia\Response;
use GraphQL\Error\DebugFlag;
2022-04-06 01:48:51 +12:00
use GraphQL\GraphQL;
use GraphQL\Type;
2022-04-20 22:30:48 +12:00
use GraphQL\Validator\Rules\DisableIntrospection;
2022-04-07 18:40:28 +12:00
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;
2020-01-04 10:16:26 +13:00
2022-04-11 23:08:57 +12:00
App::get('/v1/graphql')
->desc('GraphQL Endpoint')
->groups(['grapgql'])
2021-03-05 07:40:52 +13:00
->label('scope', 'graphql')
2022-04-11 23:08:57 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT])
2022-04-07 18:39:33 +12:00
->label('sdk.namespace', 'graphql')
2022-04-11 23:08:57 +12:00
->label('sdk.method', 'query')
->label('sdk.description', '/docs/references/graphql/query.md')
2022-04-07 18:39:33 +12:00
->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)
2022-07-13 17:06:48 +12:00
->param('query', '', new JSON(), 'The query or queries to execute.', fullBody: true)
->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')
->inject('gqlSchema')
2022-04-11 23:08:57 +12:00
->action(Closure::fromCallable('graphqlRequest'));
App::post('/v1/graphql')
->desc('GraphQL Endpoint')
->groups(['grapgql'])
2022-04-11 23:08:57 +12:00
->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)
2022-07-13 17:06:48 +12:00
->param('query', '', new JSON(), 'The query or queries to execute.', fullBody: true)
2022-04-11 23:08:57 +12:00
->inject('request')
->inject('response')
->inject('promiseAdapter')
->inject('gqlSchema')
2022-04-11 23:08:57 +12:00
->action(Closure::fromCallable('graphqlRequest'));
2021-11-25 21:04:39 +13:00
2022-07-07 18:13:12 +12:00
/**
* @throws Exception
* @throws \Exception
*/
function graphqlRequest(
2022-07-13 15:49:59 +12:00
array $query,
2022-07-07 18:13:12 +12:00
Appwrite\Utopia\Request $request,
Appwrite\Utopia\Response $response,
CoroutinePromiseAdapter $promiseAdapter,
Type\Schema $gqlSchema
): void {
$contentType = $request->getHeader('content-type');
2022-07-07 19:39:42 +12:00
2022-07-07 18:13:12 +12:00
if ($contentType === 'application/graphql') {
2022-07-13 15:49:59 +12:00
$query = [ 'query' => $request->getSwoole()->rawContent() ];
}
if (!isset($query[0])) {
$query = [$query];
2022-07-07 18:13:12 +12:00
}
2022-07-07 19:39:42 +12:00
if (\str_starts_with($contentType, 'multipart/form-data')) {
2022-07-13 15:49:59 +12:00
$operations = \json_decode($query[0]['operations'], true);
$map = \json_decode($query[0]['map'], true);
2022-07-07 19:39:42 +12:00
foreach ($map as $fileKey => $locations) {
foreach ($locations as $location) {
$items = &$operations;
2022-07-13 15:49:59 +12:00
foreach (\explode('.', $location) as $key) {
if (!isset($items[$key]) || !\is_array($items[$key])) {
2022-07-07 19:39:42 +12:00
$items[$key] = [];
}
$items = &$items[$key];
}
$items = $request->getFiles($fileKey);
}
}
2022-07-13 15:49:59 +12:00
$query[0]['query'] = $operations['query'];
$query[0]['variables'] = $operations['variables'];
2022-07-07 19:39:42 +12:00
}
2022-04-11 23:08:57 +12:00
if (empty($query)) {
2022-05-02 20:21:40 +12:00
throw new Exception('No query supplied.', 400, Exception::GRAPHQL_NO_QUERY);
2022-04-11 23:08:57 +12:00
}
2022-07-13 17:21:41 +12:00
2022-04-26 19:49:36 +12:00
$maxComplexity = App::getEnv('_APP_GRAPHQL_MAX_QUERY_COMPLEXITY', 200);
$maxDepth = App::getEnv('_APP_GRAPHQL_MAX_QUERY_DEPTH', 3);
$validations = GraphQL::getStandardValidationRules();
$validations[] = new QueryComplexity($maxComplexity);
$validations[] = new QueryDepth($maxDepth);
if (App::isProduction()) {
$validations[] = new DisableIntrospection();
$debugFlags = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE;
} else {
$debugFlags = DebugFlag::NONE;
}
2022-07-13 17:21:41 +12:00
2022-07-13 15:49:59 +12:00
$promises = [];
2022-07-13 17:21:41 +12:00
foreach ($query as $indexed) {
2022-07-13 15:49:59 +12:00
$promises[] = GraphQL::promiseToExecute(
$promiseAdapter,
$gqlSchema,
$indexed['query'],
2022-07-13 17:06:48 +12:00
variableValues: $indexed['variables'] ?? null,
operationName: $indexed['operationName'] ?? null,
2022-07-13 15:49:59 +12:00
validationRules: $validations
);
}
2022-04-07 18:40:28 +12:00
2022-05-02 20:21:40 +12:00
$output = [];
2022-04-11 23:08:57 +12:00
$wg = new WaitGroup();
$wg->add();
2022-07-13 15:49:59 +12:00
$promiseAdapter->all($promises)->then(
function ($result) use ($response, &$output, $wg, $debugFlags) {
2022-07-13 15:49:59 +12:00
foreach ($result as $queryResponse) {
$output[] = $queryResponse->toArray($debugFlags);
}
if (isset($output[1])) {
$output = \array_merge_recursive(...$output);
} else {
$output = $output[0];
}
2022-04-26 19:49:36 +12:00
$wg->done();
},
2022-05-02 20:21:40 +12:00
function ($error) use ($response, &$output, $wg) {
$output = ['errors' => [$error]];
2022-04-11 23:08:57 +12:00
$wg->done();
}
2022-04-26 19:49:36 +12:00
);
2022-04-11 23:08:57 +12:00
$wg->wait();
2022-05-02 20:21:40 +12:00
$response->json($output);
2022-04-11 23:08:57 +12:00
}