graphql-php/src/Server/ServerConfig.php

341 lines
6.6 KiB
PHP
Raw Normal View History

<?php
2018-07-10 01:19:15 +03:00
declare(strict_types=1);
namespace GraphQL\Server;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Promise\PromiseAdapter;
use GraphQL\Type\Schema;
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;
/**
* Server configuration class.
* Could be passed directly to server constructor. List of options accepted by **create** method is
* [described in docs](executing-queries.md#server-configuration-options).
*
* Usage example:
*
* $config = GraphQL\Server\ServerConfig::create()
* ->setSchema($mySchema)
* ->setContext($myContext);
*
* $server = new GraphQL\Server\StandardServer($config);
*/
class ServerConfig
{
/**
* 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
*
* @return ServerConfig
2018-09-26 11:35:23 +03:00
*
* @api
*/
public static function create(array $config = [])
{
$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));
}
$instance->$method($value);
}
2018-07-10 01:19:15 +03:00
return $instance;
}
2018-07-10 01:19:15 +03:00
/** @var Schema */
private $schema;
2018-09-26 11:35:23 +03:00
/** @var mixed|callable */
private $context;
2018-09-26 11:35:23 +03:00
/** @var mixed|callable */
private $rootValue;
2018-07-10 01:19:15 +03:00
/** @var callable|null */
private $errorFormatter;
2018-07-10 01:19:15 +03:00
/** @var callable|null */
private $errorsHandler;
2018-07-10 01:19:15 +03:00
/** @var bool */
private $debug = false;
2018-07-10 01:19:15 +03:00
/** @var bool */
private $queryBatching = false;
2018-08-07 01:35:37 +03:00
/** @var ValidationRule[]|callable */
private $validationRules;
2018-07-10 01:19:15 +03:00
/** @var callable */
2017-08-17 22:54:35 +03:00
private $fieldResolver;
2018-07-10 01:19:15 +03:00
/** @var PromiseAdapter */
private $promiseAdapter;
2018-07-10 01:19:15 +03:00
/** @var callable */
private $persistentQueryLoader;
/**
2018-07-10 01:19:15 +03:00
* @return self
2018-09-26 11:35:23 +03:00
*
* @api
*/
public function setSchema(Schema $schema)
{
$this->schema = $schema;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
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
*/
public function setContext($context)
{
$this->context = $context;
2018-07-10 01:19:15 +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
*/
public function setRootValue($rootValue)
{
$this->rootValue = $rootValue;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* Expects function(Throwable $e) : array
*
2018-07-10 01:19:15 +03:00
* @return self
2018-09-26 11:35:23 +03:00
*
* @api
*/
public function setErrorFormatter(callable $errorFormatter)
{
$this->errorFormatter = $errorFormatter;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* Expects function(array $errors, callable $formatter) : array
*
2018-07-10 01:19:15 +03:00
* @return self
2018-09-26 11:35:23 +03:00
*
* @api
*/
public function setErrorsHandler(callable $handler)
{
$this->errorsHandler = $handler;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* 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
*/
public function setValidationRules($validationRules)
{
2018-07-10 01:19:15 +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)
);
}
$this->validationRules = $validationRules;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
2018-07-10 01:19:15 +03:00
* @return self
2018-09-26 11:35:23 +03:00
*
* @api
*/
public function setFieldResolver(callable $fieldResolver)
{
$this->fieldResolver = $fieldResolver;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* Expects function($queryId, OperationParams $params) : string|DocumentNode
*
* 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
*/
public function setPersistentQueryLoader(callable $persistentQueryLoader)
{
$this->persistentQueryLoader = $persistentQueryLoader;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* 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
*/
public function setDebug($set = true)
{
$this->debug = $set;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* 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
*/
public function setQueryBatching($enableBatching)
{
$this->queryBatching = (bool) $enableBatching;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
2018-07-10 01:19:15 +03:00
* @return self
2018-09-26 11:35:23 +03:00
*
* @api
*/
public function setPromiseAdapter(PromiseAdapter $promiseAdapter)
{
$this->promiseAdapter = $promiseAdapter;
2018-07-10 01:19:15 +03:00
return $this;
}
/**
* @return mixed|callable
*/
public function getContext()
{
return $this->context;
}
/**
* @return mixed|callable
*/
public function getRootValue()
{
return $this->rootValue;
}
/**
* @return Schema
*/
public function getSchema()
{
return $this->schema;
}
/**
* @return callable|null
*/
public function getErrorFormatter()
{
return $this->errorFormatter;
}
/**
* @return callable|null
*/
public function getErrorsHandler()
{
return $this->errorsHandler;
}
/**
* @return PromiseAdapter
*/
public function getPromiseAdapter()
{
return $this->promiseAdapter;
}
/**
2018-08-07 01:35:37 +03:00
* @return ValidationRule[]|callable
*/
public function getValidationRules()
{
return $this->validationRules;
}
/**
* @return callable
*/
public function getFieldResolver()
{
return $this->fieldResolver;
}
/**
* @return callable
*/
public function getPersistentQueryLoader()
{
return $this->persistentQueryLoader;
}
/**
* @return bool
*/
public function getDebug()
{
return $this->debug;
}
/**
* @return bool
*/
public function getQueryBatching()
{
return $this->queryBatching;
}
}