-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathObjectType.php
More file actions
182 lines (162 loc) · 5.53 KB
/
ObjectType.php
File metadata and controls
182 lines (162 loc) · 5.53 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php declare(strict_types=1);
namespace GraphQL\Type\Definition;
use GraphQL\Deferred;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\Executor;
use GraphQL\Language\AST\DirectiveNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use GraphQL\Utils\Utils;
/**
* Object Type Definition.
*
* Most GraphQL types you define will be object types.
* Object types have a name, but most importantly describe their fields.
*
* Example:
*
* $AddressType = new ObjectType([
* 'name' => 'Address',
* 'fields' => [
* 'street' => GraphQL\Type\Definition\Type::string(),
* 'number' => GraphQL\Type\Definition\Type::int(),
* 'formatted' => [
* 'type' => GraphQL\Type\Definition\Type::string(),
* 'resolve' => fn (AddressModel $address): string => "{$address->number} {$address->street}",
* ],
* ],
* ]);
*
* When two types need to refer to each other, or a type needs to refer to
* itself in a field, you can use a function expression (aka a closure or a
* thunk) to supply the fields lazily.
*
* Example:
*
* $PersonType = null;
* $PersonType = new ObjectType([
* 'name' => 'Person',
* 'fields' => fn (): array => [
* 'name' => GraphQL\Type\Definition\Type::string(),
* 'bestFriend' => $PersonType,
* ],
* ]);
*
* @phpstan-import-type FieldResolver from Executor
* @phpstan-import-type ArgsMapper from Executor
*
* @phpstan-type InterfaceTypeReference InterfaceType|callable(): InterfaceType
* @phpstan-type ObjectConfig array{
* name?: string|null,
* description?: string|null,
* resolveField?: FieldResolver|null,
* argsMapper?: ArgsMapper|null,
* fields: (callable(): iterable<mixed>)|iterable<mixed>,
* interfaces?: iterable<InterfaceTypeReference>|callable(): iterable<InterfaceTypeReference>,
* isTypeOf?: (callable(mixed $objectValue, mixed $context, ResolveInfo $resolveInfo): (bool|Deferred|null))|null,
* directives?: array<DirectiveNode>|null,
* astNode?: ObjectTypeDefinitionNode|null,
* extensionASTNodes?: array<ObjectTypeExtensionNode>|null
* }
*/
class ObjectType extends Type implements OutputType, CompositeType, NullableType, HasFieldsType, NamedType, ImplementingType
{
use HasFieldsTypeImplementation;
use NamedTypeImplementation;
use ImplementingTypeImplementation;
public ?ObjectTypeDefinitionNode $astNode;
/** @var array<ObjectTypeExtensionNode> */
public array $extensionASTNodes;
/** @var array<DirectiveNode> */
public array $directives;
/**
* @var callable|null
*
* @phpstan-var FieldResolver|null
*/
public $resolveFieldFn;
/**
* @var callable|null
*
* @phpstan-var ArgsMapper|null
*/
public $argsMapper;
/** @phpstan-var ObjectConfig */
public array $config;
/**
* @phpstan-param ObjectConfig $config
*
* @throws InvariantViolation
*/
public function __construct(array $config)
{
$this->name = $config['name'] ?? $this->inferName();
$this->description = $config['description'] ?? null;
$this->resolveFieldFn = $config['resolveField'] ?? null;
$this->argsMapper = $config['argsMapper'] ?? null;
$this->astNode = $config['astNode'] ?? null;
$this->extensionASTNodes = $config['extensionASTNodes'] ?? [];
$this->directives = $config['directives'] ?? [];
$this->config = $config;
}
/**
* @param mixed $type
*
* @throws InvariantViolation
*/
public static function assertObjectType($type): self
{
if (! $type instanceof self) {
$notObjectType = Utils::printSafe($type);
throw new InvariantViolation("Expected {$notObjectType} to be a GraphQL Object type.");
}
return $type;
}
/**
* @param mixed $objectValue The resolved value for the object type
* @param mixed $context The context that was passed to GraphQL::execute()
*
* @return bool|Deferred|null
*/
public function isTypeOf($objectValue, $context, ResolveInfo $info)
{
return isset($this->config['isTypeOf'])
? $this->config['isTypeOf'](
$objectValue,
$context,
$info
)
: null;
}
/**
* Validates type config and throws if one of the type options is invalid.
* Note: this method is shallow, it won't validate object fields and their arguments.
*
* @throws Error
* @throws InvariantViolation
*/
public function assertValid(): void
{
Utils::assertValidName($this->name);
$isTypeOf = $this->config['isTypeOf'] ?? null;
// @phpstan-ignore-next-line unnecessary according to types, but can happen during runtime
if (isset($isTypeOf) && ! is_callable($isTypeOf)) {
$notCallable = Utils::printSafe($isTypeOf);
throw new InvariantViolation("{$this->name} must provide \"isTypeOf\" as null or a callable, but got: {$notCallable}.");
}
foreach ($this->getFields() as $field) {
$field->assertValid($this);
}
$this->assertValidInterfaces();
}
public function astNode(): ?ObjectTypeDefinitionNode
{
return $this->astNode;
}
/** @return array<ObjectTypeExtensionNode> */
public function extensionASTNodes(): array
{
return $this->extensionASTNodes;
}
}