2015-07-15 23:05:46 +06:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Validator\Rules;
|
|
|
|
|
|
|
|
|
|
|
|
use GraphQL\Error;
|
|
|
|
use GraphQL\Language\AST\Directive;
|
|
|
|
use GraphQL\Language\AST\Field;
|
|
|
|
use GraphQL\Language\AST\FragmentDefinition;
|
|
|
|
use GraphQL\Language\AST\FragmentSpread;
|
|
|
|
use GraphQL\Language\AST\InlineFragment;
|
|
|
|
use GraphQL\Language\AST\Node;
|
|
|
|
use GraphQL\Language\AST\OperationDefinition;
|
|
|
|
use GraphQL\Validator\Messages;
|
|
|
|
use GraphQL\Validator\ValidationContext;
|
|
|
|
|
|
|
|
class KnownDirectives
|
|
|
|
{
|
2015-08-17 20:01:55 +06:00
|
|
|
static function unknownDirectiveMessage($directiveName)
|
|
|
|
{
|
|
|
|
return "Unknown directive \"$directiveName\".";
|
|
|
|
}
|
|
|
|
|
|
|
|
static function misplacedDirectiveMessage($directiveName, $placement)
|
|
|
|
{
|
|
|
|
return "Directive \"$directiveName\" may not be used on \"$placement\".";
|
|
|
|
}
|
|
|
|
|
2015-07-15 23:05:46 +06:00
|
|
|
public function __invoke(ValidationContext $context)
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
Node::DIRECTIVE => function (Directive $node, $key, $parent, $path, $ancestors) use ($context) {
|
|
|
|
$directiveDef = null;
|
|
|
|
foreach ($context->getSchema()->getDirectives() as $def) {
|
|
|
|
if ($def->name === $node->name->value) {
|
|
|
|
$directiveDef = $def;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$directiveDef) {
|
|
|
|
return new Error(
|
2015-08-17 20:01:55 +06:00
|
|
|
self::unknownDirectiveMessage($node->name->value),
|
2015-07-15 23:05:46 +06:00
|
|
|
[$node]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$appliedTo = $ancestors[count($ancestors) - 1];
|
|
|
|
|
|
|
|
if ($appliedTo instanceof OperationDefinition && !$directiveDef->onOperation) {
|
|
|
|
return new Error(
|
2015-08-17 20:01:55 +06:00
|
|
|
self::misplacedDirectiveMessage($node->name->value, 'operation'),
|
2015-07-15 23:05:46 +06:00
|
|
|
[$node]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($appliedTo instanceof Field && !$directiveDef->onField) {
|
|
|
|
return new Error(
|
2015-08-17 20:01:55 +06:00
|
|
|
self::misplacedDirectiveMessage($node->name->value, 'field'),
|
2015-07-15 23:05:46 +06:00
|
|
|
[$node]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fragmentKind = (
|
|
|
|
$appliedTo instanceof FragmentSpread ||
|
|
|
|
$appliedTo instanceof InlineFragment ||
|
|
|
|
$appliedTo instanceof FragmentDefinition
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($fragmentKind && !$directiveDef->onFragment) {
|
|
|
|
return new Error(
|
2015-08-17 20:01:55 +06:00
|
|
|
self::misplacedDirectiveMessage($node->name->value, 'fragment'),
|
2015-07-15 23:05:46 +06:00
|
|
|
[$node]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|