Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add count GraphQL guide #5447

Merged
merged 3 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ module.exports = {
'/3.0.0-beta.x/guides/scheduled-publication',
'/3.0.0-beta.x/guides/slug',
'/3.0.0-beta.x/guides/send-email',
'/3.0.0-beta.x/guides/count-graphql',
],
},
{
Expand Down
79 changes: 79 additions & 0 deletions docs/3.0.0-beta.x/guides/count-graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Count with GraphQL

This guide explains how to count data with a GraphQL query.

The count aggregation has some issues and this feature is also not available for Bookshelf (SQL databases).

With this guide we will code our own count query.

Here is the [GraphQL documentation](../plugins/graphql.md#customise-the-graphql-schema) we will use to achieve our goal.

## Setup the application

In this example, we will use a **Restaurant** API.

Make sure your have a **Content Type** with some entries.
derrickmehaffy marked this conversation as resolved.
Show resolved Hide resolved

## Create schema.grahql file

To be able to add a new custom query (or mutation), we will have to create a `schema.grahql` file in your **Restaurant** API.
derrickmehaffy marked this conversation as resolved.
Show resolved Hide resolved

**Path** — `./api/restaurant/config/schema.graphql`

```js
module.exports = {
query: ``,
resolver: {
Query: {},
},
};
```

## Create count query

The `count` query will call the [`count`](../concepts/services.md#core-services) service function of the **Restaurant** API.

It needs a JSON object as params, so we will add a `where` options in the GraphQL query.

**Path** — `./api/restaurant/config/schema.graphql`

```js
module.exports = {
query: `
restaurantsCount(where: JSON): Int!
`,
resolver: {
Query: {
restaurantsCount: {
description: 'Return the count of restaurants',
resolverOf: 'application::restaurant.restaurant.count',
resolver: async (obj, options, ctx) => {
return await strapi.api.restaurant.services.restaurant.count(options.where || {});
},
},
},
},
};
```

And tada, you can now request the `count` of your Content Type.

## Query example

- Count all restaurants

```
{
restaurantsCount
}
```

- Count all restaurants that have `_3rd` as district value.

Based on the [FoodAdvisor](https://github.com/strapi/foodadvisor) restraurant model.

```
{
restaurantsCount(where: { district: "_3rd" })
}
```