2017-07-14 19:08:47 +07:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Server;
|
|
|
|
|
2017-07-19 19:30:39 +07:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2017-07-14 19:08:47 +07:00
|
|
|
use GraphQL\Executor\ExecutionResult;
|
|
|
|
use GraphQL\Executor\Promise\Promise;
|
2017-07-21 22:11:20 +07:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2017-07-19 23:35:22 +07:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2017-07-21 22:11:20 +07:00
|
|
|
use Psr\Http\Message\StreamInterface;
|
2017-07-14 19:08:47 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2017-08-15 18:05:09 +07:00
|
|
|
protected function __construct($config)
|
2017-07-14 19:08:47 +07:00
|
|
|
{
|
2017-08-15 18:05:09 +07:00
|
|
|
if (is_array($config)) {
|
|
|
|
$config = ServerConfig::create($config);
|
|
|
|
}
|
|
|
|
if (!$config instanceof ServerConfig) {
|
|
|
|
throw new InvariantViolation("");
|
|
|
|
}
|
|
|
|
|
2017-07-14 19:08:47 +07:00
|
|
|
$this->config = $config;
|
2017-07-16 18:52:38 +07:00
|
|
|
$this->helper = new Helper();
|
2017-07-14 19:08:47 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-19 23:35:22 +07:00
|
|
|
* Executes GraphQL operation with given server configuration and returns execution result
|
|
|
|
* (or promise when promise adapter is different from SyncPromiseAdapter)
|
|
|
|
*
|
|
|
|
* @param ServerRequestInterface $request
|
|
|
|
* @return ExecutionResult|ExecutionResult[]|Promise
|
|
|
|
*/
|
|
|
|
public function executePsrRequest(ServerRequestInterface $request)
|
|
|
|
{
|
|
|
|
$parsedBody = $this->helper->parsePsrRequest($request);
|
|
|
|
return $this->executeRequest($parsedBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes GraphQL operation with given server configuration and returns execution result
|
|
|
|
* (or promise when promise adapter is different from SyncPromiseAdapter)
|
|
|
|
*
|
2017-07-14 19:08:47 +07:00
|
|
|
* @param OperationParams|OperationParams[] $parsedBody
|
|
|
|
* @return ExecutionResult|ExecutionResult[]|Promise
|
2017-07-19 19:30:39 +07:00
|
|
|
* @throws InvariantViolation
|
2017-07-14 19:08:47 +07:00
|
|
|
*/
|
|
|
|
public function executeRequest($parsedBody = null)
|
|
|
|
{
|
2017-07-25 19:26:41 +07:00
|
|
|
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 23:48:30 +07:00
|
|
|
if (is_array($parsedBody)) {
|
|
|
|
return $this->helper->executeBatch($this->config, $parsedBody);
|
|
|
|
} else {
|
|
|
|
return $this->helper->executeOperation($this->config, $parsedBody);
|
2017-07-14 19:08:47 +07:00
|
|
|
}
|
|
|
|
}
|
2017-07-21 22:11:20 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ServerRequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
* @param StreamInterface $writableBodyStream
|
|
|
|
* @return ResponseInterface|Promise
|
|
|
|
*/
|
|
|
|
public function processPsrRequest(
|
|
|
|
ServerRequestInterface $request,
|
|
|
|
ResponseInterface $response,
|
|
|
|
StreamInterface $writableBodyStream
|
|
|
|
)
|
|
|
|
{
|
|
|
|
$result = $this->executePsrRequest($request);
|
|
|
|
return $this->helper->toPsrResponse($result, $response, $writableBodyStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param OperationParams|OperationParams[] $parsedBody
|
|
|
|
* @param bool $exitWhenDone
|
|
|
|
*/
|
|
|
|
public function processRequest($parsedBody = null, $exitWhenDone = false)
|
|
|
|
{
|
|
|
|
$result = $this->executeRequest($parsedBody);
|
|
|
|
$this->helper->sendResponse($result, $exitWhenDone);
|
|
|
|
}
|
2017-07-14 19:08:47 +07:00
|
|
|
}
|