Server: minor improvements

This commit is contained in:
Vladimir Razuvaev 2017-08-18 02:54:35 +07:00
parent d578b8a22f
commit 83cc9132a0
5 changed files with 25 additions and 33 deletions

View file

@ -83,6 +83,9 @@ class Helper
private function promiseToExecuteOperation(PromiseAdapter $promiseAdapter, ServerConfig $config, OperationParams $op, $isBatch = false) private function promiseToExecuteOperation(PromiseAdapter $promiseAdapter, ServerConfig $config, OperationParams $op, $isBatch = false)
{ {
try { try {
if (!$config->getSchema()) {
throw new InvariantViolation("Schema is required for the server");
}
if ($isBatch && !$config->getQueryBatching()) { if ($isBatch && !$config->getQueryBatching()) {
throw new RequestError("Batched queries are not supported by this server"); throw new RequestError("Batched queries are not supported by this server");
} }
@ -117,7 +120,7 @@ class Helper
$this->resolveContextValue($config, $op, $doc, $operationType), $this->resolveContextValue($config, $op, $doc, $operationType),
$op->variables, $op->variables,
$op->operation, $op->operation,
$config->getDefaultFieldResolver(), $config->getFieldResolver(),
$this->resolveValidationRules($config, $op, $doc, $operationType) $this->resolveValidationRules($config, $op, $doc, $operationType)
); );

View file

@ -58,6 +58,7 @@ class OperationParams
'query' => null, 'query' => null,
'queryid' => null, 'queryid' => null,
'documentid' => null, // alias to queryid 'documentid' => null, // alias to queryid
'id' => null, // alias to queryid
'operation' => null, 'operation' => null,
'variables' => null 'variables' => null
]; ];
@ -70,7 +71,7 @@ class OperationParams
} }
$instance->query = $params['query']; $instance->query = $params['query'];
$instance->queryId = $params['queryid'] ?: $params['documentid']; $instance->queryId = $params['queryid'] ?: $params['documentid'] ?: $params['id'];
$instance->operation = $params['operation']; $instance->operation = $params['operation'];
$instance->variables = $params['variables']; $instance->variables = $params['variables'];
$instance->readOnly = (bool) $readonly; $instance->readOnly = (bool) $readonly;

View file

@ -67,7 +67,7 @@ class ServerConfig
/** /**
* @var callable * @var callable
*/ */
private $defaultFieldResolver; private $fieldResolver;
/** /**
* @var PromiseAdapter * @var PromiseAdapter
@ -223,18 +223,18 @@ class ServerConfig
/** /**
* @return callable * @return callable
*/ */
public function getDefaultFieldResolver() public function getFieldResolver()
{ {
return $this->defaultFieldResolver; return $this->fieldResolver;
} }
/** /**
* @param callable $defaultFieldResolver * @param callable $fieldResolver
* @return $this * @return $this
*/ */
public function setDefaultFieldResolver(callable $defaultFieldResolver) public function setFieldResolver(callable $fieldResolver)
{ {
$this->defaultFieldResolver = $defaultFieldResolver; $this->fieldResolver = $fieldResolver;
return $this; return $this;
} }

View file

@ -19,17 +19,6 @@ use Psr\Http\Message\StreamInterface;
*/ */
class StandardServer class StandardServer
{ {
/**
* Creates new server
*
* @param ServerConfig $config
* @return static
*/
public static function create(ServerConfig $config)
{
return new static($config);
}
/** /**
* @var ServerConfig * @var ServerConfig
*/ */
@ -41,10 +30,11 @@ class StandardServer
private $helper; private $helper;
/** /**
* StandardServer constructor. * Standard GraphQL HTTP server implementation
* @param ServerConfig $config *
* @param ServerConfig|array $config
*/ */
protected function __construct($config) public function __construct($config)
{ {
if (is_array($config)) { if (is_array($config)) {
$config = ServerConfig::create($config); $config = ServerConfig::create($config);
@ -52,9 +42,7 @@ class StandardServer
if (!$config instanceof ServerConfig) { if (!$config instanceof ServerConfig) {
throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config)); throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config));
} }
return new static($config);
$this->config = $config;
$this->helper = new Helper();
} }
/** /**
@ -111,7 +99,7 @@ class StandardServer
* @param OperationParams|OperationParams[] $parsedBody * @param OperationParams|OperationParams[] $parsedBody
* @param bool $exitWhenDone * @param bool $exitWhenDone
*/ */
public function processRequest($parsedBody = null, $exitWhenDone = false) public function handleRequest($parsedBody = null, $exitWhenDone = false)
{ {
$result = $this->executeRequest($parsedBody); $result = $this->executeRequest($parsedBody);
$this->helper->sendResponse($result, $exitWhenDone); $this->helper->sendResponse($result, $exitWhenDone);

View file

@ -20,7 +20,7 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(null, $config->getErrorsHandler()); $this->assertEquals(null, $config->getErrorsHandler());
$this->assertEquals(null, $config->getPromiseAdapter()); $this->assertEquals(null, $config->getPromiseAdapter());
$this->assertEquals(null, $config->getValidationRules()); $this->assertEquals(null, $config->getValidationRules());
$this->assertEquals(null, $config->getDefaultFieldResolver()); $this->assertEquals(null, $config->getFieldResolver());
$this->assertEquals(null, $config->getPersistentQueryLoader()); $this->assertEquals(null, $config->getPersistentQueryLoader());
$this->assertEquals(false, $config->getDebug()); $this->assertEquals(false, $config->getDebug());
$this->assertEquals(false, $config->getQueryBatching()); $this->assertEquals(false, $config->getQueryBatching());
@ -126,12 +126,12 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase
$config = ServerConfig::create(); $config = ServerConfig::create();
$resolver = function() {}; $resolver = function() {};
$config->setDefaultFieldResolver($resolver); $config->setFieldResolver($resolver);
$this->assertSame($resolver, $config->getDefaultFieldResolver()); $this->assertSame($resolver, $config->getFieldResolver());
$resolver = 'date'; // test for callable $resolver = 'date'; // test for callable
$config->setDefaultFieldResolver($resolver); $config->setFieldResolver($resolver);
$this->assertSame($resolver, $config->getDefaultFieldResolver()); $this->assertSame($resolver, $config->getFieldResolver());
} }
public function testAllowsSettingPersistedQueryLoader() public function testAllowsSettingPersistedQueryLoader()
@ -169,7 +169,7 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase
'errorFormatter' => function() {}, 'errorFormatter' => function() {},
'promiseAdapter' => new SyncPromiseAdapter(), 'promiseAdapter' => new SyncPromiseAdapter(),
'validationRules' => [function() {}], 'validationRules' => [function() {}],
'defaultFieldResolver' => function() {}, 'fieldResolver' => function() {},
'persistentQueryLoader' => function() {}, 'persistentQueryLoader' => function() {},
'debug' => true, 'debug' => true,
'queryBatching' => true, 'queryBatching' => true,
@ -183,7 +183,7 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase
$this->assertSame($arr['errorFormatter'], $config->getErrorFormatter()); $this->assertSame($arr['errorFormatter'], $config->getErrorFormatter());
$this->assertSame($arr['promiseAdapter'], $config->getPromiseAdapter()); $this->assertSame($arr['promiseAdapter'], $config->getPromiseAdapter());
$this->assertSame($arr['validationRules'], $config->getValidationRules()); $this->assertSame($arr['validationRules'], $config->getValidationRules());
$this->assertSame($arr['defaultFieldResolver'], $config->getDefaultFieldResolver()); $this->assertSame($arr['fieldResolver'], $config->getFieldResolver());
$this->assertSame($arr['persistentQueryLoader'], $config->getPersistentQueryLoader()); $this->assertSame($arr['persistentQueryLoader'], $config->getPersistentQueryLoader());
$this->assertSame(true, $config->getDebug()); $this->assertSame(true, $config->getDebug());
$this->assertSame(true, $config->getQueryBatching()); $this->assertSame(true, $config->getQueryBatching());