也许我在我的头上,但我正在尝试为 WpGraphQL 创建一个扩展,以收集每个产品的 YITH WooCommerce 产品附加组件信息,但我已经到了沮丧地把头撞在桌子上的地步. 有没有一种相当简单的方法可以将数组输出到节点中?以下是我到目前为止所拥有的,目前我只得到一个具有集合 ID 的产品(稍后,希望它会得到与该查询关联的那个)。我可以得到一个简单的字符串输出,但我不能让它将数组输出到节点中?我究竟做错了什么?我注释掉了我对输出的想法,但它通常会导致错误或空值。我确实看到 graphql_debug 工作并将 $fields 作为数组输出?谢谢!
register_graphql_object_type('MyNewType', [
'description' => __('Describe the Type and what it represents', 'replace-me'),
'fields' => [
'nodes' => [
'type' => 'String',
'description' => __('Describe what this field should be used for', 'replace-me'),
'fields' => [
'form_type' => [
'type' => 'String',
'description' => __('Describe what this field should be used for', 'replace-me'),
],
],
],
],
]);
register_graphql_field(
'Product',
'YITH_fields',
[
'type' => 'MyNewType',
'description' => __('Example field added to the Post Type', 'replace-with-your-textdomain'),
'resolve' => function (\WPGraphQL\Model\Post $post) {
global $wpdb;
$sql = "SELECT wp_live_yith_wapo_types.type, wp_live_yith_wapo_types.options FROM wp_live_yith_wapo_groups JOIN wp_live_yith_wapo_types on wp_live_yith_wapo_groups.id = wp_live_yith_wapo_types.group_id WHERE FIND_IN_SET(13, products_id)";
$results = $wpdb->get_results($sql);
if ($results) {
$array = array('nodes');
//$array = array();
foreach ($results as $result) {
$type = array('form_type' => $result->type);
$options = maybe_unserialize($result->options);
$result = array_merge($type, $options);
$array[] = $result;
}
//$array = wp_json_encode($array);
$fields = !empty($array) ? $array : null;
} else {
$fields = null;
}
graphql_debug($fields, ['type' => 'ARGS_BREAKPOINT']);
//return $fields['nodes'];
return $fields;
}
]
);