appwrite/app/controllers/api/graphql.php

97 lines
2.6 KiB
PHP
Raw Normal View History

2020-01-03 21:16:26 +00:00
<?php
2021-03-10 07:42:45 +00:00
use Appwrite\GraphQL\GraphQLBuilder;
2020-06-22 12:04:19 +00: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 07:42:45 +00:00
use Appwrite\GraphQL\Types\JsonType;
2021-03-10 13:51:03 +00:00
use GraphQL\Error\Error;
2021-03-04 18:40:52 +00:00
use GraphQL\Error\ClientAware;
2021-03-10 13:51:03 +00:00
use GraphQL\Error\FormattedError;
2021-03-08 14:43:28 +00:00
use GraphQL\Type\Definition\ListOfType;
2020-06-28 17:31:21 +00:00
use Utopia\App;
2020-01-03 21:16:26 +00: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 06:36:11 +00:00
* 4. Handle scope authentication
* 5. Handle errors
* 6. Return response
* 7. Write tests!
2020-06-22 12:04:19 +00: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 04:08:10 +00:00
* - Clients
*
* - Queries
* - Mutations
*
2020-06-22 12:04:19 +00:00
* - Objects
2020-01-03 21:16:26 +00:00
*/
2021-03-04 18:40:52 +00: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-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 13:51:03 +00:00
->middleware(false)
2021-03-10 07:42:45 +00:00
->action(function ($request, $response, $schema) {
2021-03-04 18:40:52 +00:00
2021-03-10 13:51:03 +00:00
// $myErrorFormatter = function(Error $error) {
// $formattedError = FormattedError::createFromException($error);
// var_dump("***** IN ERROR FORMATTER ******");
// return $formattedError;
// };
2021-03-10 13:51:03 +00:00
$query = $request->getPayload('query', '');
$variables = $request->getPayload('variables', null);
$response->setContentType(Response::CONTENT_TYPE_NULL);
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-22 12:04:19 +00:00
]
2021-03-10 13:51:03 +00:00
]
];
}
$response->json($output);
2020-01-03 21:16:26 +00:00
}
2021-03-04 18:40:52 +00:00
);