|
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\Backend\Context; |
|
13
|
|
|
|
|
14
|
|
|
use GraphQL\Contracts\TypeSystem\DirectiveInterface; |
|
15
|
|
|
use Railt\SDL\Backend\Context; |
|
16
|
|
|
use Railt\SDL\Frontend\Ast\Definition\DirectiveDefinitionNode; |
|
17
|
|
|
use Railt\TypeSystem\Directive; |
|
18
|
|
|
use Railt\TypeSystem\Exception\TypeUniquenessException; |
|
19
|
|
|
use Railt\TypeSystem\Schema; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @property-read DirectiveDefinitionNode $ast |
|
23
|
|
|
* @method DirectiveDefinitionNode getAst() |
|
24
|
|
|
*/ |
|
25
|
|
|
class DirectiveContext extends DefinitionContext |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* DirectiveContext constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Context $context |
|
31
|
|
|
* @param Schema $schema |
|
32
|
|
|
* @param DirectiveDefinitionNode $ast |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Context $context, Schema $schema, DirectiveDefinitionNode $ast) |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct($context, $schema, $ast); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritDoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getName(): string |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->ast->name->value; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param array $variables |
|
49
|
|
|
* @return DirectiveInterface |
|
50
|
|
|
* @throws TypeUniquenessException |
|
51
|
|
|
* @throws \InvalidArgumentException |
|
52
|
|
|
* @throws \Throwable |
|
53
|
|
|
*/ |
|
54
|
|
|
public function resolve(array $variables = []): DirectiveInterface |
|
55
|
|
|
{ |
|
56
|
|
|
$ast = $this->precompile($this->ast, $variables); |
|
57
|
|
|
|
|
58
|
|
|
$directive = new Directive($ast->name->value, [ |
|
59
|
|
|
'description' => $this->descriptionOf($ast), |
|
60
|
|
|
'repeatable' => $ast->repeatable !== null, |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($ast->arguments as $argument) { |
|
64
|
|
|
$directive->addArgument($this->buildArgumentDefinition($argument)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach ($ast->locations as $location) { |
|
68
|
|
|
$directive->addLocation($location->name->value); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $directive; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|