- Notifications
You must be signed in to change notification settings - Fork5
Adds the ability to fetch total post counts from WP-GraphQL
License
builtbycactus/total-counts-for-wp-graphql
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
As it stands WP-GraphQL doesn't implement thetotalCount 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 thetotalCount field as per theGraphQL docs, it does add atotal field to thepageInfo 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.
To retrieve to total count, it's a simple case of requesting thetotal field with yourpageInfo. See below:
{posts(first:1){edges{node{idtitle}}pageInfo{totalhasNextPageendCursor}}}
This will then give you a result as such:
{"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 viadata.posts.pageInfo.total.
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!
About
Adds the ability to fetch total post counts from WP-GraphQL
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.