0

也许我在我的头上,但我正在尝试为 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;
            }

        ]
    );
4

1 回答 1

0

可能返回一个 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', 'replace-me'),
            ],
            // options field - list of some scalar or custom type 
            'options' => [
                'type' => [ 'list_of' => 'String' ],
                'description' => __('Describe what this field should be used for', 'replace-me'),
            ],

        ],
    ]);

扩展产品:

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的“记录”,您需要注册另一种类型 - 例如id可选选项选择所需的 s。

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

于 2021-05-12T11:52:00.683 回答