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
|
|
|
|
16
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
17
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
18
|
|
|
use EventEspresso\core\exceptions\UnexpectedEntityException; |
19
|
|
|
use EventEspresso\core\services\graphql\resolvers\ResolverBase; |
20
|
|
|
use EventEspresso\core\services\graphql\fields\GraphQLField; |
21
|
|
|
use Exception; |
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use ReflectionException; |
24
|
|
|
|
25
|
|
|
use WPGraphQL\Data\DataSource; |
26
|
|
|
use GraphQL\Deferred; |
27
|
|
|
use GraphQL\Error\UserError; |
28
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
29
|
|
|
use WPGraphQL\AppContext; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class FieldResolver |
34
|
|
|
* Field resolver for a GraphQL type |
35
|
|
|
* |
36
|
|
|
* @package EventEspresso\core\domain\services\graphql\resolvers |
37
|
|
|
* @author Manzoor Wani |
38
|
|
|
* @since $VID:$ |
39
|
|
|
*/ |
40
|
|
|
class FieldResolver extends ResolverBase |
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var mixed $model |
45
|
|
|
*/ |
46
|
|
|
protected $model; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array $fields . |
50
|
|
|
*/ |
51
|
|
|
protected $fields; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* FieldResolver constructor. |
56
|
|
|
* |
57
|
|
|
* @param mixed $model The model instance. |
58
|
|
|
* @param array $fields The fields registered for the type. |
59
|
|
|
*/ |
60
|
|
|
public function __construct($model, array $fields) |
61
|
|
|
{ |
62
|
|
|
$this->model = $model; |
63
|
|
|
$this->fields = $fields; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param mixed $source The source that's passed down the GraphQL queries |
69
|
|
|
* @param array $args The inputArgs on the field |
70
|
|
|
* @param AppContext $context The AppContext passed down the GraphQL tree |
71
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
72
|
|
|
* @return string |
73
|
|
|
* @throws EE_Error |
74
|
|
|
* @throws Exception |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
* @throws InvalidDataTypeException |
77
|
|
|
* @throws InvalidInterfaceException |
78
|
|
|
* @throws ReflectionException |
79
|
|
|
* @throws UserError |
80
|
|
|
* @throws UnexpectedEntityException |
81
|
|
|
* @since $VID:$ |
82
|
|
|
*/ |
83
|
|
|
public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
84
|
|
|
{ |
85
|
|
|
$fieldName = $info->fieldName; |
86
|
|
|
$field = isset($this->fields[ $fieldName ]) ? $this->fields[ $fieldName ] : null; |
87
|
|
|
// Field should exist in teh registered fields |
88
|
|
|
if ($field instanceof GraphQLField) { |
89
|
|
|
// check if the field should be resolved. |
90
|
|
|
if (! $field->shouldResolve()) { |
91
|
|
|
return null; |
92
|
|
|
} |
93
|
|
|
// Give priority to the internal resolver. |
94
|
|
|
if ($field->hasInternalResolver()) { |
95
|
|
|
return $field->resolve($source, $args, $context, $info); |
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); |
101
|
|
|
} |
102
|
|
|
switch ($fieldName) { |
103
|
|
|
case 'parent': |
104
|
|
|
return $this->resolveParent($source); |
105
|
|
|
case 'event': |
106
|
|
|
return $this->resolveEvent($source, $args, $context); |
107
|
|
|
case 'wpUser': |
108
|
|
|
return $this->resolveWpUser($source, $args, $context); |
109
|
|
|
case 'state': // Venue |
110
|
|
|
return $this->resolveState($source); |
111
|
|
|
case 'country': // State, Venue |
112
|
|
|
return $this->resolveCountry($source); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param mixed $source |
121
|
|
|
* @param $args |
122
|
|
|
* @param $context |
123
|
|
|
* @return Deferred|null |
124
|
|
|
* @throws Exception |
125
|
|
|
* @since $VID:$ |
126
|
|
|
*/ |
127
|
|
|
public function resolveWpUser($source, $args, $context) |
128
|
|
|
{ |
129
|
|
|
$user_id = $source->wp_user(); |
130
|
|
|
return $user_id |
131
|
|
|
? DataSource::resolve_user($user_id, $context) |
132
|
|
|
: null; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param mixed $source |
138
|
|
|
* @return EE_Base_Class|null |
139
|
|
|
* @throws EE_Error |
140
|
|
|
* @throws InvalidArgumentException |
141
|
|
|
* @throws InvalidDataTypeException |
142
|
|
|
* @throws InvalidInterfaceException |
143
|
|
|
* @throws ReflectionException |
144
|
|
|
* @since $VID:$ |
145
|
|
|
*/ |
146
|
|
|
public function resolveParent($source) |
147
|
|
|
{ |
148
|
|
|
return $this->model->get_one_by_ID($source->parent()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param mixed $source |
154
|
|
|
* @param $args |
155
|
|
|
* @param $context |
156
|
|
|
* @return Deferred|null |
157
|
|
|
* @throws EE_Error |
158
|
|
|
* @throws InvalidArgumentException |
159
|
|
|
* @throws InvalidDataTypeException |
160
|
|
|
* @throws InvalidInterfaceException |
161
|
|
|
* @throws ReflectionException |
162
|
|
|
* @throws UserError |
163
|
|
|
* @throws UnexpectedEntityException |
164
|
|
|
* @since $VID:$ |
165
|
|
|
*/ |
166
|
|
|
public function resolveEvent($source, $args, $context) |
167
|
|
|
{ |
168
|
|
|
switch (true) { |
169
|
|
|
case $source instanceof EE_Datetime: |
170
|
|
|
$event = $source->event(); |
171
|
|
|
break; |
172
|
|
|
case $source instanceof EE_Venue: |
173
|
|
|
case $source instanceof EE_Ticket: |
174
|
|
|
$event = $source->get_related_event(); |
|
|
|
|
175
|
|
|
break; |
176
|
|
|
default: |
177
|
|
|
$event = null; |
178
|
|
|
break; |
179
|
|
|
} |
180
|
|
|
return $event instanceof EE_Event |
181
|
|
|
? DataSource::resolve_post_object($event->ID(), $context) |
182
|
|
|
: null; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param mixed $source The source instance. |
188
|
|
|
* @return EE_Base_Class|null |
189
|
|
|
* @throws EE_Error |
190
|
|
|
* @throws InvalidArgumentException |
191
|
|
|
* @throws InvalidDataTypeException |
192
|
|
|
* @throws InvalidInterfaceException |
193
|
|
|
* @since $VID:$ |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public function resolveState($source) |
196
|
|
|
{ |
197
|
|
|
switch (true) { |
198
|
|
|
case $source instanceof EE_Attendee: |
199
|
|
|
case $source instanceof EE_Venue: |
200
|
|
|
$state_id = $source->state_ID(); |
201
|
|
|
break; |
202
|
|
|
default: |
203
|
|
|
$state_id = null; |
204
|
|
|
break; |
205
|
|
|
} |
206
|
|
|
return $state_id |
207
|
|
|
? EEM_State::instance()->get_one_by_ID($state_id) |
208
|
|
|
: null; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param mixed $source The source instance. |
214
|
|
|
* @return EE_Base_Class|null |
215
|
|
|
* @throws EE_Error |
216
|
|
|
* @throws InvalidArgumentException |
217
|
|
|
* @throws InvalidDataTypeException |
218
|
|
|
* @throws InvalidInterfaceException |
219
|
|
|
* @since $VID:$ |
220
|
|
|
*/ |
221
|
|
View Code Duplication |
public function resolveCountry($source) |
222
|
|
|
{ |
223
|
|
|
switch (true) { |
224
|
|
|
case $source instanceof EE_State: |
225
|
|
|
$country_iso = $source->country_iso(); |
226
|
|
|
break; |
227
|
|
|
case $source instanceof EE_Venue: |
228
|
|
|
$country_iso = $source->country_ID(); |
229
|
|
|
break; |
230
|
|
|
default: |
231
|
|
|
$country_iso = null; |
232
|
|
|
break; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $country_iso |
236
|
|
|
? EEM_Country::instance()->get_one_by_ID($country_iso) |
237
|
|
|
: null; |
238
|
|
|
} |
239
|
|
|
} |
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: