1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\graphql\fields; |
4
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
6
|
|
|
use LogicException; |
7
|
|
|
use WPGraphQL\AppContext; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class GraphQLField |
11
|
|
|
* |
12
|
|
|
* @package Event Espresso |
13
|
|
|
* @author Manzoor Wani |
14
|
|
|
*/ |
15
|
|
|
class GraphQLField implements GraphQLFieldInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string $name |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string|string[] $type |
25
|
|
|
*/ |
26
|
|
|
protected $type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null $key |
30
|
|
|
*/ |
31
|
|
|
protected $key; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string $description |
35
|
|
|
*/ |
36
|
|
|
protected $description; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var callable $formatter |
40
|
|
|
*/ |
41
|
|
|
protected $formatter; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var callable $resolve |
45
|
|
|
*/ |
46
|
|
|
protected $resolver; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array $args |
50
|
|
|
*/ |
51
|
|
|
protected $args; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array $caps |
55
|
|
|
*/ |
56
|
|
|
protected $caps; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var bool $use_for_input |
60
|
|
|
*/ |
61
|
|
|
protected $use_for_input = true; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var bool $use_for_output |
65
|
|
|
*/ |
66
|
|
|
protected $use_for_output = true; |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $name |
71
|
|
|
* @param string|string[] $type |
72
|
|
|
* @param string|null $key |
73
|
|
|
* @param string $description |
74
|
|
|
* @param callable|null $formatter |
75
|
|
|
* @param callable|null $resolver |
76
|
|
|
* @param array $caps |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
$name, |
80
|
|
|
$type, |
81
|
|
|
$key = null, |
82
|
|
|
$description = '', |
83
|
|
|
callable $formatter = null, |
84
|
|
|
callable $resolver = null, |
85
|
|
|
array $args = [], |
86
|
|
|
array $caps = [] |
87
|
|
|
) { |
88
|
|
|
$this->name = $name; |
89
|
|
|
$this->type = $type; |
90
|
|
|
$this->key = $key; |
91
|
|
|
$this->description = $description; |
92
|
|
|
$this->formatter = $formatter; |
93
|
|
|
$this->resolver = $resolver; |
94
|
|
|
$this->args = $args; |
95
|
|
|
$this->caps = $caps; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function args() |
103
|
|
|
{ |
104
|
|
|
return $this->args; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function caps() |
112
|
|
|
{ |
113
|
|
|
return $this->caps; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function description() |
121
|
|
|
{ |
122
|
|
|
return $this->description; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
public function key() |
130
|
|
|
{ |
131
|
|
|
return $this->key; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function name() |
139
|
|
|
{ |
140
|
|
|
return $this->name; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string|string[] |
146
|
|
|
*/ |
147
|
|
|
public function type() |
148
|
|
|
{ |
149
|
|
|
return $this->type; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Convert the field to array to be |
155
|
|
|
* able to pass as config to WP GraphQL |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function toArray() |
160
|
|
|
{ |
161
|
|
|
return get_object_vars($this); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets the value for use_for_input. |
167
|
|
|
* |
168
|
|
|
* @param bool $use_for_input |
169
|
|
|
*/ |
170
|
|
|
protected function setUseForInput($use_for_input) |
171
|
|
|
{ |
172
|
|
|
$this->use_for_input = filter_var($use_for_input, FILTER_VALIDATE_BOOLEAN); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Whether the field should be used for |
178
|
|
|
* mutation inputs. |
179
|
|
|
* |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function useForInput() |
183
|
|
|
{ |
184
|
|
|
return (bool) $this->use_for_input; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Whether the field should be used for |
190
|
|
|
* query outputs. |
191
|
|
|
* |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
|
|
public function useForOutput() |
195
|
|
|
{ |
196
|
|
|
return (bool) $this->use_for_output; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets the value for use_for_output |
202
|
|
|
* |
203
|
|
|
* @param bool $use_for_output |
204
|
|
|
*/ |
205
|
|
|
protected function setUseForOutput($use_for_output) |
206
|
|
|
{ |
207
|
|
|
$this->use_for_output = filter_var($use_for_output, FILTER_VALIDATE_BOOLEAN); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Whether the field should resolve |
213
|
|
|
* based on the user caps etc. |
214
|
|
|
* |
215
|
|
|
* @return boolean |
216
|
|
|
*/ |
217
|
|
|
public function shouldResolve() |
218
|
|
|
{ |
219
|
|
|
foreach ($this->caps as $cap) { |
220
|
|
|
if (! current_user_can($cap)) { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
return true; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Whether the field has an explicit resolver set. |
230
|
|
|
* |
231
|
|
|
* @return boolean |
232
|
|
|
*/ |
233
|
|
|
public function hasInternalResolver() |
234
|
|
|
{ |
235
|
|
|
return is_callable($this->resolver); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Whether the field has an explicit resolver set. |
241
|
|
|
* |
242
|
|
|
* @param mixed $source The source that's passed down the GraphQL queries |
243
|
|
|
* @param array $args The inputArgs on the field |
244
|
|
|
* @param AppContext $context The AppContext passed down the GraphQL tree |
245
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
246
|
|
|
* @return mixed |
247
|
|
|
* @throws LogicException |
248
|
|
|
*/ |
249
|
|
|
public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
250
|
|
|
{ |
251
|
|
|
if (! $this->hasInternalResolver()) { |
252
|
|
|
throw new LogicException('GraphQLField has no internal resolver.'); |
253
|
|
|
} |
254
|
|
|
// dynamic methods using $this don't play nice |
255
|
|
|
// so capture resolver to a single var first |
256
|
|
|
$resolver = $this->resolver; |
257
|
|
|
return $resolver($source, $args, $context, $info); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Checks if the format callback is set. |
263
|
|
|
* If yes, then uses it to format the value. |
264
|
|
|
* |
265
|
|
|
* @param mixed $value |
266
|
|
|
* @return mixed The formatted value. |
267
|
|
|
*/ |
268
|
|
|
public function mayBeFormatValue($value) |
269
|
|
|
{ |
270
|
|
|
if (is_callable($this->formatter)) { |
271
|
|
|
// dynamic methods using $this don't play nice |
272
|
|
|
// so capture formatter to a single var first |
273
|
|
|
$formatter = $this->formatter; |
274
|
|
|
return $formatter($value); |
275
|
|
|
} |
276
|
|
|
return $value; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|