2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Validator;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
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-11 01:20:19 +03:00
|
|
|
use GraphQL\Language\AST\NodeType;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode;
|
|
|
|
use GraphQL\Language\AST\VariableNode;
|
2016-04-25 00:57:09 +03:00
|
|
|
use GraphQL\Language\Visitor;
|
|
|
|
use \SplObjectStorage;
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\Error;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Schema;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\DocumentNode;
|
|
|
|
use GraphQL\Language\AST\FragmentDefinitionNode;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\AST\Node;
|
|
|
|
use GraphQL\Type\Definition\CompositeType;
|
|
|
|
use GraphQL\Type\Definition\FieldDefinition;
|
|
|
|
use GraphQL\Type\Definition\InputType;
|
|
|
|
use GraphQL\Type\Definition\OutputType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Utils\TypeInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Schema
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $schema;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
/**
|
2016-11-19 02:12:18 +03:00
|
|
|
* @var DocumentNode
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $ast;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TypeInfo
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $typeInfo;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var Error[]
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $errors;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
/**
|
2016-11-19 02:12:18 +03:00
|
|
|
* @var array<string, FragmentDefinitionNode>
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $fragments;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @var SplObjectStorage
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $fragmentSpreads;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SplObjectStorage
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $recursivelyReferencedFragments;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SplObjectStorage
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $variableUsages;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SplObjectStorage
|
|
|
|
*/
|
2016-10-17 14:33:47 +03:00
|
|
|
private $recursiveVariableUsages;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ValidationContext constructor.
|
|
|
|
*
|
|
|
|
* @param Schema $schema
|
2016-11-19 02:12:18 +03:00
|
|
|
* @param DocumentNode $ast
|
2016-04-25 00:57:09 +03:00
|
|
|
* @param TypeInfo $typeInfo
|
|
|
|
*/
|
2016-11-19 02:12:18 +03:00
|
|
|
function __construct(Schema $schema, DocumentNode $ast, TypeInfo $typeInfo)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->schema = $schema;
|
|
|
|
$this->ast = $ast;
|
|
|
|
$this->typeInfo = $typeInfo;
|
|
|
|
$this->errors = [];
|
|
|
|
$this->fragmentSpreads = new SplObjectStorage();
|
|
|
|
$this->recursivelyReferencedFragments = new SplObjectStorage();
|
|
|
|
$this->variableUsages = new SplObjectStorage();
|
|
|
|
$this->recursiveVariableUsages = new SplObjectStorage();
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Error $error
|
|
|
|
*/
|
|
|
|
function reportError(Error $error)
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->errors[] = $error;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Error[]
|
|
|
|
*/
|
|
|
|
function getErrors()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->errors;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
function getSchema()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->schema;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return DocumentNode
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
|
|
|
function getDocument()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->ast;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return FragmentDefinitionNode|null
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
|
|
|
function getFragment($name)
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$fragments = $this->fragments;
|
2015-07-15 20:05:46 +03:00
|
|
|
if (!$fragments) {
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->fragments = $fragments =
|
2015-07-15 20:05:46 +03:00
|
|
|
array_reduce($this->getDocument()->definitions, function($frags, $statement) {
|
2016-11-11 01:20:19 +03:00
|
|
|
if ($statement->kind === NodeType::FRAGMENT_DEFINITION) {
|
2015-07-15 20:05:46 +03:00
|
|
|
$frags[$statement->name->value] = $statement;
|
|
|
|
}
|
|
|
|
return $frags;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
return isset($fragments[$name]) ? $fragments[$name] : null;
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @param HasSelectionSet $node
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return FragmentSpreadNode[]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
|
|
|
function getFragmentSpreads(HasSelectionSet $node)
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$spreads = isset($this->fragmentSpreads[$node]) ? $this->fragmentSpreads[$node] : null;
|
2016-04-25 00:57:09 +03:00
|
|
|
if (!$spreads) {
|
|
|
|
$spreads = [];
|
|
|
|
$setsToVisit = [$node->selectionSet];
|
|
|
|
while (!empty($setsToVisit)) {
|
|
|
|
$set = array_pop($setsToVisit);
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($set->selections); $i++) {
|
|
|
|
$selection = $set->selections[$i];
|
2016-11-11 01:20:19 +03:00
|
|
|
if ($selection->kind === NodeType::FRAGMENT_SPREAD) {
|
2016-04-25 00:57:09 +03:00
|
|
|
$spreads[] = $selection;
|
|
|
|
} else if ($selection->selectionSet) {
|
|
|
|
$setsToVisit[] = $selection->selectionSet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->fragmentSpreads[$node] = $spreads;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
return $spreads;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-19 02:12:18 +03:00
|
|
|
* @param OperationDefinitionNode $operation
|
|
|
|
* @return FragmentDefinitionNode[]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2016-11-19 02:12:18 +03:00
|
|
|
function getRecursivelyReferencedFragments(OperationDefinitionNode $operation)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$fragments = isset($this->recursivelyReferencedFragments[$operation]) ? $this->recursivelyReferencedFragments[$operation] : null;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
if (!$fragments) {
|
|
|
|
$fragments = [];
|
|
|
|
$collectedNames = [];
|
|
|
|
$nodesToVisit = [$operation];
|
|
|
|
while (!empty($nodesToVisit)) {
|
|
|
|
$node = array_pop($nodesToVisit);
|
|
|
|
$spreads = $this->getFragmentSpreads($node);
|
|
|
|
for ($i = 0; $i < count($spreads); $i++) {
|
|
|
|
$fragName = $spreads[$i]->name->value;
|
|
|
|
|
|
|
|
if (empty($collectedNames[$fragName])) {
|
|
|
|
$collectedNames[$fragName] = true;
|
|
|
|
$fragment = $this->getFragment($fragName);
|
|
|
|
if ($fragment) {
|
|
|
|
$fragments[] = $fragment;
|
|
|
|
$nodesToVisit[] = $fragment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->recursivelyReferencedFragments[$operation] = $fragments;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
return $fragments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param HasSelectionSet $node
|
2016-11-19 02:12:18 +03:00
|
|
|
* @return array List of ['node' => VariableNode, 'type' => ?InputObjectType]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
|
|
|
function getVariableUsages(HasSelectionSet $node)
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$usages = isset($this->variableUsages[$node]) ? $this->variableUsages[$node] : null;
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
if (!$usages) {
|
|
|
|
$newUsages = [];
|
2016-10-17 14:33:47 +03:00
|
|
|
$typeInfo = new TypeInfo($this->schema);
|
2016-04-25 00:57:09 +03:00
|
|
|
Visitor::visit($node, Visitor::visitWithTypeInfo($typeInfo, [
|
2016-11-11 01:20:19 +03:00
|
|
|
NodeType::VARIABLE_DEFINITION => function () {
|
2016-04-25 00:57:09 +03:00
|
|
|
return false;
|
|
|
|
},
|
2016-11-19 02:12:18 +03:00
|
|
|
NodeType::VARIABLE => function (VariableNode $variable) use (&$newUsages, $typeInfo) {
|
2016-04-25 00:57:09 +03:00
|
|
|
$newUsages[] = ['node' => $variable, 'type' => $typeInfo->getInputType()];
|
|
|
|
}
|
|
|
|
]));
|
|
|
|
$usages = $newUsages;
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->variableUsages[$node] = $usages;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
return $usages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-19 02:12:18 +03:00
|
|
|
* @param OperationDefinitionNode $operation
|
|
|
|
* @return array List of ['node' => VariableNode, 'type' => ?InputObjectType]
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2016-11-19 02:12:18 +03:00
|
|
|
function getRecursiveVariableUsages(OperationDefinitionNode $operation)
|
2016-04-25 00:57:09 +03:00
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
$usages = isset($this->recursiveVariableUsages[$operation]) ? $this->recursiveVariableUsages[$operation] : null;
|
2016-04-25 00:57:09 +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]);
|
|
|
|
}
|
|
|
|
$usages = call_user_func_array('array_merge', $tmp);
|
2016-10-17 14:33:47 +03:00
|
|
|
$this->recursiveVariableUsages[$operation] = $usages;
|
2016-04-25 00:57:09 +03:00
|
|
|
}
|
|
|
|
return $usages;
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
/**
|
|
|
|
* Returns OutputType
|
|
|
|
*
|
|
|
|
* @return Type
|
|
|
|
*/
|
|
|
|
function getType()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getType();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return CompositeType
|
|
|
|
*/
|
|
|
|
function getParentType()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getParentType();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return InputType
|
|
|
|
*/
|
|
|
|
function getInputType()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getInputType();
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldDefinition
|
|
|
|
*/
|
|
|
|
function getFieldDef()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
function getDirective()
|
|
|
|
{
|
2016-10-17 14:33:47 +03:00
|
|
|
return $this->typeInfo->getDirective();
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getArgument()
|
|
|
|
{
|
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
|
|
|
}
|