2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2018-08-07 01:35:37 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
namespace GraphQL\Validator;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
use GraphQL\Error\Error;
|
|
|
|
use GraphQL\Language\AST\DocumentNode;
|
|
|
|
use GraphQL\Language\AST\FragmentDefinitionNode;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\FragmentSpreadNode;
|
2016-04-25 00:57:09 +03:00
|
|
|
use GraphQL\Language\AST\HasSelectionSet;
|
2016-11-19 13:31:47 +03:00
|
|
|
use GraphQL\Language\AST\NodeKind;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode;
|
2018-08-13 11:36:03 +03:00
|
|
|
use GraphQL\Language\AST\SelectionSetNode;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\VariableNode;
|
2016-04-25 00:57:09 +03:00
|
|
|
use GraphQL\Language\Visitor;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\FieldDefinition;
|
|
|
|
use GraphQL\Type\Definition\InputType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
2018-08-07 01:35:37 +03:00
|
|
|
use GraphQL\Type\Schema;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Utils\TypeInfo;
|
2018-08-07 01:35:37 +03:00
|
|
|
use SplObjectStorage;
|
|
|
|
use function array_pop;
|
|
|
|
use function call_user_func_array;
|
|
|
|
use function count;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An instance of this class is passed as the "this" context to all validators,
|
|
|
|
* allowing access to commonly useful contextual information from within a
|
|
|
|
* validation rule.
|
|
|
|
*/
|
|
|
|
class ValidationContext
|
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var Schema */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $schema;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var DocumentNode */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $ast;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var TypeInfo */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $typeInfo;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var Error[] */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $errors;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var FragmentDefinitionNode[] */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $fragments;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var SplObjectStorage */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $fragmentSpreads;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var SplObjectStorage */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $recursivelyReferencedFragments;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var SplObjectStorage */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $variableUsages;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
/** @var SplObjectStorage */
|
2016-10-17 14:33:47 +03:00
|
|
|
private $recursiveVariableUsages;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
public function __construct(Schema $schema, DocumentNode $ast, TypeInfo $typeInfo)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
$this->schema = $schema;
|
|
|
|
$this->ast = $ast;
|
|
|
|
$this->typeInfo = $typeInfo;
|
|
|
|
$this->errors = [];
|
|
|
|
$this->fragmentSpreads = new SplObjectStorage();
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->recursivelyReferencedFragments = new SplObjectStorage();
|
2018-08-07 01:35:37 +03:00
|
|
|
$this->variableUsages = new SplObjectStorage();
|
|
|
|
$this->recursiveVariableUsages = new SplObjectStorage();
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
public function reportError(Error $error)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->errors[] = $error;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Error[]
|
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getErrors()
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->errors;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Schema
|
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getSchema()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->schema;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-07 01:35:37 +03:00
|
|
|
* @return mixed[][] List of ['node' => VariableNode, 'type' => ?InputObjectType]
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getRecursiveVariableUsages(OperationDefinitionNode $operation)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
$usages = $this->recursiveVariableUsages[$operation] ?? null;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
if (! $usages) {
|
|
|
|
$usages = $this->getVariableUsages($operation);
|
|
|
|
$fragments = $this->getRecursivelyReferencedFragments($operation);
|
|
|
|
|
|
|
|
$tmp = [$usages];
|
|
|
|
for ($i = 0; $i < count($fragments); $i++) {
|
|
|
|
$tmp[] = $this->getVariableUsages($fragments[$i]);
|
2017-07-21 18:29:59 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
$usages = call_user_func_array('array_merge', $tmp);
|
|
|
|
$this->recursiveVariableUsages[$operation] = $usages;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
|
|
|
|
return $usages;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
2018-08-07 01:35:37 +03:00
|
|
|
* @return mixed[][] List of ['node' => VariableNode, 'type' => ?InputObjectType]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
private function getVariableUsages(HasSelectionSet $node)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
$usages = $this->variableUsages[$node] ?? null;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
if (! $usages) {
|
|
|
|
$newUsages = [];
|
|
|
|
$typeInfo = new TypeInfo($this->schema);
|
|
|
|
Visitor::visit(
|
|
|
|
$node,
|
|
|
|
Visitor::visitWithTypeInfo(
|
|
|
|
$typeInfo,
|
|
|
|
[
|
2018-09-26 12:01:29 +03:00
|
|
|
NodeKind::VARIABLE_DEFINITION => static function () {
|
2018-08-07 01:35:37 +03:00
|
|
|
return false;
|
|
|
|
},
|
2018-09-26 12:01:29 +03:00
|
|
|
NodeKind::VARIABLE => static function (VariableNode $variable) use (
|
2018-08-07 01:35:37 +03:00
|
|
|
&$newUsages,
|
|
|
|
$typeInfo
|
|
|
|
) {
|
|
|
|
$newUsages[] = ['node' => $variable, 'type' => $typeInfo->getInputType()];
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$usages = $newUsages;
|
|
|
|
$this->variableUsages[$node] = $usages;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
|
|
|
|
return $usages;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return FragmentDefinitionNode[]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getRecursivelyReferencedFragments(OperationDefinitionNode $operation)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
$fragments = $this->recursivelyReferencedFragments[$operation] ?? null;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
if (! $fragments) {
|
|
|
|
$fragments = [];
|
2016-04-25 00:57:09 +03:00
|
|
|
$collectedNames = [];
|
2018-08-07 01:35:37 +03:00
|
|
|
$nodesToVisit = [$operation];
|
|
|
|
while (! empty($nodesToVisit)) {
|
|
|
|
$node = array_pop($nodesToVisit);
|
2016-04-25 00:57:09 +03:00
|
|
|
$spreads = $this->getFragmentSpreads($node);
|
|
|
|
for ($i = 0; $i < count($spreads); $i++) {
|
|
|
|
$fragName = $spreads[$i]->name->value;
|
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
if (! empty($collectedNames[$fragName])) {
|
|
|
|
continue;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
|
|
|
|
$collectedNames[$fragName] = true;
|
|
|
|
$fragment = $this->getFragment($fragName);
|
|
|
|
if (! $fragment) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fragments[] = $fragment;
|
|
|
|
$nodesToVisit[] = $fragment;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
}
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->recursivelyReferencedFragments[$operation] = $fragments;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
return $fragments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-07 01:35:37 +03:00
|
|
|
* @return FragmentSpreadNode[]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getFragmentSpreads(HasSelectionSet $node)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
$spreads = $this->fragmentSpreads[$node] ?? null;
|
|
|
|
if (! $spreads) {
|
2018-08-22 08:07:21 +03:00
|
|
|
$spreads = [];
|
2018-08-13 11:36:03 +03:00
|
|
|
/** @var SelectionSetNode[] $setsToVisit */
|
2018-08-07 01:35:37 +03:00
|
|
|
$setsToVisit = [$node->selectionSet];
|
|
|
|
while (! empty($setsToVisit)) {
|
|
|
|
$set = array_pop($setsToVisit);
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
for ($i = 0; $i < count($set->selections); $i++) {
|
|
|
|
$selection = $set->selections[$i];
|
|
|
|
if ($selection->kind === NodeKind::FRAGMENT_SPREAD) {
|
|
|
|
$spreads[] = $selection;
|
|
|
|
} elseif ($selection->selectionSet) {
|
|
|
|
$setsToVisit[] = $selection->selectionSet;
|
|
|
|
}
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
}
|
|
|
|
$this->fragmentSpreads[$node] = $spreads;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
|
|
|
|
return $spreads;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-07 01:35:37 +03:00
|
|
|
* @param string $name
|
2018-09-26 12:01:29 +03:00
|
|
|
*
|
2018-08-07 01:35:37 +03:00
|
|
|
* @return FragmentDefinitionNode|null
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getFragment($name)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2018-08-07 01:35:37 +03:00
|
|
|
$fragments = $this->fragments;
|
|
|
|
if (! $fragments) {
|
|
|
|
$fragments = [];
|
|
|
|
foreach ($this->getDocument()->definitions as $statement) {
|
|
|
|
if ($statement->kind !== NodeKind::FRAGMENT_DEFINITION) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
$fragments[$statement->name->value] = $statement;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
$this->fragments = $fragments;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
2018-08-07 01:35:37 +03:00
|
|
|
|
|
|
|
return $fragments[$name] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DocumentNode
|
|
|
|
*/
|
|
|
|
public function getDocument()
|
|
|
|
{
|
|
|
|
return $this->ast;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
/**
|
|
|
|
* Returns OutputType
|
|
|
|
*
|
|
|
|
* @return Type
|
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getType()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getType();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-07 01:35:37 +03:00
|
|
|
* @return Type
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getParentType()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getParentType();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return InputType
|
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getInputType()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getInputType();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2018-02-15 23:29:14 +03:00
|
|
|
/**
|
|
|
|
* @return InputType
|
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getParentInputType()
|
2018-02-15 23:29:14 +03:00
|
|
|
{
|
|
|
|
return $this->typeInfo->getParentInputType();
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
/**
|
|
|
|
* @return FieldDefinition
|
|
|
|
*/
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getFieldDef()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getFieldDef();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getDirective()
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getDirective();
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2018-08-07 01:35:37 +03:00
|
|
|
public function getArgument()
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getArgument();
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|