1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation\Rule; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ValidationException; |
6
|
|
|
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode; |
|
|
|
|
7
|
|
|
use Digia\GraphQL\Language\AST\Node\InlineFragmentNode; |
|
|
|
|
8
|
|
|
use Digia\GraphQL\Language\AST\Node\NodeInterface; |
|
|
|
|
9
|
|
|
use Digia\GraphQL\Type\Definition\CompositeTypeInterface; |
10
|
|
|
use function Digia\GraphQL\Util\typeFromAST; |
11
|
|
|
use function Digia\GraphQL\Validation\fragmentOnNonCompositeMessage; |
12
|
|
|
use function Digia\GraphQL\Validation\inlineFragmentOnNonCompositeMessage; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Fragments on composite type |
17
|
|
|
* |
18
|
|
|
* Fragments use a type condition to determine if they apply, since fragments |
19
|
|
|
* can only be spread into a composite type (object, interface, or union), the |
20
|
|
|
* type condition must also be a composite type. |
21
|
|
|
*/ |
22
|
|
|
class FragmentsOnCompositeTypesRule extends AbstractRule |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function enterNode(NodeInterface $node): ?NodeInterface |
28
|
|
|
{ |
29
|
|
|
if ($node instanceof InlineFragmentNode) { |
30
|
|
|
$this->validateFragementNode($node, function (NodeInterface $node) { |
31
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
32
|
|
|
return inlineFragmentOnNonCompositeMessage((string)$node->getTypeCondition()); |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($node instanceof FragmentDefinitionNode) { |
37
|
|
|
$this->validateFragementNode($node, function (NodeInterface $node) { |
38
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
39
|
|
|
return fragmentOnNonCompositeMessage((string)$node, (string)$node->getTypeCondition()); |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $node; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param NodeInterface|InlineFragmentNode|FragmentDefinitionNode $node |
48
|
|
|
* @param callable $errorMessageFunction |
49
|
|
|
* @throws \Exception |
50
|
|
|
* @throws \TypeError |
51
|
|
|
*/ |
52
|
|
|
protected function validateFragementNode(NodeInterface $node, callable $errorMessageFunction) |
53
|
|
|
{ |
54
|
|
|
$typeCondition = $node->getTypeCondition(); |
55
|
|
|
|
56
|
|
|
if (null !== $typeCondition) { |
57
|
|
|
$type = typeFromAST($this->validationContext->getSchema(), $typeCondition); |
58
|
|
|
|
59
|
|
|
if (null !== $type && !($type instanceof CompositeTypeInterface)) { |
60
|
|
|
$this->validationContext->reportError(new ValidationException($errorMessageFunction($node), |
61
|
|
|
[$typeCondition])); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths