mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 14:26:08 +03:00
TASK: Code style #2
This commit is contained in:
parent
a3d050ff6b
commit
5a1caf0549
@ -13,19 +13,17 @@ use GraphQL\Language\AST\NodeKind;
|
|||||||
use GraphQL\Language\AST\NodeList;
|
use GraphQL\Language\AST\NodeList;
|
||||||
use GraphQL\Language\DirectiveLocation;
|
use GraphQL\Language\DirectiveLocation;
|
||||||
use GraphQL\Validator\ValidationContext;
|
use GraphQL\Validator\ValidationContext;
|
||||||
|
use function array_map;
|
||||||
use function count;
|
use function count;
|
||||||
use function in_array;
|
use function in_array;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
use function array_map;
|
|
||||||
|
|
||||||
class KnownDirectives extends ValidationRule
|
class KnownDirectives extends ValidationRule
|
||||||
{
|
{
|
||||||
|
|
||||||
public function getVisitor(ValidationContext $context)
|
public function getVisitor(ValidationContext $context)
|
||||||
{
|
{
|
||||||
$locationsMap = [];
|
$locationsMap = [];
|
||||||
$schema = $context->getSchema();
|
$schema = $context->getSchema();
|
||||||
|
|
||||||
$definedDirectives = $schema->getDirectives();
|
$definedDirectives = $schema->getDirectives();
|
||||||
|
|
||||||
foreach ($definedDirectives as $directive) {
|
foreach ($definedDirectives as $directive) {
|
||||||
@ -35,15 +33,17 @@ class KnownDirectives extends ValidationRule
|
|||||||
$astDefinition = $context->getDocument()->definitions;
|
$astDefinition = $context->getDocument()->definitions;
|
||||||
|
|
||||||
foreach ($astDefinition as $def) {
|
foreach ($astDefinition as $def) {
|
||||||
if ($def instanceof DirectiveDefinitionNode) {
|
if (! ($def instanceof DirectiveDefinitionNode)) {
|
||||||
$locationsMap[$def->name->value] = array_map(function ($name) {
|
continue;
|
||||||
return $name->value;
|
|
||||||
}, $def->locations);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$locationsMap[$def->name->value] = array_map(function ($name) {
|
||||||
|
return $name->value;
|
||||||
|
}, $def->locations);
|
||||||
}
|
}
|
||||||
return [
|
return [
|
||||||
NodeKind::DIRECTIVE => function (DirectiveNode $node, $key, $parent, $path, $ancestors) use ($context, $locationsMap) {
|
NodeKind::DIRECTIVE => function (DirectiveNode $node, $key, $parent, $path, $ancestors) use ($context, $locationsMap) {
|
||||||
$name = $node->name->value;
|
$name = $node->name->value;
|
||||||
$locations = $locationsMap[$name] ?? null;
|
$locations = $locationsMap[$name] ?? null;
|
||||||
|
|
||||||
if (! $locations) {
|
if (! $locations) {
|
||||||
@ -56,15 +56,17 @@ class KnownDirectives extends ValidationRule
|
|||||||
|
|
||||||
$candidateLocation = $this->getDirectiveLocationForASTPath($ancestors);
|
$candidateLocation = $this->getDirectiveLocationForASTPath($ancestors);
|
||||||
|
|
||||||
if ($candidateLocation && ! in_array($candidateLocation, $locations)) {
|
if (! $candidateLocation || in_array($candidateLocation, $locations)) {
|
||||||
$context->reportError(
|
return;
|
||||||
new Error(
|
|
||||||
self::misplacedDirectiveMessage($name, $candidateLocation),
|
|
||||||
[$node]
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
$context->reportError(
|
||||||
|
new Error(
|
||||||
|
self::misplacedDirectiveMessage($name, $candidateLocation),
|
||||||
|
[$node]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user