Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can graphql return aggregate counts?

Tags:

graphql

People also ask

What does GraphQL return?

After being validated, a GraphQL query is executed by a GraphQL server which returns a result that mirrors the shape of the requested query, typically as JSON.

What is aggregation in GraphQL?

Aggregate queries fetch aggregate data, including the following: Count queries that let you count fields satisfying certain criteria specified using a filter. Advanced aggregate queries that let you calculate the maximum, minimum, sum and average of specified fields.

Can GraphQL return an object?

In other words, if you return an empty object ( {} ), an empty array ( [] ) or some other value, GraphQL will treat this as you returning an object and not a null value!


You would define a new GraphQL type that is an object that contains a list and a number. The number would be defined by a resolver function.

On your GraphQL server you can define the resolver function and as part of that, you would have to write the code that performs whatever calculations and queries are necessary to get the aggregate counts.

This is similar to how you would write an object serializer for a REST API or a custom REST API endpoint that runs whatever database queries are needed to calculate the aggregate counts.

GraphQL's strength is that it gives the frontend more power in determining what data specifically is returned. Some of what you write in GraphQL will be the same as what you would write for a REST API.


You should define a Type of aggregated data in Graphql and a function you want to implement it. For example, if you want to write the following query:

SELECT age, sum(score) from student group by age;

You should define the data type that you want to return:

 type StudentScoreByAge{
      age: Int
      sumOfScore: Float
    }

and a Graphql function:

 getStudentScoreByAge : [StudentScoreByAge]
    async function(){
        const res = await client.query("SELECT age, sum(score) as sumOfScore 
                                        from Student group by age");
        return res.rows;
    }

There's no automatic aggregate function in GraphQL itself.

You can add a field called summary, and in the resolve function calculate the totals.


... need graphql to return aggregate counts? Can this be done?

Yes, it can be done.

Does GraphQL does it automatically for you? No, because it does not know / care about where you get your data source.

How? GraphQL does not dictate how you get / mutate the data that the user has queried. It's up to your implementation to get the requested aggregated data. You could get aggregated data directly from your MongoDB and serve it back, or you get all the data you need from your data source and do the aggregation yourself.


This depends on whether you build the aggregator into your schema and are able to resolve the field.

Can you share what kind of GraphQL Server you're running? As different languages have different implementations, as well as different services (like Hasura, 8base, and Prisma).

Also, when you say "counts", I'm imagining a count of objects in a relation. Such as:

query {
  user(id: "1") {
    name
    summaries {
      count
    }
  }
}

// returns
{
  "data": {
    "user": {
      "name": "Steve",
      "summaries": {
        "count": 10
      }
    }
  }
}

8base provides the count aggregate by default on relational queries.


If you are using Hasura, in the explorer, you can definitely see an "agregate" table name, thus, your query would look something similar to the following:

query queryTable {
 table_name { 
  field1
  field2
}
table_name_aggregate { 
aggregate { count } 
  } 
}

In your results, you will see the total row count for the query

"table_name_aggregate": {
      "aggregate": {
        "count": 9973
      }