1
1
<?php
2
2
3
+ use GraphQLRelay \Relay ;
3
4
use WPGraphQL \AppContext ;
4
5
use WPGraphQL \Data \DataSource ;
5
6
use WPGraphQL \Model \User ;
@@ -47,7 +48,7 @@ public static function resolve_graphql_union_type($field)
47
48
'post_type ' => $ post_type ,
48
49
'graphql_name ' => $ graphql_name ,
49
50
] = $ field ;
50
-
51
+
51
52
$ union_names = array_reduce ($ post_type , function ($ a , $ c ) {
52
53
$ post_type_object = get_post_type_object ($ c );
53
54
if (true === $ post_type_object ->show_in_graphql ) {
@@ -85,16 +86,23 @@ public static function resolve_graphql_union_type($field)
85
86
*/
86
87
public static function resolve_graphql_resolver ($ field , $ meta_args = null )
87
88
{
88
- ['type ' => $ type , 'id ' => $ field_id ] = $ field ;
89
+ [
90
+ 'type ' => $ type ,
91
+ 'id ' => $ field_id ,
92
+ ] = $ field ;
89
93
switch ($ type ) {
94
+ case 'group ' :
95
+ return function ($ node ) use ($ field_id , $ meta_args ) {
96
+ $ group_data = self ::get_field ($ node , $ field_id , $ meta_args );
97
+ return self ::resolve_field ($ group_data , function ($ field_data ) {
98
+ return $ field_data ;
99
+ });
100
+ };
90
101
case 'number ' :
91
102
case 'range ' :
92
- return function ($ node ) use ($ field_id , $ meta_args ) {
103
+ return function ($ node ) use ($ field_id , $ meta_args, $ type ) {
93
104
$ field = self ::get_field ($ node , $ field_id , $ meta_args );
94
- $ resolve_field = function ($ field_data ) {
95
- return is_numeric ($ field_data ) ? $ field_data : null ;
96
- };
97
- return self ::resolve_field ($ field , $ resolve_field );
105
+ return self ::resolve_field ($ field , self ::get_resolver ($ type ));
98
106
};
99
107
case 'switch ' :
100
108
case 'checkbox ' :
@@ -120,23 +128,17 @@ public static function resolve_graphql_resolver($field, $meta_args = null)
120
128
case 'select_advanced ' :
121
129
case 'url ' :
122
130
case 'wysiwyg ' :
123
- return function ($ node ) use ($ field_id , $ meta_args ) {
131
+ return function ($ node ) use ($ field_id , $ meta_args, $ type ) {
124
132
$ field = self ::get_field ($ node , $ field_id , $ meta_args );
125
- $ resolve_field = function ($ field_data ) {
126
- return isset ($ field_data ) ? $ field_data : null ;
127
- };
128
- return self ::resolve_field ($ field , $ resolve_field );
133
+ return self ::resolve_field ($ field , self ::get_resolver ($ type ));
129
134
};
130
135
case 'single_image ' :
131
- return function ($ node , $ args ) use ($ field_id , $ meta_args ) {
136
+ return function ($ node , $ args ) use ($ field_id , $ meta_args, $ type ) {
132
137
$ size = !isset ($ args ['size ' ]) ? 'thumbnail ' : $ args ['size ' ];
133
138
$ merged_args = array_merge ($ meta_args , ['size ' => $ size ]);
134
139
$ field = rwmb_meta ($ field_id , $ merged_args , $ node ->ID );
135
- $ resolve_field = function ($ field_data ) {
136
- return isset ($ field_data ) ? $ field_data : null ;
137
- };
138
140
139
- return self ::resolve_field ($ field , $ resolve_field );
141
+ return self ::resolve_field ($ field , self :: get_resolver ( $ type ) );
140
142
};
141
143
case 'user ' :
142
144
return function ($ node , $ args , AppContext $ context ) use ($ field_id , $ meta_args ) {
@@ -198,6 +200,82 @@ public static function resolve_graphql_args($type)
198
200
}
199
201
}
200
202
203
+ /**
204
+ * Resolves nullable fields
205
+ *
206
+ * @return function A resolver function
207
+ * @since 0.4.0
208
+ * @access private
209
+ */
210
+ private static function resolve_nullable_field ()
211
+ {
212
+ return function ($ field_data ) {
213
+ return isset ($ field_data ) ? $ field_data : null ;
214
+ };
215
+ }
216
+
217
+ /**
218
+ * Resolves nullable numric fields
219
+ *
220
+ * @return function The field type resolver
221
+ * @since 0.4.0
222
+ * @access private
223
+ */
224
+ private static function resolve_numeric_field ()
225
+ {
226
+ return function ($ field_data ) {
227
+ return is_numeric ($ field_data ) ? $ field_data : null ;
228
+ };
229
+ }
230
+
231
+ /**
232
+ * Gets the appropriate field resolver based on field type
233
+ *
234
+ * @var string The Metabox field type
235
+ * @return function The field resolver
236
+ * @since 0.4.0
237
+ * @access private
238
+ */
239
+ private static function get_resolver ($ type )
240
+ {
241
+ switch ($ type ) {
242
+ case 'group ' :
243
+ case 'number ' :
244
+ case 'range ' :
245
+ return self ::resolve_numeric_field ();
246
+ case 'switch ' :
247
+ case 'checkbox ' :
248
+ case 'checkbox_list ' :
249
+ case 'background ' :
250
+ case 'color ' :
251
+ case 'custom_html ' :
252
+ case 'date ' :
253
+ case 'heading ' :
254
+ case 'datetime ' :
255
+ case 'oembed ' :
256
+ case 'password ' :
257
+ case 'radio ' :
258
+ case 'textarea ' :
259
+ case 'time ' :
260
+ case 'select ' :
261
+ case 'email ' :
262
+ case 'tel ' :
263
+ case 'text ' :
264
+ case 'fieldset_text ' :
265
+ case 'text_list ' :
266
+ case 'key_value ' :
267
+ case 'select_advanced ' :
268
+ case 'url ' :
269
+ case 'wysiwyg ' :
270
+ case 'single_image ' :
271
+ return self ::resolve_nullable_field ();
272
+ default :
273
+ return function () {
274
+ return null ;
275
+ };
276
+ }
277
+ }
278
+
201
279
/**
202
280
* Resolves metabox field data by entity type
203
281
*
@@ -227,6 +305,7 @@ private static function get_field($node, $field_id, $args = null)
227
305
private static function get_base_graphql_type ($ field )
228
306
{
229
307
['type ' => $ type ] = $ field ;
308
+
230
309
switch ($ type ) {
231
310
case 'autocomplete ' :
232
311
case 'button ' :
@@ -305,6 +384,45 @@ private static function get_base_graphql_type($field)
305
384
}
306
385
307
386
return $ post_type_object ->graphql_single_name ;
387
+ case 'group ' :
388
+ [
389
+ 'graphql_name ' => $ graphql_name ,
390
+ 'fields ' => $ fields ,
391
+ ] = $ field ;
392
+ register_graphql_object_type ($ graphql_name , [
393
+ 'description ' => __ ("$ graphql_name Group " , 'wpgraphql-metabox ' ),
394
+ 'fields ' => array_reduce ($ fields , function ($ fields , $ field ) {
395
+ if (!key_exists ('graphql_name ' , $ field )) {
396
+ return $ fields ;
397
+ }
398
+ [
399
+ 'graphql_name ' => $ graphql_name ,
400
+ 'name ' => $ name ,
401
+ 'id ' => $ id
402
+ ] = $ field ;
403
+ $ graphql_type = WPGraphQL_MetaBox_Util::resolve_graphql_type ($ field );
404
+ $ fields [$ graphql_name ] = [
405
+ 'type ' => $ graphql_type ,
406
+ 'description ' => $ name ,
407
+ 'resolve ' => function ($ node ) use ($ id ) {
408
+ return $ node [$ id ];
409
+ },
410
+ 'args ' => WPGraphQL_MetaBox_Util::resolve_graphql_args ($ graphql_type ),
411
+ ];
412
+
413
+ return $ fields ;
414
+ }, [
415
+ 'id ' => [
416
+ 'type ' => 'ID ' ,
417
+ 'description ' => __ ('Generated ID ' , 'wpgraphql-metabox ' ),
418
+ 'resolve ' => function ($ node ) use ($ graphql_name ) {
419
+ return Relay::toGlobalId ($ graphql_name , hash ('md5 ' , json_encode ($ node )));
420
+ }
421
+ ]
422
+ ]),
423
+ ]);
424
+
425
+ return $ graphql_name ;
308
426
default :
309
427
error_log (__ ("Unknown Meta Box type supplied to wpgraphql-metabox: $ type " , 'wpgraphql-metabox ' ));
310
428
}
@@ -319,7 +437,6 @@ private static function get_base_graphql_type($field)
319
437
* @since 0.3.0
320
438
* @access private
321
439
*/
322
-
323
440
private static function resolve_field ($ field_config , $ field_resolver )
324
441
{
325
442
// cloned or multiple field
0 commit comments