2015-07-15 23:05:46 +06:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Validator;
|
|
|
|
|
2016-11-01 23:40:37 +07:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2016-11-19 06:12:18 +07:00
|
|
|
use GraphQL\Language\AST\ListValueNode;
|
|
|
|
use GraphQL\Language\AST\DocumentNode;
|
2016-11-19 17:31:47 +07:00
|
|
|
use GraphQL\Language\AST\NodeKind;
|
2016-11-19 06:12:18 +07:00
|
|
|
use GraphQL\Language\AST\NullValueNode;
|
|
|
|
use GraphQL\Language\AST\VariableNode;
|
2016-04-25 03:57:09 +06:00
|
|
|
use GraphQL\Language\Printer;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Language\Visitor;
|
|
|
|
use GraphQL\Schema;
|
|
|
|
use GraphQL\Type\Definition\InputObjectType;
|
2016-11-01 23:40:37 +07:00
|
|
|
use GraphQL\Type\Definition\LeafType;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Type\Definition\ListOfType;
|
|
|
|
use GraphQL\Type\Definition\NonNull;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Utils;
|
|
|
|
use GraphQL\Utils\TypeInfo;
|
|
|
|
use GraphQL\Validator\Rules\ArgumentsOfCorrectType;
|
|
|
|
use GraphQL\Validator\Rules\DefaultValuesOfCorrectType;
|
2017-06-17 14:51:38 +02:00
|
|
|
use GraphQL\Validator\Rules\DisableIntrospection;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Validator\Rules\FieldsOnCorrectType;
|
|
|
|
use GraphQL\Validator\Rules\FragmentsOnCompositeTypes;
|
|
|
|
use GraphQL\Validator\Rules\KnownArgumentNames;
|
|
|
|
use GraphQL\Validator\Rules\KnownDirectives;
|
|
|
|
use GraphQL\Validator\Rules\KnownFragmentNames;
|
|
|
|
use GraphQL\Validator\Rules\KnownTypeNames;
|
2016-04-25 03:57:09 +06:00
|
|
|
use GraphQL\Validator\Rules\LoneAnonymousOperation;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Validator\Rules\NoFragmentCycles;
|
|
|
|
use GraphQL\Validator\Rules\NoUndefinedVariables;
|
|
|
|
use GraphQL\Validator\Rules\NoUnusedFragments;
|
|
|
|
use GraphQL\Validator\Rules\NoUnusedVariables;
|
|
|
|
use GraphQL\Validator\Rules\OverlappingFieldsCanBeMerged;
|
|
|
|
use GraphQL\Validator\Rules\PossibleFragmentSpreads;
|
2015-08-17 20:01:55 +06:00
|
|
|
use GraphQL\Validator\Rules\ProvidedNonNullArguments;
|
2016-04-09 10:04:14 +02:00
|
|
|
use GraphQL\Validator\Rules\QueryComplexity;
|
|
|
|
use GraphQL\Validator\Rules\QueryDepth;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Validator\Rules\ScalarLeafs;
|
2016-04-25 19:29:17 +06:00
|
|
|
use GraphQL\Validator\Rules\UniqueArgumentNames;
|
2016-11-19 00:21:56 +07:00
|
|
|
use GraphQL\Validator\Rules\UniqueDirectivesPerLocation;
|
2016-04-25 19:29:17 +06:00
|
|
|
use GraphQL\Validator\Rules\UniqueFragmentNames;
|
|
|
|
use GraphQL\Validator\Rules\UniqueInputFieldNames;
|
|
|
|
use GraphQL\Validator\Rules\UniqueOperationNames;
|
|
|
|
use GraphQL\Validator\Rules\UniqueVariableNames;
|
2015-07-15 23:05:46 +06:00
|
|
|
use GraphQL\Validator\Rules\VariablesAreInputTypes;
|
|
|
|
use GraphQL\Validator\Rules\VariablesInAllowedPosition;
|
|
|
|
|
|
|
|
class DocumentValidator
|
|
|
|
{
|
2016-04-09 08:44:57 +02:00
|
|
|
private static $rules = [];
|
2015-07-15 23:05:46 +06:00
|
|
|
|
2016-04-09 08:44:57 +02:00
|
|
|
private static $defaultRules;
|
|
|
|
|
|
|
|
private static $initRules = false;
|
|
|
|
|
|
|
|
public static function allRules()
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
2016-04-09 08:44:57 +02:00
|
|
|
if (!self::$initRules) {
|
|
|
|
self::$rules = array_merge(static::defaultRules(), self::$rules);
|
|
|
|
self::$initRules = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$rules;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function defaultRules()
|
|
|
|
{
|
|
|
|
if (null === self::$defaultRules) {
|
|
|
|
self::$defaultRules = [
|
2016-04-25 19:29:17 +06:00
|
|
|
'UniqueOperationNames' => new UniqueOperationNames(),
|
2016-04-25 03:57:09 +06:00
|
|
|
'LoneAnonymousOperation' => new LoneAnonymousOperation(),
|
2016-04-09 08:44:57 +02:00
|
|
|
'KnownTypeNames' => new KnownTypeNames(),
|
|
|
|
'FragmentsOnCompositeTypes' => new FragmentsOnCompositeTypes(),
|
|
|
|
'VariablesAreInputTypes' => new VariablesAreInputTypes(),
|
|
|
|
'ScalarLeafs' => new ScalarLeafs(),
|
|
|
|
'FieldsOnCorrectType' => new FieldsOnCorrectType(),
|
2016-04-25 19:29:17 +06:00
|
|
|
'UniqueFragmentNames' => new UniqueFragmentNames(),
|
2016-04-09 08:44:57 +02:00
|
|
|
'KnownFragmentNames' => new KnownFragmentNames(),
|
|
|
|
'NoUnusedFragments' => new NoUnusedFragments(),
|
|
|
|
'PossibleFragmentSpreads' => new PossibleFragmentSpreads(),
|
|
|
|
'NoFragmentCycles' => new NoFragmentCycles(),
|
2016-04-25 19:29:17 +06:00
|
|
|
'UniqueVariableNames' => new UniqueVariableNames(),
|
2016-04-09 08:44:57 +02:00
|
|
|
'NoUndefinedVariables' => new NoUndefinedVariables(),
|
|
|
|
'NoUnusedVariables' => new NoUnusedVariables(),
|
|
|
|
'KnownDirectives' => new KnownDirectives(),
|
2016-11-19 00:21:56 +07:00
|
|
|
'UniqueDirectivesPerLocation' => new UniqueDirectivesPerLocation(),
|
2016-04-09 08:44:57 +02:00
|
|
|
'KnownArgumentNames' => new KnownArgumentNames(),
|
2016-04-25 19:29:17 +06:00
|
|
|
'UniqueArgumentNames' => new UniqueArgumentNames(),
|
2016-04-09 08:44:57 +02:00
|
|
|
'ArgumentsOfCorrectType' => new ArgumentsOfCorrectType(),
|
|
|
|
'ProvidedNonNullArguments' => new ProvidedNonNullArguments(),
|
|
|
|
'DefaultValuesOfCorrectType' => new DefaultValuesOfCorrectType(),
|
|
|
|
'VariablesInAllowedPosition' => new VariablesInAllowedPosition(),
|
|
|
|
'OverlappingFieldsCanBeMerged' => new OverlappingFieldsCanBeMerged(),
|
2016-04-25 19:29:17 +06:00
|
|
|
'UniqueInputFieldNames' => new UniqueInputFieldNames(),
|
2016-04-25 03:57:09 +06:00
|
|
|
|
2016-04-09 10:04:14 +02:00
|
|
|
// Query Security
|
2017-06-17 14:51:38 +02:00
|
|
|
'DisableIntrospection' => new DisableIntrospection(DisableIntrospection::DISABLED), // DEFAULT DISABLED
|
2016-04-09 10:04:14 +02:00
|
|
|
'QueryDepth' => new QueryDepth(QueryDepth::DISABLED), // default disabled
|
|
|
|
'QueryComplexity' => new QueryComplexity(QueryComplexity::DISABLED), // default disabled
|
2015-07-15 23:05:46 +06:00
|
|
|
];
|
|
|
|
}
|
2016-04-09 08:44:57 +02:00
|
|
|
|
|
|
|
return self::$defaultRules;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getRule($name)
|
|
|
|
{
|
2016-04-09 10:04:14 +02:00
|
|
|
$rules = static::allRules();
|
|
|
|
|
|
|
|
return isset($rules[$name]) ? $rules[$name] : null ;
|
2016-04-09 08:44:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function addRule($name, callable $rule)
|
|
|
|
{
|
|
|
|
self::$rules[$name] = $rule;
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:19:16 +07:00
|
|
|
public static function validate(Schema $schema, DocumentNode $ast, array $rules = null, TypeInfo $typeInfo = null)
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
2017-07-04 16:19:16 +07:00
|
|
|
$typeInfo = $typeInfo ?: new TypeInfo($schema);
|
2016-04-25 03:57:09 +06:00
|
|
|
$errors = static::visitUsingRules($schema, $typeInfo, $ast, $rules ?: static::allRules());
|
2015-08-17 20:01:55 +06:00
|
|
|
return $errors;
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
2016-04-09 08:44:57 +02:00
|
|
|
public static function isError($value)
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
|
|
|
return is_array($value)
|
2017-06-06 12:55:38 +02:00
|
|
|
? count(array_filter($value, function($item) { return $item instanceof \Exception || $item instanceof \Throwable;})) === count($value)
|
|
|
|
: ($value instanceof \Exception || $value instanceof \Throwable);
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
2016-04-09 08:44:57 +02:00
|
|
|
public static function append(&$arr, $items)
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
|
|
|
if (is_array($items)) {
|
|
|
|
$arr = array_merge($arr, $items);
|
|
|
|
} else {
|
|
|
|
$arr[] = $items;
|
|
|
|
}
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2016-04-25 03:57:09 +06:00
|
|
|
/**
|
|
|
|
* Utility for validators which determines if a value literal AST is valid given
|
|
|
|
* an input type.
|
|
|
|
*
|
|
|
|
* Note that this only validates literal values, variables are assumed to
|
|
|
|
* provide values of the correct type.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-19 06:47:55 +07:00
|
|
|
public static function isValidLiteralValue(Type $type, $valueNode)
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
2016-04-25 03:57:09 +06:00
|
|
|
// A value must be provided if the type is non-null.
|
|
|
|
if ($type instanceof NonNull) {
|
2016-11-19 06:47:55 +07:00
|
|
|
if (!$valueNode || $valueNode instanceof NullValueNode) {
|
2016-11-19 04:15:40 +07:00
|
|
|
return [ 'Expected "' . Utils::printSafe($type) . '", found null.' ];
|
2016-04-25 03:57:09 +06:00
|
|
|
}
|
2016-11-19 06:47:55 +07:00
|
|
|
return static::isValidLiteralValue($type->getWrappedType(), $valueNode);
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
2016-11-19 06:47:55 +07:00
|
|
|
if (!$valueNode || $valueNode instanceof NullValueNode) {
|
2016-04-25 03:57:09 +06:00
|
|
|
return [];
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
// This function only tests literals, and assumes variables will provide
|
|
|
|
// values of the correct type.
|
2016-11-19 06:47:55 +07:00
|
|
|
if ($valueNode instanceof VariableNode) {
|
2016-04-25 03:57:09 +06:00
|
|
|
return [];
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lists accept a non-list value as a list of one.
|
|
|
|
if ($type instanceof ListOfType) {
|
|
|
|
$itemType = $type->getWrappedType();
|
2016-11-19 06:47:55 +07:00
|
|
|
if ($valueNode instanceof ListValueNode) {
|
2016-04-25 03:57:09 +06:00
|
|
|
$errors = [];
|
2016-11-19 06:47:55 +07:00
|
|
|
foreach($valueNode->values as $index => $itemNode) {
|
|
|
|
$tmp = static::isValidLiteralValue($itemType, $itemNode);
|
2016-04-25 03:57:09 +06:00
|
|
|
|
|
|
|
if ($tmp) {
|
|
|
|
$errors = array_merge($errors, Utils::map($tmp, function($error) use ($index) {
|
|
|
|
return "In element #$index: $error";
|
|
|
|
}));
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 03:57:09 +06:00
|
|
|
return $errors;
|
2015-07-15 23:05:46 +06:00
|
|
|
} else {
|
2016-11-19 06:47:55 +07:00
|
|
|
return static::isValidLiteralValue($itemType, $valueNode);
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 03:57:09 +06:00
|
|
|
// Input objects check each defined field and look for undefined fields.
|
2015-07-15 23:05:46 +06:00
|
|
|
if ($type instanceof InputObjectType) {
|
2016-11-19 17:31:47 +07:00
|
|
|
if ($valueNode->kind !== NodeKind::OBJECT) {
|
2016-04-25 03:57:09 +06:00
|
|
|
return [ "Expected \"{$type->name}\", found not an object." ];
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
2016-04-25 03:57:09 +06:00
|
|
|
|
|
|
|
$fields = $type->getFields();
|
|
|
|
$errors = [];
|
|
|
|
|
|
|
|
// Ensure every provided field is defined.
|
2016-11-19 06:47:55 +07:00
|
|
|
$fieldNodes = $valueNode->fields;
|
2015-07-15 23:05:46 +06:00
|
|
|
|
2016-11-19 06:47:55 +07:00
|
|
|
foreach ($fieldNodes as $providedFieldNode) {
|
|
|
|
if (empty($fields[$providedFieldNode->name->value])) {
|
|
|
|
$errors[] = "In field \"{$providedFieldNode->name->value}\": Unknown field.";
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 03:57:09 +06:00
|
|
|
|
|
|
|
// Ensure every defined field is valid.
|
2016-11-19 06:47:55 +07:00
|
|
|
$fieldNodeMap = Utils::keyMap($fieldNodes, function($fieldNode) {return $fieldNode->name->value;});
|
2016-04-25 03:57:09 +06:00
|
|
|
foreach ($fields as $fieldName => $field) {
|
|
|
|
$result = static::isValidLiteralValue(
|
|
|
|
$field->getType(),
|
2016-11-19 06:47:55 +07:00
|
|
|
isset($fieldNodeMap[$fieldName]) ? $fieldNodeMap[$fieldName]->value : null
|
2016-04-25 03:57:09 +06:00
|
|
|
);
|
|
|
|
if ($result) {
|
|
|
|
$errors = array_merge($errors, Utils::map($result, function($error) use ($fieldName) {
|
|
|
|
return "In field \"$fieldName\": $error";
|
|
|
|
}));
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
}
|
2016-04-25 03:57:09 +06:00
|
|
|
|
|
|
|
return $errors;
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
2016-11-01 23:40:37 +07:00
|
|
|
if ($type instanceof LeafType) {
|
2017-07-04 20:19:52 +07:00
|
|
|
// Scalars must parse to a non-null value
|
|
|
|
if (!$type->isValidLiteral($valueNode)) {
|
2016-11-19 06:47:55 +07:00
|
|
|
$printed = Printer::doPrint($valueNode);
|
2016-11-01 23:40:37 +07:00
|
|
|
return [ "Expected type \"{$type->name}\", found $printed." ];
|
|
|
|
}
|
2016-04-25 03:57:09 +06:00
|
|
|
|
2016-11-01 23:40:37 +07:00
|
|
|
return [];
|
2016-04-25 03:57:09 +06:00
|
|
|
}
|
|
|
|
|
2016-11-01 23:40:37 +07:00
|
|
|
throw new InvariantViolation('Must be input type');
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This uses a specialized visitor which runs multiple visitors in parallel,
|
|
|
|
* while maintaining the visitor skip and break API.
|
|
|
|
*
|
|
|
|
* @param Schema $schema
|
2016-04-25 03:57:09 +06:00
|
|
|
* @param TypeInfo $typeInfo
|
2016-11-19 06:47:55 +07:00
|
|
|
* @param DocumentNode $documentNode
|
2015-07-15 23:05:46 +06:00
|
|
|
* @param array $rules
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-11-19 06:47:55 +07:00
|
|
|
public static function visitUsingRules(Schema $schema, TypeInfo $typeInfo, DocumentNode $documentNode, array $rules)
|
2015-07-15 23:05:46 +06:00
|
|
|
{
|
2016-11-19 06:47:55 +07:00
|
|
|
$context = new ValidationContext($schema, $documentNode, $typeInfo);
|
2016-04-25 03:57:09 +06:00
|
|
|
$visitors = [];
|
|
|
|
foreach ($rules as $rule) {
|
|
|
|
$visitors[] = $rule($context);
|
|
|
|
}
|
2016-11-19 06:47:55 +07:00
|
|
|
Visitor::visit($documentNode, Visitor::visitWithTypeInfo($typeInfo, Visitor::visitInParallel($visitors)));
|
2016-04-25 03:57:09 +06:00
|
|
|
return $context->getErrors();
|
2015-07-15 23:05:46 +06:00
|
|
|
}
|
|
|
|
}
|