2017-08-19 22:33:16 +03:00
|
|
|
<?php
|
|
|
|
// Test this using following command
|
|
|
|
// php -S localhost:8080 ./graphql.php &
|
|
|
|
// curl http://localhost:8080 -d '{"query": "query { echo(message: \"Hello World\") }" }'
|
|
|
|
// curl http://localhost:8080 -d '{"query": "mutation { sum(x: 2, y: 2) }" }'
|
|
|
|
require_once __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Type\Schema;
|
|
|
|
use GraphQL\Server\StandardServer;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$queryType = new ObjectType([
|
|
|
|
'name' => 'Query',
|
|
|
|
'fields' => [
|
|
|
|
'echo' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
|
|
|
'message' => ['type' => Type::string()],
|
|
|
|
],
|
2019-06-30 21:54:56 +03:00
|
|
|
'resolve' => function ($rootValue, $args) {
|
|
|
|
return $rootValue['prefix'] . $args['message'];
|
2017-08-19 22:33:16 +03:00
|
|
|
}
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$mutationType = new ObjectType([
|
|
|
|
'name' => 'Calc',
|
|
|
|
'fields' => [
|
|
|
|
'sum' => [
|
|
|
|
'type' => Type::int(),
|
|
|
|
'args' => [
|
|
|
|
'x' => ['type' => Type::int()],
|
|
|
|
'y' => ['type' => Type::int()],
|
|
|
|
],
|
2019-07-01 13:12:01 +03:00
|
|
|
'resolve' => function ($calc, $args) {
|
2017-08-19 22:33:16 +03:00
|
|
|
return $args['x'] + $args['y'];
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// See docs on schema options:
|
|
|
|
// http://webonyx.github.io/graphql-php/type-system/schema/#configuration-options
|
|
|
|
$schema = new Schema([
|
|
|
|
'query' => $queryType,
|
|
|
|
'mutation' => $mutationType,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// See docs on server options:
|
|
|
|
// http://webonyx.github.io/graphql-php/executing-queries/#server-configuration-options
|
|
|
|
$server = new StandardServer([
|
|
|
|
'schema' => $schema
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server->handleRequest();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
StandardServer::send500Error($e);
|
|
|
|
}
|