|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Builder; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\SDL\Ast\Type\NamedTypeNode; |
|
13
|
|
|
use GraphQL\TypeSystem\Type\UnionType; |
|
14
|
|
|
use GraphQL\TypeSystem\Type\ObjectType; |
|
15
|
|
|
use Railt\SDL\Ast\Generic\TypeDefinitionCollection; |
|
16
|
|
|
use GraphQL\Contracts\TypeSystem\Type\TypeInterface; |
|
17
|
|
|
use Railt\SDL\Ast\Definition\UnionTypeDefinitionNode; |
|
18
|
|
|
use GraphQL\Contracts\TypeSystem\DefinitionInterface; |
|
19
|
|
|
use Railt\SDL\Ast\Generic\InterfaceImplementsCollection; |
|
20
|
|
|
use GraphQL\Contracts\TypeSystem\Type\NamedTypeInterface; |
|
21
|
|
|
use GraphQL\Contracts\TypeSystem\Type\UnionTypeInterface; |
|
22
|
|
|
use GraphQL\Contracts\TypeSystem\Type\ObjectTypeInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @property UnionTypeDefinitionNode $ast |
|
26
|
|
|
*/ |
|
27
|
|
|
class UnionTypeBuilder extends TypeBuilder |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @return UnionTypeInterface|DefinitionInterface |
|
31
|
|
|
* @throws \RuntimeException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function build(): UnionTypeInterface |
|
34
|
|
|
{ |
|
35
|
|
|
$union = new UnionType([ |
|
36
|
|
|
'name' => $this->ast->name->value, |
|
37
|
|
|
'description' => $this->value($this->ast->description), |
|
38
|
|
|
]); |
|
39
|
|
|
|
|
40
|
|
|
$this->register($union); |
|
41
|
|
|
|
|
42
|
|
|
if ($this->ast->types) { |
|
43
|
|
|
$union = $union->withTypes($this->buildTypes($this->ast->types)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $union; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param TypeDefinitionCollection|NamedTypeNode[]|null $types |
|
51
|
|
|
* @return \Traversable|TypeInterface[] |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function buildTypes(?TypeDefinitionCollection $types): \Traversable |
|
54
|
|
|
{ |
|
55
|
|
|
foreach ($types as $type) { |
|
56
|
|
|
yield $this->fetch($type->name->value); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|