2020-01-04 10:16:26 +13:00
|
|
|
<?php
|
2020-06-23 00:04:19 +12:00
|
|
|
ini_set('display_errors', 1);
|
|
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
error_reporting(E_ALL);
|
2020-01-04 10:16:26 +13:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
global $utopia, $request, $response;
|
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
|
|
|
|
* 4. Handle errors if any
|
|
|
|
* 5. Returen JSON response
|
|
|
|
* 6. 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
|
|
|
*/
|
|
|
|
|
|
|
|
$utopia->post('/v1/graphql')
|
|
|
|
->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
|
|
|
);
|