WpGraphQL:创建扩展 - 有问题

问题描述

也许我脑子进水了,但我正在尝试为 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','fields' => [
                    'form_type' => [
                        'type' => 'String',],]);

    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;
            }

        ]
    );

解决方法

可能返回一个 Info 'records' 数组就足够了(不嵌入到 nodes 子字段中 - 它需要一些自己的 'InfoList' 类型定义。),然后你需要一个单一的 Info 类型,喜欢:

    register_graphql_object_type('MyInfoItem',[
        'description' => __('Describe the Type and what it represents','replace-me'),'fields' => [
            'form_type' => [
                'type' => 'String','description' => __('Describe what this field should be used for',],// options field - list of some scalar or custom type 
            'options' => [
                'type' => [ 'list_of' => 'String' ],]);

扩展产品:

register_graphql_field(
    'Product','YITH_fields',[
        'type'  => [ 'list_of' => 'MyInfoItem' ],'description' => __('Example field added to the Post Type','replace-with-your-textdomain'),'resolve' => function (\WPGraphQL\Model\Post $post) {
             // ... 
             return [
                 ['form_type' => 'some_value'],[
                     'form_type' => 'other_value','options' => ['opt_1','opt_2'],];

任何重要的数据“嵌套级别”都需要单独的类型定义。

对于更复杂的 options 'records',您需要注册另一种类型 - 例如id 是可选选项选择所必需的。

如果不想明确表达这些[未知/动态]结构,可以使用自定义JSON标量类型。