appwrite/app/controllers/api/graphql.php

58 lines
1.7 KiB
PHP
Raw Normal View History

2020-01-03 21:16:26 +00:00
<?php
use Appwrite\Utopia\Response;
2021-03-16 13:34:11 +00:00
use GraphQL\Error\DebugFlag;
2022-04-05 13:48:51 +00:00
use GraphQL\Executor\ExecutionResult;
use GraphQL\GraphQL;
use GraphQL\Type;
2020-06-28 17:31:21 +00:00
use Utopia\App;
2020-01-03 21:16:26 +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')
2022-04-05 13:48:51 +00:00
->inject('dbForProject')
->inject('promiseAdapter')
->middleware(true)
->action(function ($request, $response, $schema, $utopia, $register, $dbForProject, $promiseAdapter) {
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 */
2022-04-05 13:48:51 +00:00
/** @var \Utopia\Database\Database $dbForProject */
2021-11-25 08:04:39 +00:00
2021-03-10 13:51:03 +00:00
$query = $request->getPayload('query', '');
2022-04-05 13:48:51 +00:00
$variables = $request->getPayload('variables');
2021-03-10 13:51:03 +00: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-18 19:26:45 +00:00
$isDevelopment = App::isDevelopment();
2022-04-05 13:48:51 +00:00
$debugFlags = $isDevelopment
? DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::INCLUDE_TRACE
: DebugFlag::NONE;
$rootValue = [];
GraphQL::promiseToExecute(
$promiseAdapter,
$schema,
$query,
$rootValue,
null,
$variables
)->then(function (ExecutionResult $result) use ($response, $debugFlags) {
$response->json($result->toArray($debugFlags));
});
});