|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Type\Definition; |
|
4
|
|
|
|
|
5
|
|
|
use Digia\GraphQL\Config\ConfigObject; |
|
6
|
|
|
use Digia\GraphQL\Error\InvariantException; |
|
7
|
|
|
use Digia\GraphQL\Language\Node\NodeAwareInterface; |
|
8
|
|
|
use Digia\GraphQL\Language\Node\NodeTrait; |
|
9
|
|
|
use Digia\GraphQL\Language\Node\UnionTypeDefinitionNode; |
|
10
|
|
|
use function Digia\GraphQL\Type\resolveThunk; |
|
11
|
|
|
use function Digia\GraphQL\Util\invariant; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Union Type Definition |
|
15
|
|
|
* |
|
16
|
|
|
* When a field can return one of a heterogeneous set of types, a Union type |
|
17
|
|
|
* is used to describe what types are possible as well as providing a function |
|
18
|
|
|
* to determine which type is actually used when the field is resolved. |
|
19
|
|
|
* |
|
20
|
|
|
* Example: |
|
21
|
|
|
* |
|
22
|
|
|
* $PetType = GraphQLUnionType([ |
|
23
|
|
|
* 'name' => 'Pet', |
|
24
|
|
|
* 'types' => [$DogType, $CatType], |
|
25
|
|
|
* 'resolveType' => function ($value) { |
|
26
|
|
|
* if ($value instanceof Dog) { |
|
27
|
|
|
* return $DogType; |
|
28
|
|
|
* } |
|
29
|
|
|
* if ($value instanceof Cat) { |
|
30
|
|
|
* return $CatType; |
|
31
|
|
|
* } |
|
32
|
|
|
* } |
|
33
|
|
|
* ]); |
|
34
|
|
|
*/ |
|
35
|
|
|
class UnionType extends ConfigObject implements AbstractTypeInterface, NamedTypeInterface, CompositeTypeInterface, |
|
36
|
|
|
OutputTypeInterface, NodeAwareInterface |
|
37
|
|
|
{ |
|
38
|
|
|
use NameTrait; |
|
39
|
|
|
use DescriptionTrait; |
|
40
|
|
|
use ResolveTypeTrait; |
|
41
|
|
|
use NodeTrait; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array|callable |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $typesOrThunk; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var TypeInterface[] |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $typeMap; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function afterConfig(): void |
|
57
|
|
|
{ |
|
58
|
|
|
invariant(null !== $this->getName(), 'Must provide name.'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return NamedTypeInterface[] |
|
63
|
|
|
* @throws InvariantException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getTypes(): array |
|
66
|
|
|
{ |
|
67
|
|
|
// Types are built lazily to avoid concurrency issues. |
|
68
|
|
|
if (!isset($this->typeMap)) { |
|
69
|
|
|
$this->typeMap = $this->buildTypeMap($this->typesOrThunk ?? []); |
|
70
|
|
|
} |
|
71
|
|
|
return $this->typeMap; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Unions are created using the `ConfigAwareTrait` constructor which will automatically |
|
76
|
|
|
* call this method when setting arguments from `$config['types']`. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array|callable $typesOrThunk |
|
79
|
|
|
* @return UnionType |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function setTypes($typesOrThunk): UnionType |
|
82
|
|
|
{ |
|
83
|
|
|
$this->typesOrThunk = $typesOrThunk; |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array|callable $typesOrThunk |
|
89
|
|
|
* @return array |
|
90
|
|
|
* @throws InvariantException |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function buildTypeMap($typesOrThunk): array |
|
93
|
|
|
{ |
|
94
|
|
|
$typeMap = resolveThunk($typesOrThunk); |
|
95
|
|
|
|
|
96
|
|
|
invariant( |
|
97
|
|
|
\is_array($typeMap), |
|
98
|
|
|
\sprintf( |
|
99
|
|
|
'Must provide Array of types or a function which returns such an array for Union %s.', |
|
100
|
|
|
$this->getName() |
|
101
|
|
|
) |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
return $typeMap; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|