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;
|
|
|
|
|
|
|
|
class ServerConfig
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return static
|
|
|
|
*/
|
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
|
|
|
|
*/
|
|
|
|
private $defaultFieldResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PromiseAdapter
|
|
|
|
*/
|
|
|
|
private $promiseAdapter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $persistentQueryLoader;
|
|
|
|
|
|
|
|
/**
|
2017-07-27 12:28:56 +03:00
|
|
|
* @return mixed|callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
public function getContext()
|
|
|
|
{
|
|
|
|
return $this->context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-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-07-27 12:28:56 +03:00
|
|
|
* @return mixed|callable
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
public function getRootValue()
|
|
|
|
{
|
|
|
|
return $this->rootValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set schema instance
|
|
|
|
*
|
|
|
|
* @param Schema $schema
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setSchema(Schema $schema)
|
|
|
|
{
|
|
|
|
$this->schema = $schema;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
public function getSchema()
|
|
|
|
{
|
|
|
|
return $this->schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-17 12:42:28 +03:00
|
|
|
* @return callable|null
|
2017-07-14 15:08:47 +03:00
|
|
|
*/
|
|
|
|
public function getErrorFormatter()
|
|
|
|
{
|
|
|
|
return $this->errorFormatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expects function(Throwable $e) : array
|
|
|
|
*
|
|
|
|
* @param callable $errorFormatter
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setErrorFormatter(callable $errorFormatter)
|
|
|
|
{
|
|
|
|
$this->errorFormatter = $errorFormatter;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-17 12:42:28 +03:00
|
|
|
/**
|
|
|
|
* Expects function(array $errors, callable $formatter) : array
|
|
|
|
*
|
|
|
|
* @param callable $handler
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setErrorsHandler(callable $handler)
|
|
|
|
{
|
|
|
|
$this->errorsHandler = $handler;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return callable|null
|
|
|
|
*/
|
|
|
|
public function getErrorsHandler()
|
|
|
|
{
|
|
|
|
return $this->errorsHandler;
|
|
|
|
}
|
|
|
|
|
2017-07-14 15:08:47 +03:00
|
|
|
/**
|
|
|
|
* @return PromiseAdapter
|
|
|
|
*/
|
|
|
|
public function getPromiseAdapter()
|
|
|
|
{
|
|
|
|
return $this->promiseAdapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PromiseAdapter $promiseAdapter
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setPromiseAdapter(PromiseAdapter $promiseAdapter)
|
|
|
|
{
|
|
|
|
$this->promiseAdapter = $promiseAdapter;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array|callable
|
|
|
|
*/
|
|
|
|
public function getValidationRules()
|
|
|
|
{
|
|
|
|
return $this->validationRules;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set validation rules for this server.
|
|
|
|
*
|
|
|
|
* @param array|callable
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setValidationRules($validationRules)
|
|
|
|
{
|
|
|
|
if (!is_callable($validationRules) && !is_array($validationRules) && $validationRules !== null) {
|
|
|
|
throw new InvariantViolation(
|
2017-08-15 14:05:09 +03:00
|
|
|
'Server config expects array of validation rules or callable returning such array, but got ' .
|
2017-07-14 15:08:47 +03:00
|
|
|
Utils::printSafe($validationRules)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validationRules = $validationRules;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function getDefaultFieldResolver()
|
|
|
|
{
|
|
|
|
return $this->defaultFieldResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param callable $defaultFieldResolver
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDefaultFieldResolver(callable $defaultFieldResolver)
|
|
|
|
{
|
|
|
|
$this->defaultFieldResolver = $defaultFieldResolver;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function getPersistentQueryLoader()
|
|
|
|
{
|
|
|
|
return $this->persistentQueryLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function that takes an input id and returns a valid Document.
|
|
|
|
* If provided, this will allow your GraphQL endpoint to execute a document specified via `queryId`.
|
|
|
|
*
|
|
|
|
* @param callable $persistentQueryLoader
|
|
|
|
* @return ServerConfig
|
|
|
|
*/
|
|
|
|
public function setPersistentQueryLoader(callable $persistentQueryLoader)
|
|
|
|
{
|
|
|
|
$this->persistentQueryLoader = $persistentQueryLoader;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getDebug()
|
|
|
|
{
|
|
|
|
return $this->debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-17 12:42:28 +03:00
|
|
|
* Set response debug flags, see GraphQL\Error\Debug class for a list of available flags
|
2017-07-14 15:08:47 +03:00
|
|
|
*
|
2017-08-17 12:42:28 +03:00
|
|
|
* @param bool|int $set
|
2017-07-14 15:08:47 +03:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDebug($set = true)
|
|
|
|
{
|
2017-08-17 12:42:28 +03:00
|
|
|
$this->debug = $set;
|
2017-07-14 15:08:47 +03:00
|
|
|
return $this;
|
|
|
|
}
|
2017-08-15 14:05:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getQueryBatching()
|
|
|
|
{
|
|
|
|
return $this->queryBatching;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow batching queries
|
|
|
|
*
|
|
|
|
* @param bool $enableBatching
|
|
|
|
* @return ServerConfig
|
|
|
|
*/
|
|
|
|
public function setQueryBatching($enableBatching)
|
|
|
|
{
|
|
|
|
$this->queryBatching = (bool) $enableBatching;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-07-14 15:08:47 +03:00
|
|
|
}
|