2017-07-14 15:08:47 +03:00
|
|
|
<?php
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
* [described in docs](executing-queries/#server-configuration-options).
|
|
|
|
*
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param array $config
|
|
|
|
* @return ServerConfig
|
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);
|
|
|
|
if (!method_exists($instance, $method)) {
|
|
|
|
throw new InvariantViolation("Unknown server config option \"$key\"");
|
|
|
|
}
|
|
|
|
$instance->$method($value);
|
|
|
|
}
|
|
|
|
return $instance;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Schema
|
|
|
|
*/
|
|
|
|
private $schema;
|
|
|
|
|
|
|
|
/**
|
2017-08-15 16:45:23 +03:00
|
|
|
* @var mixed|\Closure
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
private $context;
|
|
|
|
|
|
|
|
/**
|
2017-08-15 16:45:23 +03:00
|
|
|
* @var mixed|\Closure
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
private $rootValue;
|
|
|
|
|
|
|
|
/**
|
2017-08-17 12:42:28 +03:00
|
|
|
* @var callable|null
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
2017-07-21 18:11:20 +03:00
|
|
|
private $errorFormatter;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
2017-08-17 12:42:28 +03:00
|
|
|
/**
|
|
|
|
* @var callable|null
|
|
|
|
*/
|
|
|
|
private $errorsHandler;
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $debug = false;
|
|
|
|
|
2017-08-15 14:05:09 +03:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $queryBatching = false;
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
/**
|
|
|
|
* @var array|callable
|
|
|
|
*/
|
|
|
|
private $validationRules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
2017-08-17 22:54:35 +03:00
|
|
|
private $fieldResolver;
|
2017-07-14 15:08:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PromiseAdapter
|
|
|
|
*/
|
|
|
|
private $promiseAdapter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $persistentQueryLoader;
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
|
|
|
* @param Schema $schema
|
|
|
|
* @return $this
|
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;
|
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-08-15 16:45:23 +03:00
|
|
|
* @param mixed|\Closure $context
|
2017-07-14 15:08:47 +03:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setContext($context)
|
|
|
|
{
|
|
|
|
$this->context = $context;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-08-15 16:45:23 +03:00
|
|
|
* @param mixed|\Closure $rootValue
|
2017-07-14 15:08:47 +03:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setRootValue($rootValue)
|
|
|
|
{
|
|
|
|
$this->rootValue = $rootValue;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Expects function(Throwable $e) : array
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param callable $errorFormatter
|
|
|
|
* @return $this
|
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;
|
|
|
|
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
|
|
|
*
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
|
|
|
* @param callable $handler
|
2017-07-14 15:08:47 +03:00
|
|
|
* @return $this
|
|
|
|
*/
|
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;
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* Set validation rules for this server.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param array|callable
|
|
|
|
* @return $this
|
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
|
|
|
{
|
2017-08-19 19:01:46 +03:00
|
|
|
if (!is_callable($validationRules) && !is_array($validationRules) && $validationRules !== null) {
|
|
|
|
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;
|
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
|
|
|
* @param callable $fieldResolver
|
2017-07-14 15:08:47 +03:00
|
|
|
* @return $this
|
|
|
|
*/
|
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;
|
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.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param callable $persistentQueryLoader
|
2017-08-17 12:42:28 +03:00
|
|
|
* @return $this
|
|
|
|
*/
|
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;
|
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
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param bool|int $set
|
|
|
|
* @return $this
|
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;
|
|
|
|
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)
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
* @param bool $enableBatching
|
|
|
|
* @return $this
|
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;
|
|
|
|
return $this;
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @api
|
2017-07-14 15:08:47 +03:00
|
|
|
* @param PromiseAdapter $promiseAdapter
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setPromiseAdapter(PromiseAdapter $promiseAdapter)
|
|
|
|
{
|
|
|
|
$this->promiseAdapter = $promiseAdapter;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-19 19:01:46 +03:00
|
|
|
* @return array|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
|
|
|
}
|