1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/******************************************************************************* |
4
|
|
|
* This file is part of the GraphQL Bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) YnloUltratech <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
******************************************************************************/ |
11
|
|
|
|
12
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Plugin; |
13
|
|
|
|
14
|
|
|
use Ynlo\GraphQLBundle\Definition\DefinitionInterface; |
15
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\TypeAwareDefinitionInterface; |
18
|
|
|
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry; |
19
|
|
|
|
20
|
|
|
class SchemaValidatorDefinitionPlugin extends AbstractDefinitionPlugin |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Endpoint |
24
|
|
|
*/ |
25
|
|
|
protected $endpoint; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
*/ |
30
|
|
|
public function configureEndpoint(Endpoint $endpoint): void |
31
|
|
|
{ |
32
|
|
|
$this->endpoint = $endpoint; |
33
|
|
|
|
34
|
|
|
//run extensions recursively in all types and fields |
35
|
|
|
foreach ($endpoint->allTypes() as $type) { |
36
|
|
|
$this->validateDefinition($type); |
37
|
|
|
if ($type instanceof FieldsAwareDefinitionInterface) { |
38
|
|
|
foreach ($type->getFields() as $field) { |
39
|
|
|
$this->validateDefinition($field, $type->getName()); |
40
|
|
|
foreach ($field->getArguments() as $argument) { |
41
|
|
|
$this->validateDefinition($argument, sprintf('%s.%s', $type->getName(), $field->getName())); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
//run extension in all queries |
48
|
|
|
foreach ($endpoint->allQueries() as $query) { |
49
|
|
|
$this->validateDefinition($query); |
50
|
|
|
foreach ($query->getArguments() as $argument) { |
51
|
|
|
$this->validateDefinition($argument, $query->getName()); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
//run extensions in all mutations |
56
|
|
|
foreach ($endpoint->allMutations() as $mutation) { |
57
|
|
|
$this->validateDefinition($mutation); |
58
|
|
|
foreach ($mutation->getArguments() as $argument) { |
59
|
|
|
$this->validateDefinition($argument, $mutation->getName()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function validateDefinition(DefinitionInterface $definition, string $context = null) |
65
|
|
|
{ |
66
|
|
|
if ($context) { |
67
|
|
|
$path = sprintf('%s.%s', $context, $definition->getName()); |
68
|
|
|
} else { |
69
|
|
|
$path = $definition->getName(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($definition instanceof TypeAwareDefinitionInterface) { |
73
|
|
|
if (!$definition->getType()) { |
74
|
|
|
throw new \RuntimeException(sprintf('"%s" does not have any type configured.', $path)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
try { |
78
|
|
|
TypeRegistry::has($definition->getType()); |
79
|
|
|
} catch (\UnexpectedValueException $exception) { |
80
|
|
|
throw new \RuntimeException(sprintf('The type "%s" used in "%s" is not a valid registered type.', $definition->getType(), $path)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|