admin管理员组文章数量:1395744
I'm using this plugin to enable GQL API in WP.
Unfortunately, according to this discussion, it's pretty limited and doesn't return total number of records for requested entity, e.g. list of users.
query MyQuery {
users(first: 1) {
edges {
node {
id
name
}
cursor
}
pageInfo {
endCursor
startCursor
}
}
}
Questions
- Does anybody know how to get total count of records using this API?
- Is there any other way to implement GraphQL for WP using normal offset - limit pagination with total count?
I'm using this plugin to enable GQL API in WP.
Unfortunately, according to this discussion, it's pretty limited and doesn't return total number of records for requested entity, e.g. list of users.
query MyQuery {
users(first: 1) {
edges {
node {
id
name
}
cursor
}
pageInfo {
endCursor
startCursor
}
}
}
Questions
- Does anybody know how to get total count of records using this API?
- Is there any other way to implement GraphQL for WP using normal offset - limit pagination with total count?
1 Answer
Reset to default 0Kind of figured it out. It's possible to extend existing schema with custom properties that will have their own callbacks-resolvers. Reference to custom types in the 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
];
}
);
Now, WPGraphQL shows new property on the query customUserResponse that can return the following structure.
{
"data": {
"customUserResponse": {
"count": 1,
"items": [
{
"name": "Demo User"
}
]
}
}
}
本文标签: Get total count of records in WP GraphQL API
版权声明:本文标题:Get total count of records in WP GraphQL API 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744768894a2624214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论