appwrite/app/controllers/api/graphql.php

62 lines
2.1 KiB
PHP
Raw Normal View History

2020-01-03 21:16:26 +00:00
<?php
2021-03-18 18:55:43 +00:00
use Appwrite\GraphQL\Builder;
2020-06-22 12:04:19 +00:00
use GraphQL\GraphQL;
2021-03-12 19:00:43 +00:00
use GraphQL\Type;
use Appwrite\Utopia\Response;
2021-03-16 13:34:11 +00:00
use GraphQL\Error\DebugFlag;
2020-06-28 17:31:21 +00:00
use Utopia\App;
2020-01-03 21:16:26 +00:00
2021-03-04 18:40:52 +00:00
App::post('/v1/graphql')
->desc('GraphQL Endpoint')
2021-03-04 18:40:52 +00:00
->label('scope', 'graphql')
->inject('request')
->inject('response')
2021-03-10 07:42:45 +00:00
->inject('schema')
2021-03-10 15:22:19 +00:00
->inject('utopia')
->inject('register')
->middleware(true)
2021-03-10 15:22:19 +00:00
->action(function ($request, $response, $schema, $utopia, $register) {
2021-03-12 19:00:43 +00:00
/** @var Utopia\Swoole\Request $request */
/** @var Appwrite\Utopia\Response $response */
/** @var Type\Schema $schema */
/** @var Utopia\App $utopia */
/** @var Utopia\Registry\Registry $register */
2021-03-04 18:40:52 +00:00
2021-03-10 13:51:03 +00:00
$query = $request->getPayload('query', '');
$variables = $request->getPayload('variables', null);
$response->setContentType(Response::CONTENT_TYPE_NULL);
2021-03-10 15:22:19 +00:00
$register->set('__app', function() use ($utopia) {
return $utopia;
});
$register->set('__response', function() use ($response) {
return $response;
});
2021-03-10 13:51:03 +00:00
2021-03-18 19:26:45 +00:00
$isDevelopment = App::isDevelopment();
$version = App::getEnv('_APP_VERSION', 'UNKNOWN');
2021-03-10 13:51:03 +00:00
try {
2021-03-18 19:26:45 +00:00
$debug = $isDevelopment ? ( DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE ) : DebugFlag::NONE;
2021-03-10 13:51:03 +00:00
$rootValue = [];
2021-03-17 20:45:47 +00:00
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables)
2021-03-18 19:26:45 +00:00
->setErrorFormatter(Builder::getErrorFormatter($isDevelopment, $version));
2021-03-16 13:34:11 +00:00
$output = $result->toArray($debug);
2021-03-10 13:51:03 +00:00
} catch (\Exception $error) {
$output = [
'errors' => [
[
'message' => $error->getMessage().'xxx',
'code' => $error->getCode(),
'file' => $error->getFile(),
'line' => $error->getLine(),
'trace' => $error->getTrace(),
2020-06-22 12:04:19 +00:00
]
2021-03-10 13:51:03 +00:00
]
];
}
$response->json($output);
2021-03-17 20:45:47 +00:00
}
);