2017-07-14 15:08:47 +03:00
|
|
|
<?php
|
2018-07-10 01:19:15 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
namespace GraphQL\Server;
|
|
|
|
|
|
|
|
use GraphQL\Error\InvariantViolation;
|
|
|
|
use GraphQL\Executor\Promise\PromiseAdapter;
|
2017-08-12 17:32:07 +03:00
|
|
|
use GraphQL\Type\Schema;
|
2017-07-14 15:08:47 +03:00
|
|
|
use GraphQL\Utils\Utils;
|
2018-08-07 01:35:37 +03:00
|
|
|
use GraphQL\Validator\Rules\ValidationRule;
|
2018-07-10 01:19:15 +03:00
|
|
|
use function is_array;
|
|
|
|
use function is_callable;
|
|
|
|
use function method_exists;
|
|
|
|
use function sprintf;
|
|
|
|
use function ucfirst;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
/**
|
|
|
|
* Server configuration class.
|
|
|
|
* Could be passed directly to server constructor. List of options accepted by **create** method is
|
2017-08-19 22:33:31 +03:00
|
|
|
* [described in docs](executing-queries.md#server-configuration-options).
|
2017-08-19 19:01:46 +03:00
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
*
|
|
|
|
* $config = GraphQL\Server\ServerConfig::create()
|
|
|
|
* ->setSchema($mySchema)
|
|
|
|
* ->setContext($myContext);
|
|
|
|
*
|
|
|
|
* $server = new GraphQL\Server\StandardServer($config);
|
|
|
|
*/
|
2017-07-14 15:08:47 +03:00
|
|
|
class ServerConfig
|
|
|
|
{
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Converts an array of options to instance of ServerConfig
|
|
|
|
* (or just returns empty config when array is not passed).
|
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @param mixed[] $config
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return ServerConfig
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-15 14:05:09 +03:00
|
|
|
public static function create(array $config = [])
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-15 14:05:09 +03:00
|
|
|
$instance = new static();
|
|
|
|
foreach ($config as $key => $value) {
|
|
|
|
$method = 'set' . ucfirst($key);
|
2018-07-10 01:19:15 +03:00
|
|
|
if (! method_exists($instance, $method)) {
|
|
|
|
throw new InvariantViolation(sprintf('Unknown server config option "%s"', $key));
|
2017-08-15 14:05:09 +03:00
|
|
|
}
|
|
|
|
$instance->$method($value);
|
|
|
|
}
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-15 14:05:09 +03:00
|
|
|
return $instance;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var Schema */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $schema;
|
|
|
|
|
2018-09-26 11:35:23 +03:00
|
|
|
/** @var mixed|callable */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $context;
|
|
|
|
|
2018-09-26 11:35:23 +03:00
|
|
|
/** @var mixed|callable */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $rootValue;
|
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var callable|null */
|
2017-07-21 18:11:20 +03:00
|
|
|
private $errorFormatter;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var callable|null */
|
2017-08-17 12:42:28 +03:00
|
|
|
private $errorsHandler;
|
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var bool */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $debug = false;
|
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var bool */
|
2017-08-15 14:05:09 +03:00
|
|
|
private $queryBatching = false;
|
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var ValidationRule[]|callable */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $validationRules;
|
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var callable */
|
2017-08-17 22:54:35 +03:00
|
|
|
private $fieldResolver;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var PromiseAdapter */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $promiseAdapter;
|
|
|
|
|
2018-07-10 01:19:15 +03:00
|
|
|
/** @var callable */
|
2017-07-14 15:08:47 +03:00
|
|
|
private $persistentQueryLoader;
|
|
|
|
|
|
|
|
/**
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setSchema(Schema $schema)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->schema = $schema;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-26 11:35:23 +03:00
|
|
|
* @param mixed|callable $context
|
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
public function setContext($context)
|
|
|
|
{
|
|
|
|
$this->context = $context;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-26 11:35:23 +03:00
|
|
|
* @param mixed|callable $rootValue
|
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
public function setRootValue($rootValue)
|
|
|
|
{
|
|
|
|
$this->rootValue = $rootValue;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Expects function(Throwable $e) : array
|
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setErrorFormatter(callable $errorFormatter)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->errorFormatter = $errorFormatter;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Expects function(array $errors, callable $formatter) : array
|
2017-07-14 15:08:47 +03:00
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setErrorsHandler(callable $handler)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->errorsHandler = $handler;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Set validation rules for this server.
|
|
|
|
*
|
2018-08-07 01:35:37 +03:00
|
|
|
* @param ValidationRule[]|callable $validationRules
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setValidationRules($validationRules)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2018-07-10 01:19:15 +03:00
|
|
|
if (! is_callable($validationRules) && ! is_array($validationRules) && $validationRules !== null) {
|
2017-08-19 19:01:46 +03:00
|
|
|
throw new InvariantViolation(
|
|
|
|
'Server config expects array of validation rules or callable returning such array, but got ' .
|
|
|
|
Utils::printSafe($validationRules)
|
|
|
|
);
|
|
|
|
}
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->validationRules = $validationRules;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setFieldResolver(callable $fieldResolver)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->fieldResolver = $fieldResolver;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:42:28 +03:00
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Expects function($queryId, OperationParams $params) : string|DocumentNode
|
2017-08-17 12:42:28 +03:00
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* This function must return query string or valid DocumentNode.
|
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-08-17 12:42:28 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setPersistentQueryLoader(callable $persistentQueryLoader)
|
2017-08-17 12:42:28 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->persistentQueryLoader = $persistentQueryLoader;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-17 12:42:28 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Set response debug flags. See GraphQL\Error\Debug class for a list of all available flags
|
|
|
|
*
|
|
|
|
* @param bool|int $set
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-08-17 12:42:28 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setDebug($set = true)
|
2017-08-17 12:42:28 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->debug = $set;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this;
|
2017-08-17 12:42:28 +03:00
|
|
|
}
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Allow batching queries (disabled by default)
|
|
|
|
*
|
|
|
|
* @param bool $enableBatching
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function setQueryBatching($enableBatching)
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
$this->queryBatching = (bool) $enableBatching;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-10 01:19:15 +03:00
|
|
|
* @return self
|
2018-09-26 11:35:23 +03:00
|
|
|
*
|
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
public function setPromiseAdapter(PromiseAdapter $promiseAdapter)
|
|
|
|
{
|
|
|
|
$this->promiseAdapter = $promiseAdapter;
|
2018-07-10 01:19:15 +03:00
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return mixed|callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getContext()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->context;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return mixed|callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getRootValue()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->rootValue;
|
|
|
|
}
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2017-08-19 19:01:46 +03:00
|
|
|
/**
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
public function getSchema()
|
|
|
|
{
|
|
|
|
return $this->schema;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return callable|null
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getErrorFormatter()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->errorFormatter;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return callable|null
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getErrorsHandler()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->errorsHandler;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return PromiseAdapter
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getPromiseAdapter()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->promiseAdapter;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-07 01:35:37 +03:00
|
|
|
* @return ValidationRule[]|callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getValidationRules()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->validationRules;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getFieldResolver()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->fieldResolver;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getPersistentQueryLoader()
|
2017-07-14 15:08:47 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->persistentQueryLoader;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
2017-08-15 14:05:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getDebug()
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->debug;
|
2017-08-15 14:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return bool
|
2017-08-15 14:05:09 +03:00
|
|
|
*/
|
2017-08-19 19:01:46 +03:00
|
|
|
public function getQueryBatching()
|
2017-08-15 14:05:09 +03:00
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
return $this->queryBatching;
|
2017-08-15 14:05:09 +03:00
|
|
|
}
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|