https://github.com/builtbycactus/total-counts-for-wp-graphql
Adds the ability to fetch total post counts from WP-GraphQL
https://github.com/builtbycactus/total-counts-for-wp-graphql
wordpress wordpress-plugin wp-graphql
Last synced: about 1 month ago
JSON representation
Adds the ability to fetch total post counts from WP-GraphQL
- Host: GitHub
- URL: https://github.com/builtbycactus/total-counts-for-wp-graphql
- Owner: builtbycactus
- License: gpl-2.0
- Created: 2019-08-21T11:32:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-08-31T16:29:17.000Z (over 2 years ago)
- Last Synced: 2024-11-01T20:36:19.123Z (6 months ago)
- Topics: wordpress, wordpress-plugin, wp-graphql
- Language: PHP
- Size: 11.7 KB
- Stars: 18
- Watchers: 2
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-wordpress-gatsby - Total Counts for WPGraphQL - This FREE plugin from @builtbycactus exposes total counts to connections in the WPGraphQL Schema. (Plugins / WordPress)
README
# Total Counts for WP-GraphQL
As it stands WP-GraphQL doesn't implement the `totalCount` field. This is due to a number of reasons, including the performance impact that would come with such a query.
We found that we needed this for a site being developed, so built up this basic plugin.
Whilst this doesn't implement the `totalCount` field as per the [GraphQL docs](https://graphql.org/learn/pagination/#end-of-list-counts-and-connections), it does add a `total` field to the `pageInfo` object that is used for pagination. This seemed like the most sensible place to put it, as 99% of the time, we'll be using this field to pull back the total number of items, alongside our paginated result.
## Retrieving the total count.
To retrieve to total count, it's a simple case of requesting the `total` field with your `pageInfo`. See below:
```js
{
posts(first:1) {
edges {
node {
id
title
}
}
pageInfo {
total
hasNextPage
endCursor
}
}
}
```This will then give you a result as such:
```json
{
"data": {
"posts": {
"edges": [
{
"node": {
"id": "cG9zdDozOA==",
"title": "Post B"
}
}
],
"pageInfo": {
"total": 2,
"hasNextPage": true,
"endCursor": "YXJyYXljb25uZWN0aW9uOjM4"
}
}
}
}
```As you can see, the total number of items is accessible via `data.posts.pageInfo.total`.
## Contributions
Contributions are welcome. This was a very quick build and as such there are probably performance gains to be had.
Feel free to make a PR against this repo!