|
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 $caps |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $caps; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var bool $use_for_input |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $use_for_input = true; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var bool $use_for_output |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $use_for_output = true; |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param string|string[] $type |
|
67
|
|
|
* @param string|null $key |
|
68
|
|
|
* @param string $description |
|
69
|
|
|
* @param callable|null $formatter |
|
70
|
|
|
* @param callable|null $resolver |
|
71
|
|
|
* @param array $caps |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct( |
|
74
|
|
|
$name, |
|
75
|
|
|
$type, |
|
76
|
|
|
$key = null, |
|
77
|
|
|
$description = '', |
|
78
|
|
|
callable $formatter = null, |
|
79
|
|
|
callable $resolver = null, |
|
80
|
|
|
array $caps = [] |
|
81
|
|
|
) { |
|
82
|
|
|
$this->name = $name; |
|
83
|
|
|
$this->type = $type; |
|
84
|
|
|
$this->key = $key; |
|
85
|
|
|
$this->description = $description; |
|
86
|
|
|
$this->formatter = $formatter; |
|
87
|
|
|
$this->resolver = $resolver; |
|
88
|
|
|
$this->caps = $caps; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function caps() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->caps; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function description() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->description; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function key() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->key; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function name() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->name; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return string|string[] |
|
130
|
|
|
*/ |
|
131
|
|
|
public function type() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->type; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Convert the field to array to be |
|
139
|
|
|
* able to pass as config to WP GraphQL |
|
140
|
|
|
* |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public function toArray() |
|
144
|
|
|
{ |
|
145
|
|
|
return get_object_vars($this); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Sets the value for use_for_input. |
|
151
|
|
|
* |
|
152
|
|
|
* @param bool $use_for_input |
|
153
|
|
|
* |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function setUseForInput($use_for_input) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->use_for_input = filter_var($use_for_input, FILTER_VALIDATE_BOOLEAN); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Whether the field should be used for |
|
163
|
|
|
* mutation inputs. |
|
164
|
|
|
* |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function useForInput() |
|
168
|
|
|
{ |
|
169
|
|
|
return (bool) $this->use_for_input; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Whether the field should be used for |
|
175
|
|
|
* query outputs. |
|
176
|
|
|
* |
|
177
|
|
|
* @return bool |
|
178
|
|
|
*/ |
|
179
|
|
|
public function useForOutput() |
|
180
|
|
|
{ |
|
181
|
|
|
return (bool) $this->use_for_output; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets the value for use_for_output |
|
187
|
|
|
* |
|
188
|
|
|
* @param bool $use_for_output |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function setUseForOutput($use_for_output) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->use_for_output = filter_var($use_for_output, FILTER_VALIDATE_BOOLEAN); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Whether the field should resolve |
|
198
|
|
|
* based on the user caps etc. |
|
199
|
|
|
* |
|
200
|
|
|
* @return boolean |
|
201
|
|
|
*/ |
|
202
|
|
|
public function shouldResolve() |
|
203
|
|
|
{ |
|
204
|
|
|
foreach ($this->caps as $cap) { |
|
205
|
|
|
if (! current_user_can($cap)) { |
|
206
|
|
|
return false; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
return true; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Whether the field has an explicit resolver set. |
|
215
|
|
|
* |
|
216
|
|
|
* @return boolean |
|
217
|
|
|
*/ |
|
218
|
|
|
public function hasInternalResolver() |
|
219
|
|
|
{ |
|
220
|
|
|
return is_callable($this->resolver); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Whether the field has an explicit resolver set. |
|
226
|
|
|
* |
|
227
|
|
|
* @param mixed $source The source that's passed down the GraphQL queries |
|
228
|
|
|
* @param array $args The inputArgs on the field |
|
229
|
|
|
* @param AppContext $context The AppContext passed down the GraphQL tree |
|
230
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
231
|
|
|
* @return mixed |
|
232
|
|
|
* @throws LogicException |
|
233
|
|
|
*/ |
|
234
|
|
|
public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
|
235
|
|
|
{ |
|
236
|
|
|
if (! $this->hasInternalResolver()) { |
|
237
|
|
|
throw new LogicException('GraphQLField has no internal resolver.'); |
|
238
|
|
|
} |
|
239
|
|
|
// dynamic methods using $this don't play nice |
|
240
|
|
|
// so capture resolver to a single var first |
|
241
|
|
|
$resolver = $this->resolver; |
|
242
|
|
|
return $resolver($source, $args, $context, $info); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Checks if the format callback is set. |
|
248
|
|
|
* If yes, then uses it to format the value. |
|
249
|
|
|
* |
|
250
|
|
|
* @param mixed $value |
|
251
|
|
|
* @return mixed The formatted value. |
|
252
|
|
|
*/ |
|
253
|
|
|
public function mayBeFormatValue($value) |
|
254
|
|
|
{ |
|
255
|
|
|
if (is_callable($this->formatter)) { |
|
256
|
|
|
// dynamic methods using $this don't play nice |
|
257
|
|
|
// so capture formatter to a single var first |
|
258
|
|
|
$formatter = $this->formatter; |
|
259
|
|
|
return $formatter($value); |
|
260
|
|
|
} |
|
261
|
|
|
return $value; |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|