WpGraphQL:创建扩展 - 遇到问题

问题描述 投票:0回答:1

也许我有点不知所措,但我正在尝试为 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;
            }

        ]
    );
php wordpress woocommerce wordpress-plugin-creation wp-graphql
1个回答
0
投票

可能返回一个 Info“记录”数组就足够了(无需嵌入到

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
是可选选项选择所必需的。

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

© www.soinside.com 2019 - 2024. All rights reserved.