Skip to content

Commit 7472d35

Browse files
authored
Feature/support group fields (#33)
* add group to schema * resolve groups with id * bail out of config if group field has no graphql name * update readme
1 parent c7e1763 commit 7472d35

File tree

3 files changed

+141
-24
lines changed

3 files changed

+141
-24
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ The WPGraphQL documentation can be found [here](https://docs.wpgraphql.com).
88

99
- Requires PHP 7.3+
1010
- Requires WordPress 5.4+
11-
- Requires WPGraphQL 0.8.1+
12-
- Requires Meta Box 5.2.10+
11+
- Requires WPGraphQL 1.0.4+
12+
- Requires Meta Box 5.3.3+
1313
- Requires MB User Meta extension for User fields
1414

1515
## Overview
@@ -66,7 +66,7 @@ query {
6666
```
6767

6868
## Limitations
69-
Currently the plugin only supports custom fields on `post` types (ie no User or Settings Pages).
69+
Currently the plugin only supports custom fields on `post` and `user` types (ie no Settings Pages).
7070

7171
Currently the plugin only supports using the following Meta Box types:
7272
- `switch`
@@ -92,4 +92,4 @@ Currently the plugin only supports using the following Meta Box types:
9292
- `key_value`
9393
- `select_advanced`
9494
- `url`
95-
- `single_image`
95+
- `single_image`

src/util.php

+135-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use GraphQLRelay\Relay;
34
use WPGraphQL\AppContext;
45
use WPGraphQL\Data\DataSource;
56
use WPGraphQL\Model\User;
@@ -47,7 +48,7 @@ public static function resolve_graphql_union_type($field)
4748
'post_type' => $post_type,
4849
'graphql_name' => $graphql_name,
4950
] = $field;
50-
51+
5152
$union_names = array_reduce($post_type, function ($a, $c) {
5253
$post_type_object = get_post_type_object($c);
5354
if (true === $post_type_object->show_in_graphql) {
@@ -85,16 +86,23 @@ public static function resolve_graphql_union_type($field)
8586
*/
8687
public static function resolve_graphql_resolver($field, $meta_args = null)
8788
{
88-
['type' => $type, 'id' => $field_id] = $field;
89+
[
90+
'type' => $type,
91+
'id' => $field_id,
92+
] = $field;
8993
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+
};
90101
case 'number':
91102
case 'range':
92-
return function ($node) use ($field_id, $meta_args) {
103+
return function ($node) use ($field_id, $meta_args, $type) {
93104
$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));
98106
};
99107
case 'switch':
100108
case 'checkbox':
@@ -120,23 +128,17 @@ public static function resolve_graphql_resolver($field, $meta_args = null)
120128
case 'select_advanced':
121129
case 'url':
122130
case 'wysiwyg':
123-
return function ($node) use ($field_id, $meta_args) {
131+
return function ($node) use ($field_id, $meta_args, $type) {
124132
$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));
129134
};
130135
case 'single_image':
131-
return function ($node, $args) use ($field_id, $meta_args) {
136+
return function ($node, $args) use ($field_id, $meta_args, $type) {
132137
$size = !isset($args['size']) ? 'thumbnail' : $args['size'];
133138
$merged_args = array_merge($meta_args, ['size' => $size]);
134139
$field = rwmb_meta($field_id, $merged_args, $node->ID);
135-
$resolve_field = function ($field_data) {
136-
return isset($field_data) ? $field_data : null;
137-
};
138140

139-
return self::resolve_field($field, $resolve_field);
141+
return self::resolve_field($field, self::get_resolver($type));
140142
};
141143
case 'user':
142144
return function ($node, $args, AppContext $context) use ($field_id, $meta_args) {
@@ -198,6 +200,82 @@ public static function resolve_graphql_args($type)
198200
}
199201
}
200202

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+
201279
/**
202280
* Resolves metabox field data by entity type
203281
*
@@ -227,6 +305,7 @@ private static function get_field($node, $field_id, $args = null)
227305
private static function get_base_graphql_type($field)
228306
{
229307
['type' => $type] = $field;
308+
230309
switch ($type) {
231310
case 'autocomplete':
232311
case 'button':
@@ -305,6 +384,45 @@ private static function get_base_graphql_type($field)
305384
}
306385

307386
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;
308426
default:
309427
error_log(__("Unknown Meta Box type supplied to wpgraphql-metabox: $type", 'wpgraphql-metabox'));
310428
}
@@ -319,7 +437,6 @@ private static function get_base_graphql_type($field)
319437
* @since 0.3.0
320438
* @access private
321439
*/
322-
323440
private static function resolve_field($field_config, $field_resolver)
324441
{
325442
// cloned or multiple field

wp-graphql-metabox.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* Description: WP GraphQL provider for Meta Box
77
* Author: hsimah
88
* Author URI: http://www.hsimah.com
9-
* Version: 0.3.1
9+
* Version: 0.4.0
1010
* Text Domain: wpgraphql-metabox
1111
* License: GPL-3
1212
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
1313
*
1414
* @package WPGraphQL_MetaBox
1515
* @author hsimah
16-
* @version 0.3.1
16+
* @version 0.4.0
1717
*/
1818

1919
// Exit if accessed directly.

0 commit comments

Comments
 (0)