1
0
Fork 0
mirror of synced 2024-10-02 18:26:49 +13:00
appwrite/app/controllers/api/graphql.php

105 lines
2.9 KiB
PHP
Raw Normal View History

2020-01-04 10:16:26 +13:00
<?php
2021-03-10 20:42:45 +13:00
use Appwrite\GraphQL\GraphQLBuilder;
2020-06-23 00:04:19 +12:00
use GraphQL\GraphQL;
use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
2021-03-10 20:42:45 +13:00
use Appwrite\GraphQL\Types\JsonType;
2021-03-11 02:51:03 +13:00
use GraphQL\Error\Error;
2021-03-05 07:40:52 +13:00
use GraphQL\Error\ClientAware;
2021-03-11 02:51:03 +13:00
use GraphQL\Error\FormattedError;
2021-03-09 03:43:28 +13:00
use GraphQL\Type\Definition\ListOfType;
2020-06-29 05:31:21 +12:00
use Utopia\App;
2020-01-04 10:16:26 +13:00
/**
* TODO:
* 1. Map all objects, object-params, object-fields
* 2. Parse GraphQL request payload (use: https://github.com/webonyx/graphql-php)
* 3. Route request to relevant controllers (of REST API?) / resolvers and aggergate data
2021-01-17 19:36:11 +13:00
* 4. Handle scope authentication
* 5. Handle errors
* 6. Return response
* 7. Write tests!
2020-06-23 00:04:19 +12:00
*
* Demo
* curl -H "Content-Type: application/json" http://localhost/v1/graphql -d '{"query": "query { echo(message: \"Hello World\") }" }'
*
* Explorers:
* - https://shopify.dev/tools/graphiql-admin-api
* - https://developer.github.com/v4/explorer/
* - http://localhost:4000
*
* Docs
* - Overview
2020-06-24 16:08:10 +12:00
* - Clients
*
* - Queries
* - Mutations
*
2020-06-23 00:04:19 +12:00
* - Objects
2020-01-04 10:16:26 +13:00
*/
2021-03-05 07:40:52 +13:00
class MySafeException extends \Exception implements ClientAware
{
public function isClientSafe()
{
return true;
}
public function getCategory()
{
return 'businessLogic';
}
}
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')
2021-03-11 02:51:03 +13:00
->middleware(false)
2021-03-11 04:22:19 +13:00
->action(function ($request, $response, $schema, $utopia, $register) {
2021-03-05 07:40:52 +13:00
2021-03-11 02:51:03 +13:00
// $myErrorFormatter = function(Error $error) {
// $formattedError = FormattedError::createFromException($error);
// var_dump("***** IN ERROR FORMATTER ******");
// return $formattedError;
// };
2021-03-11 02:51:03 +13:00
$query = $request->getPayload('query', '');
$variables = $request->getPayload('variables', null);
$response->setContentType(Response::CONTENT_TYPE_NULL);
2021-03-11 04:22:19 +13:00
$register->set('__app', function() use ($utopia) {
return $utopia;
});
$register->set('__response', function() use ($response) {
return $response;
});
2021-03-11 02:51:03 +13:00
try {
$rootValue = [];
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variables);
$output = $result->toArray();
} catch (\Exception $error) {
$output = [
'errors' => [
[
'message' => $error->getMessage().'xxx',
'code' => $error->getCode(),
'file' => $error->getFile(),
'line' => $error->getLine(),
'trace' => $error->getTrace(),
2020-06-23 00:04:19 +12:00
]
2021-03-11 02:51:03 +13:00
]
];
}
$response->json($output);
2020-01-04 10:16:26 +13:00
}
2021-03-05 07:40:52 +13:00
);