2017-07-14 19:08:47 +07:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Server;
|
|
|
|
|
|
|
|
use GraphQL\Error\InvariantViolation;
|
|
|
|
use GraphQL\Utils\Utils;
|
|
|
|
use GraphQL\Executor\ExecutionResult;
|
|
|
|
use GraphQL\Executor\Promise\Promise;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class StandardServer
|
|
|
|
*
|
|
|
|
* GraphQL server compatible with both:
|
|
|
|
* https://github.com/graphql/express-graphql and https://github.com/apollographql/graphql-server
|
|
|
|
*
|
|
|
|
* @package GraphQL\Server
|
|
|
|
*/
|
|
|
|
class StandardServer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates new server
|
|
|
|
*
|
|
|
|
* @param ServerConfig $config
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function create(ServerConfig $config)
|
|
|
|
{
|
|
|
|
return new static($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ServerConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2017-07-16 18:52:38 +07:00
|
|
|
/**
|
|
|
|
* @var Helper
|
|
|
|
*/
|
|
|
|
private $helper;
|
|
|
|
|
2017-07-14 19:08:47 +07:00
|
|
|
/**
|
|
|
|
* StandardServer constructor.
|
|
|
|
* @param ServerConfig $config
|
|
|
|
*/
|
|
|
|
protected function __construct(ServerConfig $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
2017-07-16 18:52:38 +07:00
|
|
|
$this->helper = new Helper();
|
2017-07-14 19:08:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param OperationParams|OperationParams[] $parsedBody
|
|
|
|
* @return ExecutionResult|ExecutionResult[]|Promise
|
|
|
|
*/
|
|
|
|
public function executeRequest($parsedBody = null)
|
|
|
|
{
|
|
|
|
if (null !== $parsedBody) {
|
2017-07-16 18:52:38 +07:00
|
|
|
$parsedBody = $this->helper->parseHttpRequest();
|
2017-07-14 19:08:47 +07:00
|
|
|
}
|
2017-07-17 16:57:30 +07:00
|
|
|
$this->helper->assertValidRequest($parsedBody);
|
2017-07-14 19:08:47 +07:00
|
|
|
|
|
|
|
$batched = is_array($parsedBody);
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
foreach ((array) $parsedBody as $index => $operationParams) {
|
2017-07-16 18:52:38 +07:00
|
|
|
$result[] = $this->helper->executeOperation($this->config, $operationParams);
|
2017-07-14 19:08:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return $batched ? $result : $result[0];
|
|
|
|
}
|
|
|
|
}
|