1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bakery\Types\Definitions; |
4
|
|
|
|
5
|
|
|
use Bakery\Utils\Utils; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Bakery\Support\TypeRegistry; |
8
|
|
|
use Illuminate\Contracts\Auth\Access\Gate; |
9
|
|
|
use GraphQL\Type\Definition\Type as GraphQLType; |
10
|
|
|
use GraphQL\Type\Definition\NamedType as GraphQLNamedType; |
11
|
|
|
|
12
|
|
|
class RootType |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Bakery\Support\TypeRegistry |
16
|
|
|
*/ |
17
|
|
|
protected $registry; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Gate |
21
|
|
|
*/ |
22
|
|
|
protected $gate; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
26
|
|
|
*/ |
27
|
|
|
protected $model; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The name of the type. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The description of the type. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The underlying type. |
45
|
|
|
* |
46
|
|
|
* @var GraphQLNamedType |
47
|
|
|
*/ |
48
|
|
|
protected $type; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Whether the type is nullable. |
52
|
|
|
* |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $nullable = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Whether the type is a list. |
59
|
|
|
* |
60
|
|
|
* @var bool |
61
|
|
|
*/ |
62
|
|
|
protected $list = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Whether the items of the list are nullable. |
66
|
|
|
* |
67
|
|
|
* @var bool |
68
|
|
|
*/ |
69
|
|
|
protected $nullableItems = false; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Construct a new type. |
73
|
|
|
* |
74
|
|
|
* @param \Bakery\Support\TypeRegistry $registry |
75
|
|
|
* @param \GraphQL\Type\Definition\Type $type |
76
|
|
|
*/ |
77
|
|
|
public function __construct(TypeRegistry $registry, GraphQLType $type = null) |
78
|
|
|
{ |
79
|
|
|
$this->registry = $registry; |
80
|
|
|
|
81
|
|
|
if ($type) { |
82
|
|
|
$this->type = $type; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the type registry. |
88
|
|
|
* |
89
|
|
|
* @return \Bakery\Support\TypeRegistry |
90
|
|
|
*/ |
91
|
|
|
public function getRegistry(): TypeRegistry |
92
|
|
|
{ |
93
|
|
|
return $this->registry; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the type registry. |
98
|
|
|
* |
99
|
|
|
* @param \Bakery\Support\TypeRegistry $registry |
100
|
|
|
* @return \Bakery\Types\Definitions\RootType |
101
|
|
|
*/ |
102
|
|
|
public function setRegistry(TypeRegistry $registry): self |
103
|
|
|
{ |
104
|
|
|
$this->registry = $registry; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Define the underlying type. |
111
|
|
|
* |
112
|
|
|
* This can be overridden when extending the type. |
113
|
|
|
* |
114
|
|
|
* @return \GraphQL\Type\Definition\NamedType |
115
|
|
|
*/ |
116
|
|
|
protected function type(): GraphQLNamedType |
117
|
|
|
{ |
118
|
|
|
return $this->type; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the underlying type. |
123
|
|
|
* |
124
|
|
|
* @return \GraphQL\Type\Definition\NamedType |
125
|
|
|
*/ |
126
|
|
|
public function getType(): GraphQLNamedType |
127
|
|
|
{ |
128
|
|
|
return $this->type(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set a description. |
133
|
|
|
* |
134
|
|
|
* @param string $value |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function description(string $value) |
138
|
|
|
{ |
139
|
|
|
$this->description = $value; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Define if the type is nullable. |
146
|
|
|
* |
147
|
|
|
* @param bool value |
|
|
|
|
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function nullable(bool $value = true) |
151
|
|
|
{ |
152
|
|
|
$this->nullable = $value; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return if the type is nullable. |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function isNullable(): bool |
163
|
|
|
{ |
164
|
|
|
return $this->nullable; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Define if the type is a list. |
169
|
|
|
* |
170
|
|
|
* @param bool|null $value |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
public function list(bool $value = true) |
174
|
|
|
{ |
175
|
|
|
$this->list = $value; |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Returns if the type is a list. |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function isList(): bool |
186
|
|
|
{ |
187
|
|
|
return $this->list; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns if the list has nullable items. |
192
|
|
|
* |
193
|
|
|
* @return bool |
194
|
|
|
*/ |
195
|
|
|
public function hasNullableItems(): bool |
196
|
|
|
{ |
197
|
|
|
return $this->nullableItems; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Define if the items are nullable. |
202
|
|
|
* |
203
|
|
|
* @param bool $value |
204
|
|
|
*/ |
205
|
|
|
public function nullableItems(bool $value = true) |
206
|
|
|
{ |
207
|
|
|
$this->nullableItems = $value; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set a name on the type. |
212
|
|
|
* |
213
|
|
|
* @param $name |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function setName($name) |
217
|
|
|
{ |
218
|
|
|
$this->name = $name; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Define the name of the type. |
225
|
|
|
* |
226
|
|
|
* This method can be overridden when extending the type. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
protected function name(): string |
231
|
|
|
{ |
232
|
|
|
if (isset($this->name)) { |
233
|
|
|
return $this->name; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return Utils::typename(Str::before(class_basename($this), 'Type')); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get the name of the type. |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function getName(): string |
245
|
|
|
{ |
246
|
|
|
return $this->name(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Returns if the underlying type is a leaf type. |
251
|
|
|
* |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
public function isLeafType(): bool |
255
|
|
|
{ |
256
|
|
|
if ($this->isList()) { |
257
|
|
|
return false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return GraphQLType::isLeafType($this->getType()); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Convert the Bakery type to a GraphQL type. |
265
|
|
|
* |
266
|
|
|
* @return GraphQLType |
267
|
|
|
*/ |
268
|
|
|
public function toType(): GraphQLType |
269
|
|
|
{ |
270
|
|
|
$type = $this->getType(); |
271
|
|
|
|
272
|
|
|
if ($this->isList()) { |
273
|
|
|
$type = $this->hasNullableItems() ? $type : GraphQLType::nonNull($type); |
274
|
|
|
$type = $this->isNullable() |
275
|
|
|
? GraphQLType::listOf($type) |
276
|
|
|
: GraphQLType::nonNull(GraphQLType::listOf($type)); |
277
|
|
|
} else { |
278
|
|
|
$type = $this->isNullable() ? $type : GraphQLType::nonNull($type); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $type; |
|
|
|
|
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Convert the Bakery type to a GraphQL (named) type. |
286
|
|
|
* |
287
|
|
|
* @return \GraphQL\Type\Definition\NamedType |
288
|
|
|
*/ |
289
|
|
|
public function toNamedType(): GraphQLNamedType |
290
|
|
|
{ |
291
|
|
|
return $this->getType(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Invoked when the object is being serialized. |
296
|
|
|
* |
297
|
|
|
* @return array |
298
|
|
|
*/ |
299
|
|
|
public function __sleep() |
300
|
|
|
{ |
301
|
|
|
return [ |
302
|
|
|
'type', |
303
|
|
|
'list', |
304
|
|
|
'nullable', |
305
|
|
|
'registry', |
306
|
|
|
]; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Invoked when the object is unserialized. |
311
|
|
|
*/ |
312
|
|
|
public function __wakeup() |
313
|
|
|
{ |
314
|
|
|
// |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..