|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\resolvers; |
|
4
|
|
|
|
|
5
|
|
|
use EE_Attendee; |
|
6
|
|
|
use EE_Base_Class; |
|
7
|
|
|
use EE_Event; |
|
8
|
|
|
use EE_Venue; |
|
9
|
|
|
use EE_Datetime; |
|
10
|
|
|
use EE_Ticket; |
|
11
|
|
|
use EE_State; |
|
12
|
|
|
use EEM_State; |
|
13
|
|
|
use EEM_Country; |
|
14
|
|
|
use EE_Error; |
|
15
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
16
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
17
|
|
|
use EventEspresso\core\exceptions\UnexpectedEntityException; |
|
18
|
|
|
use EventEspresso\core\services\graphql\resolvers\ResolverBase; |
|
19
|
|
|
use EventEspresso\core\services\graphql\fields\GraphQLField; |
|
20
|
|
|
use Exception; |
|
21
|
|
|
use InvalidArgumentException; |
|
22
|
|
|
use ReflectionException; |
|
23
|
|
|
use GraphQL\Deferred; |
|
24
|
|
|
use GraphQL\Error\UserError; |
|
25
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
26
|
|
|
use GraphQLRelay\Relay; |
|
27
|
|
|
use WPGraphQL\AppContext; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class FieldResolver |
|
31
|
|
|
* Field resolver for a GraphQL type |
|
32
|
|
|
* |
|
33
|
|
|
* @package EventEspresso\core\domain\services\graphql\resolvers |
|
34
|
|
|
* @author Manzoor Wani |
|
35
|
|
|
* @since $VID:$ |
|
36
|
|
|
*/ |
|
37
|
|
|
class FieldResolver extends ResolverBase |
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var mixed $model |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $model; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array $fields . |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $fields; |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* FieldResolver constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $model The model instance. |
|
55
|
|
|
* @param array $fields The fields registered for the type. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct($model, array $fields) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->model = $model; |
|
60
|
|
|
$this->fields = $fields; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param mixed $source The source that's passed down the GraphQL queries |
|
66
|
|
|
* @param array $args The inputArgs on the field |
|
67
|
|
|
* @param AppContext $context The AppContext passed down the GraphQL tree |
|
68
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
69
|
|
|
* @return string |
|
70
|
|
|
* @throws EE_Error |
|
71
|
|
|
* @throws Exception |
|
72
|
|
|
* @throws InvalidArgumentException |
|
73
|
|
|
* @throws InvalidDataTypeException |
|
74
|
|
|
* @throws InvalidInterfaceException |
|
75
|
|
|
* @throws ReflectionException |
|
76
|
|
|
* @throws UserError |
|
77
|
|
|
* @throws UnexpectedEntityException |
|
78
|
|
|
* @since $VID:$ |
|
79
|
|
|
*/ |
|
80
|
|
|
public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
|
81
|
|
|
{ |
|
82
|
|
|
$fieldName = $info->fieldName; |
|
83
|
|
|
$field = isset($this->fields[ $fieldName ]) ? $this->fields[ $fieldName ] : null; |
|
84
|
|
|
// Field should exist in teh registered fields |
|
85
|
|
|
if ($field instanceof GraphQLField) { |
|
86
|
|
|
// check if the field should be resolved. |
|
87
|
|
|
if (! $field->shouldResolve()) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
// Give priority to the internal resolver. |
|
91
|
|
|
if ($field->hasInternalResolver()) { |
|
92
|
|
|
return $field->resolve($source, $args, $context, $info); |
|
93
|
|
|
} |
|
94
|
|
|
if (! ($source instanceof EE_Base_Class)) { |
|
95
|
|
|
return null; |
|
96
|
|
|
} |
|
97
|
|
|
// Check if the field has a key mapped to model. |
|
98
|
|
|
if (! empty($field->key())) { |
|
99
|
|
|
$value = $source->{$field->key()}(); |
|
100
|
|
|
return $field->mayBeFormatValue($value, $source); |
|
101
|
|
|
} |
|
102
|
|
|
switch ($fieldName) { |
|
103
|
|
|
case 'id': // the global ID |
|
104
|
|
|
return $this->resolveId($source); |
|
105
|
|
|
case 'cacheId': |
|
106
|
|
|
return $this->resolveCacheId($source); |
|
107
|
|
|
case 'parent': |
|
108
|
|
|
return $this->resolveParent($source); |
|
109
|
|
|
case 'event': |
|
110
|
|
|
return $this->resolveEvent($source, $args, $context); |
|
111
|
|
|
case 'wpUser': |
|
112
|
|
|
case 'manager': |
|
113
|
|
|
return $this->resolveWpUser($source, $args, $context); |
|
114
|
|
|
case 'userId': |
|
115
|
|
|
return $this->resolveUserId($source, $args, $context); |
|
116
|
|
|
case 'state': // Venue |
|
117
|
|
|
return $this->resolveState($source); |
|
118
|
|
|
case 'country': // State, Venue |
|
119
|
|
|
return $this->resolveCountry($source); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return null; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Resolve the global ID |
|
128
|
|
|
* |
|
129
|
|
|
* @param mixed $source |
|
130
|
|
|
* @return string|null |
|
131
|
|
|
* @throws Exception |
|
132
|
|
|
* @since $VID:$ |
|
133
|
|
|
*/ |
|
134
|
|
|
public function resolveId($source) |
|
135
|
|
|
{ |
|
136
|
|
|
$ID = $source instanceof EE_Base_Class ? $source->ID() : 0; |
|
137
|
|
|
|
|
138
|
|
|
return $ID ? Relay::toGlobalId($this->model->item_name(), $ID) : null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Resolve the cache ID |
|
144
|
|
|
* |
|
145
|
|
|
* @param mixed $source |
|
146
|
|
|
* @return string |
|
147
|
|
|
* @since $VID:$ |
|
148
|
|
|
*/ |
|
149
|
|
|
public function resolveCacheId($source) |
|
150
|
|
|
{ |
|
151
|
|
|
$model_name = $this->model->item_name(); |
|
152
|
|
|
$ID = $source->ID(); |
|
153
|
|
|
// Since cacheId does not need to be globally unique |
|
154
|
|
|
// $uniqid is sufficient, still adding the model name and ID |
|
155
|
|
|
// may be we need/use them in future. |
|
156
|
|
|
return uniqid("{$model_name}:{$ID}:", true); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param mixed $source |
|
162
|
|
|
* @param $args |
|
163
|
|
|
* @param $context |
|
164
|
|
|
* @return Deferred|null |
|
165
|
|
|
* @throws Exception |
|
166
|
|
|
* @since $VID:$ |
|
167
|
|
|
*/ |
|
168
|
|
|
public function resolveWpUser($source, $args, $context) |
|
169
|
|
|
{ |
|
170
|
|
|
$user_id = $source->wp_user(); |
|
171
|
|
|
return $user_id |
|
172
|
|
|
? $context->get_loader('user')->load_deferred($user_id) |
|
173
|
|
|
: null; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param mixed $source |
|
179
|
|
|
* @param $args |
|
180
|
|
|
* @param $context |
|
181
|
|
|
* @return Deferred|null |
|
182
|
|
|
* @throws Exception |
|
183
|
|
|
* @since $VID:$ |
|
184
|
|
|
*/ |
|
185
|
|
|
public function resolveUserId($source, $args, $context) |
|
186
|
|
|
{ |
|
187
|
|
|
$user_id = $source->wp_user(); |
|
188
|
|
|
return $user_id ? Relay::toGlobalId('user', $user_id) : null; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param mixed $source |
|
194
|
|
|
* @return EE_Base_Class|null |
|
195
|
|
|
* @throws EE_Error |
|
196
|
|
|
* @throws InvalidArgumentException |
|
197
|
|
|
* @throws InvalidDataTypeException |
|
198
|
|
|
* @throws InvalidInterfaceException |
|
199
|
|
|
* @throws ReflectionException |
|
200
|
|
|
* @since $VID:$ |
|
201
|
|
|
*/ |
|
202
|
|
|
public function resolveParent($source) |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->model->get_one_by_ID($source->parent()); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param mixed $source |
|
210
|
|
|
* @param $args |
|
211
|
|
|
* @param $context |
|
212
|
|
|
* @return Deferred|null |
|
213
|
|
|
* @throws EE_Error |
|
214
|
|
|
* @throws InvalidArgumentException |
|
215
|
|
|
* @throws InvalidDataTypeException |
|
216
|
|
|
* @throws InvalidInterfaceException |
|
217
|
|
|
* @throws ReflectionException |
|
218
|
|
|
* @throws UserError |
|
219
|
|
|
* @throws UnexpectedEntityException |
|
220
|
|
|
* @since $VID:$ |
|
221
|
|
|
*/ |
|
222
|
|
|
public function resolveEvent($source, $args, $context) |
|
223
|
|
|
{ |
|
224
|
|
|
switch (true) { |
|
225
|
|
|
case $source instanceof EE_Datetime: |
|
226
|
|
|
$event = $source->event(); |
|
227
|
|
|
break; |
|
228
|
|
|
case $source instanceof EE_Venue: |
|
229
|
|
|
case $source instanceof EE_Ticket: |
|
230
|
|
|
$event = $source->get_related_event(); |
|
|
|
|
|
|
231
|
|
|
break; |
|
232
|
|
|
default: |
|
233
|
|
|
$event = null; |
|
234
|
|
|
break; |
|
235
|
|
|
} |
|
236
|
|
|
return $event instanceof EE_Event |
|
237
|
|
|
? $context->get_loader('post')->load_deferred($event->ID()) |
|
238
|
|
|
: null; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param mixed $source The source instance. |
|
244
|
|
|
* @return EE_Base_Class|null |
|
245
|
|
|
* @throws EE_Error |
|
246
|
|
|
* @throws InvalidArgumentException |
|
247
|
|
|
* @throws InvalidDataTypeException |
|
248
|
|
|
* @throws InvalidInterfaceException |
|
249
|
|
|
* @since $VID:$ |
|
250
|
|
|
*/ |
|
251
|
|
|
public function resolveState($source) |
|
252
|
|
|
{ |
|
253
|
|
|
switch (true) { |
|
254
|
|
|
case $source instanceof EE_Attendee: |
|
255
|
|
|
case $source instanceof EE_Venue: |
|
256
|
|
|
$state_id = $source->state_ID(); |
|
257
|
|
|
break; |
|
258
|
|
|
default: |
|
259
|
|
|
$state_id = null; |
|
260
|
|
|
break; |
|
261
|
|
|
} |
|
262
|
|
|
return $state_id |
|
263
|
|
|
? EEM_State::instance()->get_one_by_ID($state_id) |
|
264
|
|
|
: null; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @param mixed $source The source instance. |
|
270
|
|
|
* @return EE_Base_Class|null |
|
271
|
|
|
* @throws EE_Error |
|
272
|
|
|
* @throws InvalidArgumentException |
|
273
|
|
|
* @throws InvalidDataTypeException |
|
274
|
|
|
* @throws InvalidInterfaceException |
|
275
|
|
|
* @since $VID:$ |
|
276
|
|
|
*/ |
|
277
|
|
|
public function resolveCountry($source) |
|
278
|
|
|
{ |
|
279
|
|
|
switch (true) { |
|
280
|
|
|
case $source instanceof EE_State: |
|
281
|
|
|
$country_iso = $source->country_iso(); |
|
282
|
|
|
break; |
|
283
|
|
|
case $source instanceof EE_Attendee: |
|
284
|
|
|
case $source instanceof EE_Venue: |
|
285
|
|
|
$country_iso = $source->country_ID(); |
|
286
|
|
|
break; |
|
287
|
|
|
default: |
|
288
|
|
|
$country_iso = null; |
|
289
|
|
|
break; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
return $country_iso |
|
293
|
|
|
? EEM_Country::instance()->get_one_by_ID($country_iso) |
|
294
|
|
|
: null; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: