1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Definition\Plugin; |
12
|
|
|
|
13
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
14
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
15
|
|
|
use Ynlo\GraphQLBundle\Model\ConstraintViolation; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\ConstraintViolationParameter; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This plugin remove the field constraintViolations and all related types |
20
|
|
|
* when violations should be located only in errors. |
21
|
|
|
*/ |
22
|
|
|
class ConstraintViolationsDefinitionPlugin extends AbstractDefinitionPlugin |
23
|
|
|
{ |
24
|
|
|
protected $config; |
25
|
|
|
|
26
|
2 |
|
public function __construct($config = []) |
27
|
|
|
{ |
28
|
2 |
|
$this->config = $config; |
29
|
2 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
2 |
|
public function configureEndpoint(Endpoint $endpoint): void |
35
|
|
|
{ |
36
|
2 |
|
if (($this->config['validation_messages'] ?? null) !== 'error') { |
37
|
2 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($endpoint->hasTypeForClass(ConstraintViolation::class)) { |
41
|
|
|
$violationType = $endpoint->getType(ConstraintViolation::class); |
42
|
|
|
foreach ($endpoint->allTypes() as $type) { |
43
|
|
|
if ($type instanceof FieldsAwareDefinitionInterface) { |
44
|
|
|
foreach ($type->getFields() as $field) { |
45
|
|
|
if ($endpoint->hasType($field->getType())) { |
46
|
|
|
if ($endpoint->getType($field->getType())->getName() === $violationType->getName()) { |
47
|
|
|
$type->removeField($field->getName()); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$endpoint->removeType($violationType->getName()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($endpoint->hasTypeForClass(ConstraintViolationParameter::class)) { |
58
|
|
|
$endpoint->removeType($endpoint->getTypeForClass(ConstraintViolationParameter::class)); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|