-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectBuilder.php
More file actions
108 lines (89 loc) · 2.7 KB
/
ObjectBuilder.php
File metadata and controls
108 lines (89 loc) · 2.7 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
declare(strict_types=1);
namespace SimPod\GraphQLUtils\Builder;
use GraphQL\Type\Definition\FieldDefinition;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use function is_callable;
/**
* @see FieldDefinition
* @see ObjectType
*
* @phpstan-import-type FieldDefinitionConfig from FieldDefinition
* @phpstan-import-type ObjectConfig from ObjectType
*/
class ObjectBuilder extends TypeBuilder
{
/** @var InterfaceType[] */
private array $interfaces = [];
/** @var array<FieldDefinition|FieldDefinitionConfig>|callable():array<FieldDefinition|FieldDefinitionConfig> */
private $fields = [];
/** @var callable(mixed, array<mixed>, mixed, ResolveInfo) : mixed|null */
private $fieldResolver;
final private function __construct(private string|null $name)
{
}
/** @return static */
public static function create(string $name): self
{
return new static($name);
}
/** @return $this */
public function addInterface(InterfaceType $interfaceType): self
{
$this->interfaces[] = $interfaceType;
return $this;
}
/**
* @param array<FieldDefinition|FieldDefinitionConfig>|callable():array<FieldDefinition|FieldDefinitionConfig> $fields
*
* @return $this
*/
public function setFields(callable|array $fields): self
{
$this->fields = $fields;
return $this;
}
/**
* @param FieldDefinition|FieldDefinitionConfig $field
*
* @return $this
*/
public function addField(FieldDefinition|array $field): self
{
if (is_callable($this->fields)) {
$originalFields = $this->fields;
$closure = static function () use ($field, $originalFields): array {
$originalFields = $originalFields();
$originalFields[] = $field;
return $originalFields;
};
$this->fields = $closure;
} else {
$this->fields[] = $field;
}
return $this;
}
/**
* @param callable(mixed, array<mixed>, mixed, ResolveInfo) : mixed $fieldResolver
*
* @return $this
*/
public function setFieldResolver(callable $fieldResolver): self
{
$this->fieldResolver = $fieldResolver;
return $this;
}
/** @phpstan-return ObjectConfig */
public function build(): array
{
return [
'name' => $this->name,
'description' => $this->description,
'interfaces' => $this->interfaces,
'fields' => $this->fields,
'resolveField' => $this->fieldResolver,
];
}
}