This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
UnionType.php
91 lines (80 loc) · 2.14 KB
/
UnionType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace Digia\GraphQL\Type\Definition;
use Digia\GraphQL\Behavior\ConfigTrait;
use Digia\GraphQL\Type\Definition\Behavior\DescriptionTrait;
use Digia\GraphQL\Language\AST\Node\NodeTrait;
use Digia\GraphQL\Language\AST\Node\UnionTypeDefinitionNode;
use Digia\GraphQL\Type\Definition\Behavior\NameTrait;
use Digia\GraphQL\Type\Definition\Behavior\ResolveTypeTrait;
use Digia\GraphQL\Type\Definition\Contract\AbstractTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\CompositeTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\OutputTypeInterface;
use Digia\GraphQL\Type\Definition\Contract\TypeInterface;
/**
* Union Type Definition
*
* When a field can return one of a heterogeneous set of types, a Union type
* is used to describe what types are possible as well as providing a function
* to determine which type is actually used when the field is resolved.
*
* Example:
*
* const PetType = new GraphQLUnionType({
* name: 'Pet',
* types: [ DogType, CatType ],
* resolveType(value) {
* if (value instanceof Dog) {
* return DogType;
* }
* if (value instanceof Cat) {
* return CatType;
* }
* }
* });
*
*/
/**
* Class UnionType
*
* @package Digia\GraphQL\Type\Definition
* @property UnionTypeDefinitionNode $astNode
*/
class UnionType implements TypeInterface, AbstractTypeInterface, CompositeTypeInterface, OutputTypeInterface
{
use NameTrait;
use DescriptionTrait;
use ResolveTypeTrait;
use NodeTrait;
use ConfigTrait;
/**
* @var TypeInterface[]
*/
private $types;
/**
* @return TypeInterface[]
*/
public function getTypes(): array
{
return $this->types;
}
/**
* @param TypeInterface $type
* @return $this
*/
public function addType(TypeInterface $type)
{
$this->types[] = $type;
return $this;
}
/**
* @param TypeInterface[] $types
* @return $this
*/
protected function setTypes(array $types)
{
foreach ($types as $type) {
$this->addType($type);
}
return $this;
}
}