1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\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 |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array $caps |
20
|
|
|
*/ |
21
|
|
|
protected $caps; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string $description |
25
|
|
|
*/ |
26
|
|
|
protected $description; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var callable $formatter |
30
|
|
|
*/ |
31
|
|
|
protected $formatter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string|null $key |
35
|
|
|
*/ |
36
|
|
|
protected $key; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string $name |
40
|
|
|
*/ |
41
|
|
|
protected $name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var callable $resolve |
45
|
|
|
*/ |
46
|
|
|
protected $resolver; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|string[] $type |
50
|
|
|
*/ |
51
|
|
|
protected $type; |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* @param string|string[] $type |
57
|
|
|
* @param string|null $key |
58
|
|
|
* @param string $description |
59
|
|
|
* @param callable|null $formatter |
60
|
|
|
* @param callable|null $resolver |
61
|
|
|
* @param array $caps |
62
|
|
|
*/ |
63
|
|
|
public function __construct( |
64
|
|
|
$name, |
65
|
|
|
$type, |
66
|
|
|
$key = null, |
67
|
|
|
$description = '', |
68
|
|
|
callable $formatter = null, |
69
|
|
|
callable $resolver = null, |
70
|
|
|
array $caps = [] |
71
|
|
|
) { |
72
|
|
|
$this->key = $key; |
73
|
|
|
$this->name = $name; |
74
|
|
|
$this->type = $type; |
75
|
|
|
$this->description = $description; |
76
|
|
|
$this->formatter = $formatter; |
77
|
|
|
$this->resolver = $resolver; |
78
|
|
|
$this->caps = $caps; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public function caps() |
86
|
|
|
{ |
87
|
|
|
return $this->caps; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function description() |
95
|
|
|
{ |
96
|
|
|
return $this->description; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function key() |
104
|
|
|
{ |
105
|
|
|
return $this->key; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function name() |
113
|
|
|
{ |
114
|
|
|
return $this->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string|string[] |
120
|
|
|
*/ |
121
|
|
|
public function type() |
122
|
|
|
{ |
123
|
|
|
return $this->type; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Whether the field should resolve |
131
|
|
|
* based on the user caps etc. |
132
|
|
|
* |
133
|
|
|
* @return boolean |
134
|
|
|
*/ |
135
|
|
|
public function shouldResolve() |
136
|
|
|
{ |
137
|
|
|
foreach ($this->caps as $cap) { |
138
|
|
|
if (! current_user_can($cap)) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Whether the field has an explicit resolver set. |
148
|
|
|
* |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
|
|
public function hasInternalResolver() |
152
|
|
|
{ |
153
|
|
|
return is_callable($this->resolver); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Whether the field has an explicit resolver set. |
159
|
|
|
* |
160
|
|
|
* @param mixed $source The source that's passed down the GraphQL queries |
161
|
|
|
* @param array $args The inputArgs on the field |
162
|
|
|
* @param AppContext $context The AppContext passed down the GraphQL tree |
163
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
164
|
|
|
* @return mixed |
165
|
|
|
* @throws LogicException |
166
|
|
|
*/ |
167
|
|
|
public function resolve($source, array $args, AppContext $context, ResolveInfo $info) |
168
|
|
|
{ |
169
|
|
|
if (! $this->hasInternalResolver()) { |
170
|
|
|
throw new LogicException('GraphQLField has no internal resolver.'); |
171
|
|
|
} |
172
|
|
|
// dynamic methods using $this don't play nice |
173
|
|
|
// so capture resolver to a single var first |
174
|
|
|
$resolver = $this->resolver; |
175
|
|
|
return $resolver($source, $args, $context, $info); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Checks if the format callback is set. |
181
|
|
|
* If yes, then uses it to format the value. |
182
|
|
|
* |
183
|
|
|
* @param mixed $value |
184
|
|
|
* @return mixed The formatted value. |
185
|
|
|
*/ |
186
|
|
|
public function mayBeFormatValue($value) |
187
|
|
|
{ |
188
|
|
|
if (is_callable($this->formatter)) { |
189
|
|
|
// dynamic methods using $this don't play nice |
190
|
|
|
// so capture formatter to a single var first |
191
|
|
|
$formatter = $this->formatter; |
192
|
|
|
return $formatter($value); |
193
|
|
|
} |
194
|
|
|
return $value; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Convert the field to array to be |
200
|
|
|
* able to pass as config to WP GraphQL |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
public function toArray() |
205
|
|
|
{ |
206
|
|
|
return get_object_vars($this); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|