From 83cc9132a000ce147c0a1f2d8354d8bceac0f911 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Fri, 18 Aug 2017 02:54:35 +0700 Subject: [PATCH] Server: minor improvements --- src/Server/Helper.php | 5 ++++- src/Server/OperationParams.php | 3 ++- src/Server/ServerConfig.php | 12 ++++++------ src/Server/StandardServer.php | 24 ++++++------------------ tests/Server/ServerConfigTest.php | 14 +++++++------- 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/Server/Helper.php b/src/Server/Helper.php index 145ff4a..af8de39 100644 --- a/src/Server/Helper.php +++ b/src/Server/Helper.php @@ -83,6 +83,9 @@ class Helper private function promiseToExecuteOperation(PromiseAdapter $promiseAdapter, ServerConfig $config, OperationParams $op, $isBatch = false) { try { + if (!$config->getSchema()) { + throw new InvariantViolation("Schema is required for the server"); + } if ($isBatch && !$config->getQueryBatching()) { throw new RequestError("Batched queries are not supported by this server"); } @@ -117,7 +120,7 @@ class Helper $this->resolveContextValue($config, $op, $doc, $operationType), $op->variables, $op->operation, - $config->getDefaultFieldResolver(), + $config->getFieldResolver(), $this->resolveValidationRules($config, $op, $doc, $operationType) ); diff --git a/src/Server/OperationParams.php b/src/Server/OperationParams.php index 34e4943..91301db 100644 --- a/src/Server/OperationParams.php +++ b/src/Server/OperationParams.php @@ -58,6 +58,7 @@ class OperationParams 'query' => null, 'queryid' => null, 'documentid' => null, // alias to queryid + 'id' => null, // alias to queryid 'operation' => null, 'variables' => null ]; @@ -70,7 +71,7 @@ class OperationParams } $instance->query = $params['query']; - $instance->queryId = $params['queryid'] ?: $params['documentid']; + $instance->queryId = $params['queryid'] ?: $params['documentid'] ?: $params['id']; $instance->operation = $params['operation']; $instance->variables = $params['variables']; $instance->readOnly = (bool) $readonly; diff --git a/src/Server/ServerConfig.php b/src/Server/ServerConfig.php index 9bb0dd2..5eb515e 100644 --- a/src/Server/ServerConfig.php +++ b/src/Server/ServerConfig.php @@ -67,7 +67,7 @@ class ServerConfig /** * @var callable */ - private $defaultFieldResolver; + private $fieldResolver; /** * @var PromiseAdapter @@ -223,18 +223,18 @@ class ServerConfig /** * @return callable */ - public function getDefaultFieldResolver() + public function getFieldResolver() { - return $this->defaultFieldResolver; + return $this->fieldResolver; } /** - * @param callable $defaultFieldResolver + * @param callable $fieldResolver * @return $this */ - public function setDefaultFieldResolver(callable $defaultFieldResolver) + public function setFieldResolver(callable $fieldResolver) { - $this->defaultFieldResolver = $defaultFieldResolver; + $this->fieldResolver = $fieldResolver; return $this; } diff --git a/src/Server/StandardServer.php b/src/Server/StandardServer.php index 3b5b184..ed8e135 100644 --- a/src/Server/StandardServer.php +++ b/src/Server/StandardServer.php @@ -19,17 +19,6 @@ use Psr\Http\Message\StreamInterface; */ class StandardServer { - /** - * Creates new server - * - * @param ServerConfig $config - * @return static - */ - public static function create(ServerConfig $config) - { - return new static($config); - } - /** * @var ServerConfig */ @@ -41,10 +30,11 @@ class StandardServer private $helper; /** - * StandardServer constructor. - * @param ServerConfig $config + * Standard GraphQL HTTP server implementation + * + * @param ServerConfig|array $config */ - protected function __construct($config) + public function __construct($config) { if (is_array($config)) { $config = ServerConfig::create($config); @@ -52,9 +42,7 @@ class StandardServer if (!$config instanceof ServerConfig) { throw new InvariantViolation("Expecting valid server config, but got " . Utils::printSafe($config)); } - - $this->config = $config; - $this->helper = new Helper(); + return new static($config); } /** @@ -111,7 +99,7 @@ class StandardServer * @param OperationParams|OperationParams[] $parsedBody * @param bool $exitWhenDone */ - public function processRequest($parsedBody = null, $exitWhenDone = false) + public function handleRequest($parsedBody = null, $exitWhenDone = false) { $result = $this->executeRequest($parsedBody); $this->helper->sendResponse($result, $exitWhenDone); diff --git a/tests/Server/ServerConfigTest.php b/tests/Server/ServerConfigTest.php index b1e83ea..3e2da61 100644 --- a/tests/Server/ServerConfigTest.php +++ b/tests/Server/ServerConfigTest.php @@ -20,7 +20,7 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase $this->assertEquals(null, $config->getErrorsHandler()); $this->assertEquals(null, $config->getPromiseAdapter()); $this->assertEquals(null, $config->getValidationRules()); - $this->assertEquals(null, $config->getDefaultFieldResolver()); + $this->assertEquals(null, $config->getFieldResolver()); $this->assertEquals(null, $config->getPersistentQueryLoader()); $this->assertEquals(false, $config->getDebug()); $this->assertEquals(false, $config->getQueryBatching()); @@ -126,12 +126,12 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase $config = ServerConfig::create(); $resolver = function() {}; - $config->setDefaultFieldResolver($resolver); - $this->assertSame($resolver, $config->getDefaultFieldResolver()); + $config->setFieldResolver($resolver); + $this->assertSame($resolver, $config->getFieldResolver()); $resolver = 'date'; // test for callable - $config->setDefaultFieldResolver($resolver); - $this->assertSame($resolver, $config->getDefaultFieldResolver()); + $config->setFieldResolver($resolver); + $this->assertSame($resolver, $config->getFieldResolver()); } public function testAllowsSettingPersistedQueryLoader() @@ -169,7 +169,7 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase 'errorFormatter' => function() {}, 'promiseAdapter' => new SyncPromiseAdapter(), 'validationRules' => [function() {}], - 'defaultFieldResolver' => function() {}, + 'fieldResolver' => function() {}, 'persistentQueryLoader' => function() {}, 'debug' => true, 'queryBatching' => true, @@ -183,7 +183,7 @@ class ServerConfigTest extends \PHPUnit_Framework_TestCase $this->assertSame($arr['errorFormatter'], $config->getErrorFormatter()); $this->assertSame($arr['promiseAdapter'], $config->getPromiseAdapter()); $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(true, $config->getDebug()); $this->assertSame(true, $config->getQueryBatching());