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

135 lines
4.3 KiB
PHP
Raw Normal View History

2020-01-04 10:16:26 +13:00
<?php
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;
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
*/
2020-06-29 05:31:21 +12:00
App::post('/v1/graphql')
2020-01-04 10:16:26 +13:00
->desc('GraphQL Endpoint')
2020-06-26 06:32:12 +12:00
->groups(['api', 'graphql'])
2020-01-04 10:16:26 +13:00
->label('scope', 'public')
->action(
2020-06-25 00:55:05 +12:00
function () use ($request, $response, $utopia) {
foreach ($utopia->getRoutes() as $method => $routes) {
var_dump($method);
foreach ($routes as $key => $route) {
var_dump($key);
}
}
2020-06-23 00:04:19 +12:00
$userType = new ObjectType([
'name' => 'User',
'fields' => [
'name' => [
'type' => Type::string(),
],
],
]);
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'echo' => [
'type' => Type::string(),
'args' => [
'message' => ['type' => Type::string()],
],
'resolve' => function ($rootValue, $args) {
return $rootValue['prefix'] . $args['message'];
}
],
'users' => [
'type' => Type::listOf($userType),
//'type' => $userType,
'args' => [
'message' => ['type' => Type::string()],
],
'resolve' => function ($rootValue, $args) {
return ['name' => 'Eldad Fux'];
return [
['name' => 'Eldad Fux'],
['name' => 'Sharon Kapon'],
];
}
],
],
]);
$mutationType = new ObjectType([
'name' => 'Mutation',
'fields' => [
'sum' => [
'type' => Type::int(),
'args' => [
'x' => ['type' => Type::int()],
'y' => ['type' => Type::int()],
],
'resolve' => function ($calc, $args) {
return $args['x'] + $args['y'];
},
],
],
]);
$schema = new Schema([
'query' => $queryType,
'mutation' => $mutationType,
]);
$query = $request->getPayload('query', '');
$variables = $request->getPayload('variables', 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(),
]
]
];
}
$response->json($output);
echo "\n"; //TODO REMOVE THIS
2020-01-04 10:16:26 +13:00
}
2020-06-23 00:04:19 +12:00
);