|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Railt package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Railt\SDL\Validator; |
|
13
|
|
|
|
|
14
|
|
|
use GraphQL\Contracts\TypeSystem\DefinitionInterface; |
|
15
|
|
|
use GraphQL\Contracts\TypeSystem\SchemaInterface; |
|
16
|
|
|
use Phplrt\Source\Exception\NotAccessibleException; |
|
17
|
|
|
use Railt\SDL\Exception\TypeErrorException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class SchemaValidator |
|
21
|
|
|
*/ |
|
22
|
|
|
class SchemaValidator extends Validator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private const ERROR_SCHEMA_OPERATION_TYPE = 'Schema %s operation should be defined by GraphQL Object type'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param DefinitionInterface $type |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function match(DefinitionInterface $type): bool |
|
34
|
|
|
{ |
|
35
|
|
|
return $type instanceof SchemaInterface; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param DefinitionInterface|SchemaInterface $schema |
|
40
|
|
|
* @return void |
|
41
|
|
|
* @throws TypeErrorException |
|
42
|
|
|
* @throws NotAccessibleException |
|
43
|
|
|
* @throws \RuntimeException |
|
44
|
|
|
*/ |
|
45
|
|
|
public function assert(DefinitionInterface $schema): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->assertOperation(fn() => $schema->getQueryType(), 'query'); |
|
|
|
|
|
|
48
|
|
|
$this->assertOperation(fn() => $schema->getMutationType(), 'mutation'); |
|
|
|
|
|
|
49
|
|
|
$this->assertOperation(fn() => $schema->getSubscriptionType(), 'subscription'); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
foreach ($schema->getTypeMap() as $type) { |
|
|
|
|
|
|
52
|
|
|
$this->validate($type); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
foreach ($schema->getDirectives() as $directive) { |
|
|
|
|
|
|
56
|
|
|
$this->validate($directive); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \Closure $closure |
|
62
|
|
|
* @param string $operation |
|
63
|
|
|
* @return void |
|
64
|
|
|
* @throws TypeErrorException |
|
65
|
|
|
* @throws NotAccessibleException |
|
66
|
|
|
* @throws \RuntimeException |
|
67
|
|
|
*/ |
|
68
|
|
|
private function assertOperation(\Closure $closure, string $operation): void |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
$closure(); |
|
72
|
|
|
} catch (\Throwable $e) { |
|
73
|
|
|
throw new TypeErrorException(\sprintf(self::ERROR_SCHEMA_OPERATION_TYPE, $operation)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|