|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\ValidationException; |
|
6
|
|
|
use Digia\GraphQL\Language\AST\Node\DocumentNode; |
|
7
|
|
|
use Digia\GraphQL\Language\AST\Node\FragmentDefinitionNode; |
|
8
|
|
|
use Digia\GraphQL\Type\Definition\Argument; |
|
9
|
|
|
use Digia\GraphQL\Type\Definition\Directive; |
|
10
|
|
|
use Digia\GraphQL\Type\Definition\Field; |
|
11
|
|
|
use Digia\GraphQL\Type\Definition\TypeInterface; |
|
12
|
|
|
use Digia\GraphQL\Type\SchemaInterface; |
|
13
|
|
|
use Digia\GraphQL\Util\TypeInfo; |
|
14
|
|
|
|
|
15
|
|
|
class ValidationContext |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var SchemaInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $schema; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var DocumentNode |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $documentNode; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TypeInfo |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $typeInfo; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array|ValidationException[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $errors = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array|FragmentDefinitionNode[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $fragments = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* ValidationContext constructor. |
|
44
|
|
|
* @param SchemaInterface $schema |
|
45
|
|
|
* @param DocumentNode $documentNode |
|
46
|
|
|
* @param TypeInfo $typeInfo |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(SchemaInterface $schema, DocumentNode $documentNode, TypeInfo $typeInfo) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->schema = $schema; |
|
51
|
|
|
$this->documentNode = $documentNode; |
|
52
|
|
|
$this->typeInfo = $typeInfo; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ValidationException $error |
|
57
|
|
|
*/ |
|
58
|
|
|
public function reportError(ValidationException $error): void |
|
59
|
|
|
{ |
|
60
|
|
|
$this->errors[] = $error; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array|ValidationException[] |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getErrors(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->errors; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return TypeInterface|null |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getParentType(): ?TypeInterface |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->typeInfo->getParentType(); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Field|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getFieldDefinition(): ?Field |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->typeInfo->getFieldDefinition(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return SchemaInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getSchema(): SchemaInterface |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->schema; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return Argument|null |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getArgument(): ?Argument |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->typeInfo->getArgument(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Directive|null |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getDirective(): ?Directive |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->typeInfo->getDirective(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $name |
|
113
|
|
|
* @return FragmentDefinitionNode|null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getFragment(string $name): ?FragmentDefinitionNode |
|
116
|
|
|
{ |
|
117
|
|
|
if (empty($this->fragments)) { |
|
118
|
|
|
$this->fragments = array_reduce($this->documentNode->getDefinitions(), function ($fragments, $definition) { |
|
119
|
|
|
if ($definition instanceof FragmentDefinitionNode) { |
|
120
|
|
|
$fragments[$definition->getNameValue()] = $definition; |
|
121
|
|
|
} |
|
122
|
|
|
return $fragments; |
|
123
|
|
|
}, []); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->fragments[$name] ?? null; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|