获取WP GraphQL API中的记录总数

时间:2020-02-06 作者:Anonymous

我正在使用this 在WP中启用GQL API的插件。

不幸的是,根据this 讨论时,它非常有限,并且不会返回请求实体的记录总数,例如用户列表。

query MyQuery {
  users(first: 1) {
    edges {
      node {
        id
        name
      }
      cursor
    }
    pageInfo {
      endCursor
      startCursor
    }
  }
}

Questions

<有人知道如何使用此API获取记录总数吗

1 个回复
最合适的回答,由SO网友:Anonymous 整理而成

有点明白了。可以使用自定义属性扩展现有模式,这些属性将有自己的回调解析程序。中对自定义类型的引用documentation.

Place this class to your functions.php

class GQLRegister 
{
  var $itemName = null;
  var $itemProps = null;
  var $responseName = null;
  var $responseParams = null;
  var $responseParamsFunction = null;

  function __construct(
    $itemName, 
    $itemProps, 
    $responseName, 
    $responseParams, 
    $responseParamsFunction) {    
    $this->itemName = $itemName;
    $this->itemProps = $itemProps;
    $this->responseName = $responseName;
    $this->responseParams = $responseParams;
    $this->responseParamsFunction = $responseParamsFunction;
    add_action(\'graphql_register_types\', [ $this, \'setItemType\' ]);
    add_action(\'graphql_register_types\', [ $this, \'setResponseType\' ]);
    add_action(\'graphql_register_types\', [ $this, \'setResponseParamsType\' ]);
  }

  function setItemType() {
    register_graphql_object_type($this->itemName, [ \'fields\' => $this->itemProps ]);
  }

  function setResponseType() {
    register_graphql_object_type($this->responseName, [
      \'fields\' => [
        \'count\' => [ \'type\' => \'Integer\' ],
        \'items\' => [ \'type\' => [ \'list_of\' => $this->itemName ]],
        \'errors\' => [ \'type\' => [ \'list_of\' => \'String\' ]]
      ]
    ]);
  }

  function setResponseParamsType() {
    register_graphql_field(\'RootQuery\', $this->responseName, [
      \'type\' => $this->responseName,
      \'args\' => $this->responseParams,
      \'resolve\' => $this->responseParamsFunction
    ]);
  }
}
Call constructor of this class with the following parameters.

new GQLRegister(
  \'CustomUser\',                               // define fields of a single object
  [
    \'id\' => [ \'type\' => \'Integer\' ],
    \'name\' => [ \'type\' => \'String\' ]
  ], 
  \'CustomUserResponse\',
  [                                           // define input parameters for new property
    \'page\' => [ \'type\' => \'Integer\' ],
    \'count\' => [ \'type\' => \'Integer\' ]
  ],
  function($root, $args, $context, $info) {   // extract data from DB in this method
    // $users = get_users($args);

    return [                                
      \'count\' => 1,                           // count($users) 
      \'items\' => [[ \'name\' => \'Demo User\' ]]  // $users
    ];
  }
);
现在,WPGraphQL在查询上显示新属性customUserResponse 可以返回以下结构。

{
  "data": {
    "customUserResponse": {
      "count": 1,
      "items": [
        {
          "name": "Demo User"
        }
      ]
    }
  }
}

相关推荐

参考REST API筛选器中的数组数据

是否可以使用REST API filter或任何其他技术制作一个过滤器,以发现job\\u listing\\u category=86的所有元素?因为job\\u listing\\u类别是一个数组。Actual state:https://parquedasfeiras.online/wp-json/wp/v2/job_listing?filter[meta_key]=_case27_listing_type&filter[meta_value]=place\"job_listing_cate