|
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
|
|
|
|
|
12
|
|
|
use Youshido\GraphQL\AbstractSchema; |
|
13
|
|
|
use Youshido\GraphQL\Type\Field\Field; |
|
14
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
|
15
|
|
|
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerInterface; |
|
16
|
|
|
use Youshido\GraphQL\Validator\ErrorContainer\ErrorContainerTrait; |
|
17
|
|
|
use Youshido\GraphQL\Validator\Exception\ConfigurationException; |
|
18
|
|
|
|
|
19
|
|
|
class SchemaValidator implements ErrorContainerInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use ErrorContainerTrait; |
|
22
|
|
|
|
|
23
|
19 |
|
public function validate(AbstractSchema $schema) |
|
24
|
|
|
{ |
|
25
|
|
|
try { |
|
26
|
19 |
|
foreach ($schema->getQueryType()->getConfig()->getFields() as $field) { |
|
27
|
18 |
|
if ($field->getType() instanceof AbstractObjectType) { |
|
28
|
19 |
|
$this->assertInterfaceImplementationCorrect($field->getType()); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} catch (\Exception $e) { |
|
32
|
|
|
$this->addError($e); |
|
33
|
|
|
|
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
19 |
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
18 |
|
protected function assertInterfaceImplementationCorrect(AbstractObjectType $type) |
|
41
|
|
|
{ |
|
42
|
18 |
|
if (!$type->getInterfaces()) return true; |
|
43
|
|
|
|
|
44
|
7 |
|
foreach ($type->getInterfaces() as $interface) { |
|
45
|
7 |
|
foreach ($interface->getConfig()->getFields() as $intField) { |
|
46
|
7 |
|
$this->assertFieldsIdentical($intField, $type->getConfig()->getField($intField->getName()), $interface); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
7 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Field $intField |
|
53
|
|
|
* @param Field $objField |
|
54
|
|
|
* @return bool |
|
55
|
|
|
* @throws ConfigurationException |
|
56
|
|
|
*/ |
|
57
|
7 |
|
protected function assertFieldsIdentical($intField, $objField, $interface) |
|
58
|
|
|
{ |
|
59
|
7 |
|
$intType = $intField->getConfig()->getType(); |
|
60
|
7 |
|
$objType = $objField->getConfig()->getType(); |
|
61
|
|
|
|
|
62
|
7 |
|
$isValid = true; |
|
63
|
7 |
|
if ($intType->getName() != $objType->getName()) { |
|
64
|
|
|
$isValid = false; |
|
65
|
|
|
} |
|
66
|
7 |
|
if ($intType->isCompositeType() && ($intType->getNamedType()->getName() != $objType->getNamedType()->getName())) { |
|
|
|
|
|
|
67
|
|
|
$isValid = false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
7 |
|
if (!$isValid) { |
|
71
|
|
|
throw new ConfigurationException(sprintf('Implementation of %s is invalid for the field %s', $interface->getName(), $objField->getName())); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
7 |
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.