|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is a part of GraphQL project. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Alexandr Viniychuk <[email protected]> |
|
6
|
|
|
* created: 4/30/16 9:11 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Youshido\GraphQL\Validator\SchemaValidator; |
|
10
|
|
|
|
|
11
|
|
|
use Youshido\GraphQL\Field\Field; |
|
12
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
|
13
|
|
|
use Youshido\GraphQL\Type\InterfaceType\AbstractInterfaceType; |
|
14
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
15
|
|
|
use Youshido\GraphQL\Validator\Exception\ConfigurationException; |
|
16
|
|
|
|
|
17
|
|
|
class SchemaValidator |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param AbstractSchema $schema |
|
22
|
|
|
* |
|
23
|
|
|
* @throws ConfigurationException |
|
24
|
|
|
*/ |
|
25
|
27 |
|
public function validate(AbstractSchema $schema) |
|
26
|
|
|
{ |
|
27
|
27 |
|
if (!$schema->getQueryType()->hasFields()) { |
|
28
|
2 |
|
throw new ConfigurationException('Schema has to have fields'); |
|
29
|
|
|
} |
|
30
|
25 |
|
foreach ($schema->getQueryType()->getConfig()->getFields() as $field) { |
|
|
|
|
|
|
31
|
25 |
|
if ($field->getType() instanceof AbstractObjectType) { |
|
32
|
23 |
|
$this->assertInterfaceImplementationCorrect($field->getType()); |
|
33
|
20 |
|
} |
|
34
|
22 |
|
} |
|
35
|
22 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param AbstractObjectType $type |
|
39
|
|
|
* |
|
40
|
|
|
* @throws ConfigurationException |
|
41
|
|
|
*/ |
|
42
|
23 |
|
protected function assertInterfaceImplementationCorrect(AbstractObjectType $type) |
|
43
|
|
|
{ |
|
44
|
23 |
|
if (!$type->getInterfaces()) { |
|
45
|
10 |
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
15 |
|
foreach ($type->getInterfaces() as $interface) { |
|
49
|
15 |
|
foreach ($interface->getConfig()->getFields() as $intField) { |
|
|
|
|
|
|
50
|
15 |
|
$this->assertFieldsIdentical($intField, $type->getConfig()->getField($intField->getName()), $interface); |
|
|
|
|
|
|
51
|
12 |
|
} |
|
52
|
12 |
|
} |
|
53
|
12 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Field $intField |
|
57
|
|
|
* @param Field $objField |
|
58
|
|
|
* @param AbstractInterfaceType $interface |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
* |
|
62
|
|
|
* @throws ConfigurationException |
|
63
|
|
|
*/ |
|
64
|
15 |
|
protected function assertFieldsIdentical($intField, $objField, AbstractInterfaceType $interface) |
|
65
|
|
|
{ |
|
66
|
15 |
|
$isValid = true; |
|
67
|
15 |
|
if ($intField->getType()->isCompositeType() !== $objField->getType()->isCompositeType()) { |
|
68
|
1 |
|
$isValid = false; |
|
69
|
1 |
|
} |
|
70
|
15 |
|
if ($intField->getType()->getNamedType()->getName() != $objField->getType()->getNamedType()->getName()) { |
|
71
|
2 |
|
$isValid = false; |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
15 |
|
if (!$isValid) { |
|
75
|
3 |
|
throw new ConfigurationException(sprintf('Implementation of %s is invalid for the field %s', $interface->getName(), $objField->getName())); |
|
76
|
|
|
} |
|
77
|
12 |
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: