mirror of
https://github.com/retailcrm/graphql-php.git
synced 2025-02-06 15:59:24 +03:00
Server: Ability to use thunk for root value and context
This commit is contained in:
parent
e7838d2253
commit
6a20483b87
@ -95,14 +95,16 @@ class Helper
|
|||||||
if (!$doc instanceof DocumentNode) {
|
if (!$doc instanceof DocumentNode) {
|
||||||
$doc = Parser::parse($doc);
|
$doc = Parser::parse($doc);
|
||||||
}
|
}
|
||||||
if ($op->isReadOnly() && AST::getOperation($doc, $op->operation) !== 'query') {
|
|
||||||
|
$operationType = AST::getOperation($doc, $op->operation);
|
||||||
|
if ($op->isReadOnly() && $operationType !== 'query') {
|
||||||
throw new Error("GET supports only query operation");
|
throw new Error("GET supports only query operation");
|
||||||
}
|
}
|
||||||
|
|
||||||
$validationErrors = DocumentValidator::validate(
|
$validationErrors = DocumentValidator::validate(
|
||||||
$config->getSchema(),
|
$config->getSchema(),
|
||||||
$doc,
|
$doc,
|
||||||
$this->resolveValidationRules($config, $op)
|
$this->resolveValidationRules($config, $op, $doc, $operationType)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!empty($validationErrors)) {
|
if (!empty($validationErrors)) {
|
||||||
@ -114,8 +116,8 @@ class Helper
|
|||||||
$promiseAdapter,
|
$promiseAdapter,
|
||||||
$config->getSchema(),
|
$config->getSchema(),
|
||||||
$doc,
|
$doc,
|
||||||
$config->getRootValue(),
|
$this->resolveRootValue($config, $op, $doc, $operationType),
|
||||||
$config->getContext(),
|
$this->resolveContextValue($config, $op, $doc, $operationType),
|
||||||
$op->variables,
|
$op->variables,
|
||||||
$op->operation,
|
$op->operation,
|
||||||
$config->getDefaultFieldResolver()
|
$config->getDefaultFieldResolver()
|
||||||
@ -176,15 +178,17 @@ class Helper
|
|||||||
/**
|
/**
|
||||||
* @param ServerConfig $config
|
* @param ServerConfig $config
|
||||||
* @param OperationParams $params
|
* @param OperationParams $params
|
||||||
|
* @param DocumentNode $doc
|
||||||
|
* @param $operationType
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function resolveValidationRules(ServerConfig $config, OperationParams $params)
|
private function resolveValidationRules(ServerConfig $config, OperationParams $params, DocumentNode $doc, $operationType)
|
||||||
{
|
{
|
||||||
// Allow customizing validation rules per operation:
|
// Allow customizing validation rules per operation:
|
||||||
$validationRules = $config->getValidationRules();
|
$validationRules = $config->getValidationRules();
|
||||||
|
|
||||||
if (is_callable($validationRules)) {
|
if (is_callable($validationRules)) {
|
||||||
$validationRules = $validationRules($params);
|
$validationRules = $validationRules($params, $doc, $operationType);
|
||||||
|
|
||||||
if (!is_array($validationRules)) {
|
if (!is_array($validationRules)) {
|
||||||
throw new InvariantViolation(sprintf(
|
throw new InvariantViolation(sprintf(
|
||||||
@ -197,6 +201,42 @@ class Helper
|
|||||||
return $validationRules;
|
return $validationRules;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ServerConfig $config
|
||||||
|
* @param OperationParams $params
|
||||||
|
* @param DocumentNode $doc
|
||||||
|
* @param $operationType
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function resolveRootValue(ServerConfig $config, OperationParams $params, DocumentNode $doc, $operationType)
|
||||||
|
{
|
||||||
|
$root = $config->getRootValue();
|
||||||
|
|
||||||
|
if (is_callable($root)) {
|
||||||
|
$root = $root($params, $doc, $operationType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ServerConfig $config
|
||||||
|
* @param OperationParams $params
|
||||||
|
* @param DocumentNode $doc
|
||||||
|
* @param $operationType
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private function resolveContextValue(ServerConfig $config, OperationParams $params, DocumentNode $doc, $operationType)
|
||||||
|
{
|
||||||
|
$context = $config->getContext();
|
||||||
|
|
||||||
|
if (is_callable($context)) {
|
||||||
|
$context = $context($params, $doc, $operationType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses HTTP request and returns GraphQL OperationParams contained in this request.
|
* Parses HTTP request and returns GraphQL OperationParams contained in this request.
|
||||||
* For batched requests it returns an array of OperationParams.
|
* For batched requests it returns an array of OperationParams.
|
||||||
|
@ -23,12 +23,12 @@ class ServerConfig
|
|||||||
private $schema;
|
private $schema;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var mixed
|
* @var mixed|callable
|
||||||
*/
|
*/
|
||||||
private $context;
|
private $context;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var mixed
|
* @var mixed|callable
|
||||||
*/
|
*/
|
||||||
private $rootValue;
|
private $rootValue;
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ class ServerConfig
|
|||||||
private $persistentQueryLoader;
|
private $persistentQueryLoader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|callable
|
||||||
*/
|
*/
|
||||||
public function getContext()
|
public function getContext()
|
||||||
{
|
{
|
||||||
@ -71,7 +71,7 @@ class ServerConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $context
|
* @param mixed|callable $context
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setContext($context)
|
public function setContext($context)
|
||||||
@ -81,7 +81,7 @@ class ServerConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $rootValue
|
* @param mixed|callable $rootValue
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setRootValue($rootValue)
|
public function setRootValue($rootValue)
|
||||||
@ -91,7 +91,7 @@ class ServerConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed|callable
|
||||||
*/
|
*/
|
||||||
public function getRootValue()
|
public function getRootValue()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user